vite-plugin-react-server 0.3.9 → 0.3.11

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (197) hide show
  1. package/dist/client.d.ts +1 -1
  2. package/dist/client.d.ts.map +1 -1
  3. package/dist/client.js +1 -1
  4. package/dist/index.d.ts +3 -6
  5. package/dist/index.d.ts.map +1 -1
  6. package/dist/index.js +2 -2
  7. package/dist/index.js.map +1 -1
  8. package/dist/package.json +2 -4
  9. package/dist/plugin/assertServerCondition.d.ts +5 -1
  10. package/dist/plugin/assertServerCondition.d.ts.map +1 -1
  11. package/dist/plugin/assertServerCondition.js +1 -3
  12. package/dist/plugin/checkFilesExist.js +10 -7
  13. package/dist/plugin/checkFilesExist.js.map +1 -1
  14. package/dist/plugin/components.js +1 -1
  15. package/dist/plugin/components.js.map +1 -1
  16. package/dist/plugin/config/createModuleIdGenerator.d.ts +11 -0
  17. package/dist/plugin/config/createModuleIdGenerator.d.ts.map +1 -0
  18. package/dist/plugin/config/createModuleIdGenerator.js +44 -0
  19. package/dist/plugin/config/createModuleIdGenerator.js.map +1 -0
  20. package/dist/plugin/config/defaults.d.ts +31 -19
  21. package/dist/plugin/config/defaults.d.ts.map +1 -1
  22. package/dist/plugin/config/defaults.js +22 -27
  23. package/dist/plugin/config/defaults.js.map +1 -1
  24. package/dist/plugin/config/resolveOptions.d.ts +1 -1
  25. package/dist/plugin/config/resolveOptions.d.ts.map +1 -1
  26. package/dist/plugin/config/resolveOptions.js +202 -16
  27. package/dist/plugin/config/resolveOptions.js.map +1 -1
  28. package/dist/plugin/config/resolvePages.d.ts +2 -0
  29. package/dist/plugin/config/resolvePages.d.ts.map +1 -1
  30. package/dist/plugin/config/resolvePages.js.map +1 -1
  31. package/dist/plugin/config/resolveUserConfig.d.ts +2 -2
  32. package/dist/plugin/config/resolveUserConfig.d.ts.map +1 -1
  33. package/dist/plugin/config/resolveUserConfig.js +161 -50
  34. package/dist/plugin/config/resolveUserConfig.js.map +1 -1
  35. package/dist/plugin/helpers/getBundleManifest.d.ts +20 -0
  36. package/dist/plugin/helpers/getBundleManifest.d.ts.map +1 -0
  37. package/dist/plugin/helpers/getBundleManifest.js +43 -0
  38. package/dist/plugin/helpers/getBundleManifest.js.map +1 -0
  39. package/dist/plugin/helpers/getModuleManifest.d.ts +5 -0
  40. package/dist/plugin/helpers/getModuleManifest.d.ts.map +1 -1
  41. package/dist/plugin/helpers/getModuleManifest.js +20 -21
  42. package/dist/plugin/helpers/inputNormalizer.d.ts +15 -1
  43. package/dist/plugin/helpers/inputNormalizer.d.ts.map +1 -1
  44. package/dist/plugin/helpers/inputNormalizer.js +122 -16
  45. package/dist/plugin/helpers/inputNormalizer.js.map +1 -1
  46. package/dist/plugin/helpers/inputNormalizerWorker.d.ts +2 -1
  47. package/dist/plugin/helpers/inputNormalizerWorker.d.ts.map +1 -1
  48. package/dist/plugin/helpers/inputNormalizerWorker.js +10 -7
  49. package/dist/plugin/helpers/tryManifest.d.ts +2 -0
  50. package/dist/plugin/helpers/tryManifest.d.ts.map +1 -1
  51. package/dist/plugin/helpers/tryManifest.js +1 -1
  52. package/dist/plugin/helpers/tryManifest.js.map +1 -1
  53. package/dist/plugin/loader/createBuildLoader.d.ts +6 -2
  54. package/dist/plugin/loader/createBuildLoader.d.ts.map +1 -1
  55. package/dist/plugin/loader/createBuildLoader.js +35 -10
  56. package/dist/plugin/loader/createBuildLoader.js.map +1 -1
  57. package/dist/plugin/loader/createPageLoader.d.ts.map +1 -1
  58. package/dist/plugin/loader/createPageLoader.js +0 -7
  59. package/dist/plugin/plugin.d.ts +0 -1
  60. package/dist/plugin/plugin.d.ts.map +1 -1
  61. package/dist/plugin/plugin.js +2 -1
  62. package/dist/plugin/preserver/plugin.d.ts.map +1 -1
  63. package/dist/plugin/preserver/plugin.js +3 -2
  64. package/dist/plugin/preserver/plugin.js.map +1 -1
  65. package/dist/plugin/react-client/index.d.ts +2 -1
  66. package/dist/plugin/react-client/index.d.ts.map +1 -1
  67. package/dist/plugin/react-client/index.js +19 -1
  68. package/dist/plugin/react-client/index.js.map +1 -0
  69. package/dist/plugin/react-client/plugin.d.ts +2 -2
  70. package/dist/plugin/react-client/plugin.d.ts.map +1 -1
  71. package/dist/plugin/react-client/plugin.js +106 -10
  72. package/dist/plugin/react-client/plugin.js.map +1 -1
  73. package/dist/plugin/react-server/createHandler.d.ts +2 -2
  74. package/dist/plugin/react-server/createHandler.d.ts.map +1 -1
  75. package/dist/plugin/react-server/createHandler.js +5 -5
  76. package/dist/plugin/react-server/createHandler.js.map +1 -1
  77. package/dist/plugin/react-server/createRscStream.d.ts.map +1 -1
  78. package/dist/plugin/react-server/createRscStream.js +15 -1
  79. package/dist/plugin/react-server/createRscStream.js.map +1 -1
  80. package/dist/plugin/react-server/createSsrHandler.d.ts +2 -2
  81. package/dist/plugin/react-server/createSsrHandler.d.ts.map +1 -1
  82. package/dist/plugin/react-server/createSsrHandler.js +5 -12
  83. package/dist/plugin/react-server/index.js +18 -9
  84. package/dist/plugin/react-server/index.js.map +1 -0
  85. package/dist/plugin/react-server/plugin.d.ts.map +1 -1
  86. package/dist/plugin/react-server/plugin.js +133 -127
  87. package/dist/plugin/react-server/plugin.js.map +1 -1
  88. package/dist/plugin/transformer/plugin.d.ts +2 -1
  89. package/dist/plugin/transformer/plugin.d.ts.map +1 -1
  90. package/dist/plugin/transformer/plugin.js +52 -72
  91. package/dist/plugin/transformer/plugin.js.map +1 -1
  92. package/dist/plugin/transformer/transformer-client-components.d.ts +12 -3
  93. package/dist/plugin/transformer/transformer-client-components.d.ts.map +1 -1
  94. package/dist/plugin/transformer/transformer-client-components.js +71 -10
  95. package/dist/plugin/transformer/transformer-client-components.js.map +1 -1
  96. package/dist/plugin/transformer/transformer-server-actions.d.ts +3 -3
  97. package/dist/plugin/transformer/transformer-server-actions.d.ts.map +1 -1
  98. package/dist/plugin/transformer/transformer-server-actions.js +80 -66
  99. package/dist/plugin/transformer/types.d.ts +4 -0
  100. package/dist/plugin/transformer/types.d.ts.map +1 -1
  101. package/dist/plugin/types.d.ts +51 -20
  102. package/dist/plugin/types.d.ts.map +1 -1
  103. package/dist/plugin/worker/html/messageHandler.d.ts.map +1 -1
  104. package/dist/plugin/worker/html/messageHandler.js +37 -23
  105. package/dist/plugin/worker/html/messageHandler.js.map +1 -1
  106. package/dist/plugin/worker/html/plugin.d.ts.map +1 -1
  107. package/dist/plugin/worker/html/plugin.js +10 -5
  108. package/dist/plugin/worker/html/renderPages.d.ts +7 -6
  109. package/dist/plugin/worker/html/renderPages.d.ts.map +1 -1
  110. package/dist/plugin/worker/html/renderPages.js +112 -57
  111. package/dist/plugin/worker/html/renderPages.js.map +1 -1
  112. package/dist/plugin/worker/loader.d.ts +1 -11
  113. package/dist/plugin/worker/loader.d.ts.map +1 -1
  114. package/dist/plugin/worker/loader.js +2 -2
  115. package/dist/plugin/worker/loader.js.map +1 -1
  116. package/dist/plugin/worker/plugin.d.ts +10 -1
  117. package/dist/plugin/worker/plugin.d.ts.map +1 -1
  118. package/dist/plugin/worker/plugin.js +10 -1
  119. package/dist/plugin/worker/types.d.ts +17 -4
  120. package/dist/plugin/worker/types.d.ts.map +1 -1
  121. package/dist/server.d.ts +1 -3
  122. package/dist/server.d.ts.map +1 -1
  123. package/dist/server.js +1 -3
  124. package/dist/server.js.map +1 -1
  125. package/dist/tsconfig.tsbuildinfo +1 -1
  126. package/package.json +2 -4
  127. package/plugin/assertServerCondition.ts +2 -3
  128. package/plugin/checkFilesExist.ts +10 -10
  129. package/plugin/components.tsx +1 -1
  130. package/plugin/config/createModuleIdGenerator.ts +52 -0
  131. package/plugin/config/defaults.ts +24 -15
  132. package/plugin/config/resolveOptions.ts +253 -25
  133. package/plugin/config/resolvePages.ts +1 -1
  134. package/plugin/config/resolveUserConfig.ts +197 -62
  135. package/plugin/helpers/getBundleManifest.ts +75 -0
  136. package/plugin/helpers/getModuleManifest.ts +5 -0
  137. package/plugin/helpers/inputNormalizer.ts +175 -26
  138. package/plugin/helpers/inputNormalizerWorker.ts +13 -8
  139. package/plugin/helpers/tryManifest.ts +2 -0
  140. package/plugin/loader/createBuildLoader.ts +54 -10
  141. package/plugin/loader/createPageLoader.ts +1 -7
  142. package/plugin/plugin.ts +2 -1
  143. package/plugin/preserver/plugin.ts +2 -1
  144. package/plugin/react-client/index.ts +12 -1
  145. package/plugin/react-client/plugin.ts +127 -12
  146. package/plugin/react-server/createHandler.ts +7 -13
  147. package/plugin/react-server/createRscStream.ts +14 -2
  148. package/plugin/react-server/createSsrHandler.ts +7 -26
  149. package/plugin/react-server/plugin.ts +176 -144
  150. package/plugin/transformer/plugin.ts +69 -94
  151. package/plugin/transformer/transformer-client-components.ts +98 -24
  152. package/plugin/transformer/transformer-server-actions.ts +22 -7
  153. package/plugin/transformer/types.ts +4 -0
  154. package/plugin/types.ts +118 -64
  155. package/plugin/worker/html/messageHandler.ts +46 -31
  156. package/plugin/worker/html/plugin.ts +15 -11
  157. package/plugin/worker/html/renderPages.ts +175 -88
  158. package/plugin/worker/loader.ts +4 -13
  159. package/plugin/worker/plugin.ts +10 -1
  160. package/plugin/worker/types.ts +24 -3
  161. package/dist/node_modules/magic-string/dist/magic-string.es.js +0 -1283
  162. package/dist/node_modules/magic-string/dist/magic-string.es.js.map +0 -1
  163. package/dist/plugin/build/createClientBuildConfig.d.ts +0 -3
  164. package/dist/plugin/build/createClientBuildConfig.d.ts.map +0 -1
  165. package/dist/plugin/build/createClientBuildConfig.js +0 -14
  166. package/dist/plugin/build/createServerBuildConfig.d.ts +0 -12
  167. package/dist/plugin/build/createServerBuildConfig.d.ts.map +0 -1
  168. package/dist/plugin/build/createServerBuildConfig.js +0 -40
  169. package/dist/plugin/build/createSharedBuildConfig.d.ts +0 -5
  170. package/dist/plugin/build/createSharedBuildConfig.d.ts.map +0 -1
  171. package/dist/plugin/build/createSharedBuildConfig.js +0 -28
  172. package/dist/plugin/build/mergeInputs.d.ts +0 -9
  173. package/dist/plugin/build/mergeInputs.d.ts.map +0 -1
  174. package/dist/plugin/build/mergeInputs.js +0 -56
  175. package/dist/plugin/config/moduleIdDefault.d.ts +0 -8
  176. package/dist/plugin/config/moduleIdDefault.d.ts.map +0 -1
  177. package/dist/plugin/config/moduleIdDefault.js +0 -23
  178. package/dist/plugin/config/moduleIdDefault.js.map +0 -1
  179. package/dist/plugin/helpers/getModuleManifest.js.map +0 -1
  180. package/dist/plugin/react-server/createDevMiddleware.d.ts +0 -8
  181. package/dist/plugin/react-server/createDevMiddleware.d.ts.map +0 -1
  182. package/dist/plugin/react-server/createDevMiddleware.js +0 -68
  183. package/dist/plugin/react-server/createDevServer.d.ts +0 -4
  184. package/dist/plugin/react-server/createDevServer.d.ts.map +0 -1
  185. package/dist/plugin/react-server/createDevServer.js +0 -4
  186. package/dist/plugin/react-server/createReactNodeStreamer.d.ts +0 -10
  187. package/dist/plugin/react-server/createReactNodeStreamer.d.ts.map +0 -1
  188. package/dist/plugin/react-server/createReactNodeStreamer.js +0 -7
  189. package/dist/plugin/transformer/transformer-server-actions.js.map +0 -1
  190. package/plugin/build/createClientBuildConfig.ts +0 -21
  191. package/plugin/build/createServerBuildConfig.ts +0 -66
  192. package/plugin/build/createSharedBuildConfig.ts +0 -35
  193. package/plugin/build/mergeInputs.ts +0 -58
  194. package/plugin/config/moduleIdDefault.ts +0 -23
  195. package/plugin/react-server/createDevMiddleware.ts +0 -91
  196. package/plugin/react-server/createDevServer.ts +0 -9
  197. package/plugin/react-server/createReactNodeStreamer.ts +0 -26
@@ -1,8 +1,19 @@
1
- import { DEFAULT_CONFIG } from "../config/defaults.js";
2
- import type { StreamPluginOptions } from "../types.js";
1
+ import { resolveOptions } from "../config/resolveOptions.js";
2
+ import type {
3
+ ResolvedUserConfig,
4
+ ResolvedUserOptions,
5
+ StreamPluginOptions,
6
+ } from "../types.js";
3
7
  import { createClientComponentTransformer } from "./transformer-client-components.js";
4
- import { createServerActionTransformer } from "./transformer-server-actions.js";
8
+ import { createModuleIdGenerator } from "../config/createModuleIdGenerator.js";
9
+ import { type ConfigEnv, type Plugin, type ResolvedConfig } from "vite";
10
+ import { createInputNormalizer } from "../helpers/inputNormalizer.js";
11
+ import { resolveUserConfig } from "../config/resolveUserConfig.js";
5
12
 
13
+ let userOptions: ResolvedUserOptions;
14
+ let userConfig: ResolvedUserConfig;
15
+ let resolvedConfig: ResolvedConfig;
16
+ let resolvedConfigEnv: ConfigEnv;
6
17
 
7
18
  /**
8
19
  * Plugin for transforming React Client Components.
@@ -29,113 +40,77 @@ import { createServerActionTransformer } from "./transformer-server-actions.js";
29
40
  * ```
30
41
  */
31
42
 
32
- export function reactTransformPlugin(
33
- options?: StreamPluginOptions
34
- ): import("vite").Plugin {
35
- if (process.env["NODE_OPTIONS"]?.match(/--conditions[= ]react-server/)) {
36
- console.log("react-server");
37
- } else {
38
- console.log(process.env["NODE_OPTIONS"]);
39
- throw new Error(
40
- 'react-server condition not found, set NODE_OPTIONS="--conditions react-server"'
41
- );
43
+ export function reactTransformPlugin(options: StreamPluginOptions): Plugin {
44
+ const resolvedUserOptions = resolveOptions(options);
45
+ if (resolvedUserOptions.type === "error") {
46
+ throw resolvedUserOptions.error;
42
47
  }
43
- const projectRoot = options?.projectRoot || process.cwd();
44
- // const includeClient = options?.autoDiscover?.clientComponents || DEFAULT_CONFIG.AUTO_DISCOVER.clientComponents;
45
- // const includeServerFunctions = options?.autoDiscover?.serverFunctions || DEFAULT_CONFIG.AUTO_DISCOVER.serverFunctions;
46
- let transformClientComponent: any;
47
- let transformServerAction: any;
48
- // get the file we are imported from (parent)
48
+ userOptions = resolvedUserOptions.userOptions;
49
49
 
50
- // Track client components
51
- const clientComponents = new Set<string>();
50
+ let manifest: Record<string, { file: string }> = {};
51
+ const pendingEmits = new Map<string, { referenceId: string }>();
52
52
 
53
53
  return {
54
- name: "vite:react-stream-transformer",
54
+ name: "vite:react-transform",
55
55
  enforce: "post",
56
56
 
57
+ config(config, configEnv) {
58
+ const resolvedUserConfig = resolveUserConfig({
59
+ isClient: false,
60
+ config,
61
+ configEnv,
62
+ userOptions,
63
+ });
64
+ if (resolvedUserConfig.type === "error") {
65
+ throw resolvedUserConfig.error;
66
+ }
67
+ userConfig = resolvedUserConfig.userConfig;
68
+ resolvedConfigEnv = configEnv;
69
+ },
57
70
  configResolved(config) {
58
- transformClientComponent = createClientComponentTransformer({
59
- moduleId:
60
- options?.moduleId ||
61
- DEFAULT_CONFIG.MODULE_ID({
62
- projectRoot: projectRoot,
63
- output: {
64
- dir: config.build?.outDir ?? DEFAULT_CONFIG.BUILD.server,
65
- },
66
- isProduction: config.isProduction,
67
- }),
68
- }).transform;
69
- transformServerAction = createServerActionTransformer({
70
- moduleId:
71
- options?.moduleId ||
72
- DEFAULT_CONFIG.MODULE_ID({
73
- projectRoot: projectRoot,
74
- output: {
75
- dir: config.build?.outDir ?? DEFAULT_CONFIG.BUILD.server,
76
- },
77
- isProduction: config.isProduction,
78
- }),
79
- }).transform;
71
+ resolvedConfig = config;
80
72
  },
81
73
 
82
- config(config) {
83
- // Get existing inputs
84
- const existingInput = config.build?.rollupOptions?.input || {};
85
- const currentInputs =
86
- typeof existingInput === "string"
87
- ? { default: existingInput }
88
- : existingInput;
74
+ transform(code: string, id: string, opt: { ssr?: boolean } = {}) {
75
+ const hasClientDirective = code.match(/^["']use client["'];?/);
76
+ if (!hasClientDirective) return null;
89
77
 
90
- // Add client components
91
- const entries = Array.from(clientComponents).reduce(
92
- (acc, path) => ({
93
- ...acc,
94
- [path.replace(DEFAULT_CONFIG.FILE_REGEX, "")]: path,
95
- }),
96
- {}
97
- );
78
+ const moduleInfo = this.getModuleInfo(id);
79
+ if (!moduleInfo) return null;
98
80
 
99
- return {
100
- build: {
101
- rollupOptions: {
102
- input: {
103
- ...currentInputs,
104
- ...entries,
105
- },
106
- },
107
- },
108
- };
109
- },
81
+ // In dev mode, just transform the code without emitting
82
+ if (resolvedConfigEnv.command === 'serve') {
83
+ const transformer = createClientComponentTransformer(userOptions, userConfig, resolvedConfigEnv);
84
+ return transformer.transform!.bind(this)(code, id, opt);
85
+ }
110
86
 
111
- buildStart() {
112
- // Reset client components at start of each build
113
- clientComponents.clear();
114
- },
87
+ // Production build logic...
88
+ const normalizer = createInputNormalizer({
89
+ root: resolvedConfig.root,
90
+ preserveModulesRoot: undefined,
91
+ });
92
+ const [normalizedId, normalizedPath] = normalizer(id);
115
93
 
116
- async transform(code: string, id: string, options?: { ssr?: boolean }) {
117
- // Check for directives
118
- const hasClientDirective = code.match(/^["']use client["'];?/);
119
- if (!hasClientDirective) {
120
- const hasServerDirective = code.match(/^["']use server["'];?/);
121
- if (!hasServerDirective) {
122
- return null;
123
- }
124
- return transformServerAction.bind(this)(code, id, options);
125
- }
94
+ // Add to manifest immediately with a temporary path
95
+ manifest[normalizedId] = { file: normalizedPath };
96
+
97
+ const referenceId = this.emitFile({
98
+ type: "chunk",
99
+ id: normalizedPath,
100
+ name: normalizedId,
101
+ });
102
+ pendingEmits.set(normalizedId, { referenceId });
126
103
 
127
- // Track client component and transform
128
- clientComponents.add(id);
129
- console.log("[TransformerPlugin] Found client component:", id);
130
- return transformClientComponent.bind(this)(code, id, options);
104
+ const transformer = createClientComponentTransformer(userOptions, userConfig, resolvedConfigEnv);
105
+ return transformer.transform!.bind(this)(code, id, opt);
131
106
  },
132
107
 
133
- // Log final client components list
134
- buildEnd() {
135
- console.log(
136
- "[TransformerPlugin] Final client components:",
137
- Array.from(clientComponents)
138
- );
108
+ renderDynamicImport() {
109
+ // Update manifest with real file names
110
+ for (const [normalizedId, { referenceId }] of pendingEmits) {
111
+ const fileName = this.getFileName(referenceId);
112
+ manifest[normalizedId] = { file: fileName };
113
+ }
139
114
  },
140
115
  };
141
116
  }
@@ -1,33 +1,90 @@
1
- import type { Plugin as RollupPlugin } from "rollup";
1
+ import type { PluginContext } from "rollup";
2
2
  import type { TransformerOptions } from "./types.js";
3
+ import { DEFAULT_CONFIG } from "../config/defaults.js";
4
+ import { createInputNormalizer } from "../helpers/inputNormalizer.js";
5
+ import type { ResolvedUserConfig, ResolvedUserOptions } from "../types.js";
6
+ import type { ConfigEnv } from "vite";
7
+ import { join } from "path";
8
+ import { tryManifest } from "../helpers/tryManifest.js";
3
9
 
4
- export function createClientComponentTransformer({
5
- moduleId: userModuleId,
6
- }: TransformerOptions): RollupPlugin {
7
- let moduleIdFn = userModuleId;
10
+ export function createClientComponentTransformer(
11
+ {
12
+ moduleBase = DEFAULT_CONFIG.MODULE_BASE,
13
+ projectRoot,
14
+ moduleId,
15
+ build,
16
+ }: ResolvedUserOptions,
17
+ config: ResolvedUserConfig,
18
+ configEnv: ConfigEnv
19
+ ) {
20
+ let moduleIdFn = moduleId;
21
+ const normalizer = createInputNormalizer({
22
+ root: projectRoot,
23
+ preserveModulesRoot: moduleBase,
24
+ removeExtension: true,
25
+ });
26
+ const isClient = config.build.outDir?.endsWith(build.client);
27
+ const isServer = config.build.outDir?.endsWith(build.server);
28
+ const references = isServer
29
+ ? tryManifest({
30
+ root: projectRoot,
31
+ outDir: join(build.outDir, build.client),
32
+ ssrManifest: false,
33
+ }).manifest
34
+ : null;
8
35
 
9
36
  return {
10
37
  name: "vite-plugin-react-server:client-components-transformer",
11
38
 
12
- async transform(code: string, id: string, options?: { ssr?: boolean }) {
39
+ async transform(
40
+ this: PluginContext,
41
+ code: string,
42
+ id: string,
43
+ options?: { ssr?: boolean }
44
+ ) {
13
45
  try {
14
46
  // Skip node_modules and vite internal files
15
- if (id.includes('node_modules') || id.includes('vite/dist')) {
47
+ if (id.includes("node_modules") || id.includes("vite/dist")) {
16
48
  return null;
17
49
  }
18
50
 
19
51
  // Check if this is a client component from metadata or directive
52
+ let rest = id.split("/");
53
+ let fileName = rest.pop() as string;
54
+ let dir = rest.join("/");
55
+ if (dir.startsWith(projectRoot)) {
56
+ dir = dir.replace(projectRoot, "");
57
+ }
58
+ const index = dir.indexOf(moduleBase);
59
+ if (index !== -1) {
60
+ if (configEnv.command === "build") {
61
+ if (isServer) {
62
+ dir = join(build.assetsDir, dir.slice(index + moduleBase.length));
63
+ } else {
64
+ dir = new URL(
65
+ dir,
66
+ `file://${config?.server?.host ?? "localhost"}:${
67
+ config?.server?.port ?? configEnv.isPreview ? 4173 : 5173
68
+ }`
69
+ ).pathname;
70
+ }
71
+ } else if (configEnv.command === "serve") {
72
+ dir = "";
73
+ }
74
+ }
75
+ if (!fileName) fileName = id;
20
76
  const info = this?.getModuleInfo(id);
21
77
  const hasDirective = code.match(/^["']use client["'];?/);
22
- const isClientComponent = hasDirective || info?.meta?.['directives']?.includes('use client');
78
+ const isClientComponent =
79
+ hasDirective || info?.meta?.["directives"]?.includes("use client");
23
80
 
24
81
  if (!isClientComponent) {
25
- return null; // Not a client component, skip
82
+ return null; // Not a client component, skip
26
83
  }
27
84
 
28
85
  let transformedCode = code;
29
- const moduleId = moduleIdFn!(id, options?.ssr ?? false);
30
-
86
+ const moduleId = moduleIdFn!(id, configEnv.isSsrBuild ?? false);
87
+ const [key, value] = normalizer([moduleId, id]);
31
88
  // Find all named exports
32
89
  const exportMatches = Array.from(
33
90
  code.matchAll(/export\s+(?:const|let|var|function|class)\s+(\w+)/g)
@@ -40,39 +97,56 @@ export function createClientComponentTransformer({
40
97
  // Transform each export
41
98
  for (const [fullMatch, exportName] of exportMatches) {
42
99
  if (!exportName) continue;
43
-
100
+ console.log("exportName", dir, fileName, exportName);
44
101
  const isClass = fullMatch.includes("class");
45
-
46
102
  // Remove export keyword
47
103
  transformedCode = transformedCode.replace(
48
104
  fullMatch,
49
105
  fullMatch.replace("export ", "")
50
106
  );
107
+ let path = `import.meta.url`;
108
+ // path from moduleBase to value
109
+ const { manifest, error } = tryManifest({
110
+ root: projectRoot,
111
+ outDir: join(build.outDir, build.client),
112
+ ssrManifest: false,
113
+ });
114
+ if (error) {
115
+ console.error(`[RSC] Error getting manifest: ${error}`);
116
+ continue;
117
+ }
118
+ const fileFromManifest = manifest[join(moduleBase, value)]?.file;
119
+ if (fileFromManifest) {
120
+ path = JSON.stringify(fileFromManifest);
121
+ } else {
122
+ console.error(`[RSC] File not found in manifest: ${value}`);
123
+ continue;
124
+ }
51
125
 
52
- transformedCode += `
53
- const ${exportName}Ref = Object.defineProperties(
54
- ${isClass
55
- ? `class extends ${exportName} {
126
+ transformedCode += `const ${exportName}Ref = Object.defineProperties(${
127
+ isClass
128
+ ? `class extends ${exportName} {
56
129
  constructor(...args) { super(...args); }
57
130
  }`
58
- : `function(...args) { return ${exportName}.apply(null, args); }`
59
- },
131
+ : `function(...args) { return ${exportName}.apply(null, args); }`
132
+ },
60
133
  {
61
134
  $$typeof: { value: Symbol.for("react.client.reference") },
62
- $$id: { value: ${JSON.stringify(moduleId + "#" + exportName)} },
63
- $$filepath: { value: ${JSON.stringify(id)} }
135
+ $$id: { value: \`\${${path}}${"#" + exportName}\` }
64
136
  }
65
137
  );
66
138
  export { ${exportName}Ref as ${exportName} };`;
67
139
  }
68
-
69
140
  return { code: transformedCode, map: null };
70
141
  } catch (error) {
71
- console.error(`[RSC] Error transforming client component: ${id}`, error);
142
+ console.error(
143
+ `[RSC] Error transforming client component: ${id}`,
144
+ error
145
+ );
72
146
  throw error;
73
147
  }
74
148
  },
75
- };
149
+ } as const;
76
150
  }
77
151
 
78
152
  /**
@@ -1,20 +1,35 @@
1
1
  import { SourceMapGenerator } from "source-map";
2
2
  import type { TransformerOptions } from "./types.js";
3
+ import { DEFAULT_CONFIG } from "../config/defaults.js";
4
+ import type { ResolvedUserOptions, ResolvedUserConfig } from "../types.js";
5
+ import type { ConfigEnv } from "vite";
6
+ import { createInputNormalizer } from "../helpers/inputNormalizer.js";
3
7
 
4
- export function createServerActionTransformer(options: TransformerOptions) {
8
+ export function createServerActionTransformer({
9
+ moduleBase = DEFAULT_CONFIG.MODULE_BASE,
10
+ projectRoot,
11
+ moduleId,
12
+ }: ResolvedUserOptions, config: ResolvedUserConfig, configEnv: ConfigEnv) {
13
+ let moduleIdFn = moduleId;
14
+ const normalizer = createInputNormalizer({
15
+ root: projectRoot,
16
+ preserveModulesRoot: moduleBase,
17
+ removeExtension: DEFAULT_CONFIG.FILE_REGEX,
18
+ });
5
19
  return {
6
20
  name: "vite-plugin-react-server:server-actions-transformer",
7
- enforce: "post" as const,
8
21
 
9
22
  async transform(
10
23
  code: string,
11
24
  path: string,
12
- { ssr }: { ssr: boolean } = { ssr: false }
13
- ) {
25
+ { ssr }: { ssr: boolean } = { ssr: false }
26
+ ) {
14
27
  try {
15
28
  let transformedCode = code;
16
29
  const directiveMatch = code.match(/^["']use server["'];?/);
17
- const moduleId = options.moduleId(path, ssr);
30
+ const moduleId = moduleIdFn(path, ssr);
31
+ const [key,value] = normalizer(moduleId);
32
+
18
33
 
19
34
  // Log the path transformation
20
35
  console.log("[RSC Transform] Module path transformation:", {
@@ -66,8 +81,8 @@ const ${exportName}Ref = Object.defineProperties(
66
81
  },
67
82
  {
68
83
  $$typeof: { value: Symbol.for("react.server.reference") },
69
- $$id: { value: ${JSON.stringify(moduleId + "#" + exportName)} },
70
- $$filepath: { value: ${JSON.stringify(path)} },
84
+ $$src: { value: ${JSON.stringify(value)} },
85
+ $$id: { value: import.meta.url.slice(import.meta.url.indexOf("/${key}")}) + ${JSON.stringify("#" + exportName)} },
71
86
  $$async: { value: true }
72
87
  }
73
88
  );
@@ -12,4 +12,8 @@ export interface TransformerOptions {
12
12
  * Optional validation function for module IDs
13
13
  */
14
14
  validateModuleId?: (moduleId: string) => boolean;
15
+ /**
16
+ * The directory to use for the module IDs
17
+ */
18
+ moduleBase: string;
15
19
  }