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,16 +1,235 @@
1
+ import type { PreRenderedAsset } from "rollup";
2
+ import type { PreRenderedChunk } from "rollup";
1
3
  import type { StreamPluginOptions, ResolvedUserOptions } from "../types.js";
2
4
  import { DEFAULT_CONFIG } from "./defaults.js";
5
+ import { join } from "path";
6
+ import { createModuleIdGenerator } from "./createModuleIdGenerator.js";
7
+
8
+ const resolveAutoDiscoverMatcher = (
9
+ options: undefined | string | RegExp | ((path: string) => boolean),
10
+ fallback: RegExp | ((path: string) => boolean)
11
+ ) => {
12
+ if (!options) {
13
+ if (typeof fallback === "function") {
14
+ return fallback;
15
+ } else {
16
+ return (path: string) => fallback.test(path);
17
+ }
18
+ }
19
+ if (typeof options === "string") {
20
+ const matcher = new RegExp(options);
21
+ return (path: string) => matcher.test(path);
22
+ } else if (typeof options === "function") {
23
+ return options;
24
+ } else {
25
+ return (path: string) => options.test(path);
26
+ }
27
+ };
3
28
 
4
29
  export const resolveOptions = (
5
- options: StreamPluginOptions
30
+ options: StreamPluginOptions,
31
+ isClient: boolean = false
6
32
  ):
7
33
  | { type: "success"; userOptions: ResolvedUserOptions }
8
34
  | { type: "error"; error: Error } => {
9
35
  const projectRoot = options.projectRoot ?? process.cwd();
36
+ const { pageExportName = DEFAULT_CONFIG.PAGE_EXPORT, propsExportName = DEFAULT_CONFIG.PROPS_EXPORT } = options;
37
+ const pages =
38
+ typeof options.build?.pages === "function"
39
+ ? options.build.pages
40
+ : Array.isArray(options.build?.pages)
41
+ ? options.build.pages
42
+ : DEFAULT_CONFIG.BUILD.pages;
43
+ let client = options.build?.client ?? DEFAULT_CONFIG.BUILD.client;
44
+ let server = options.build?.server ?? DEFAULT_CONFIG.BUILD.server;
45
+
46
+ const api = options.build?.api ?? DEFAULT_CONFIG.BUILD.api;
47
+ const staticBuild = options.build?.static ?? DEFAULT_CONFIG.BUILD.static;
48
+ const outDir = options.build?.outDir ?? DEFAULT_CONFIG.BUILD.outDir;
49
+ const assetsDir =
50
+ options.build?.assetsDir ??
51
+ `${DEFAULT_CONFIG.CLIENT_ASSETS_DIR}`;
52
+
53
+ const ensureModuleBase = (n: string | null) => {
54
+ if(!n) return '';
55
+ return n.startsWith(moduleBase + "/") ? n.slice(moduleBase.length + 1) : n;
56
+ }
57
+
58
+ const testModulePattern = resolveAutoDiscoverMatcher(
59
+ options.autoDiscover?.modulePattern,
60
+ DEFAULT_CONFIG.AUTO_DISCOVER.modulePattern
61
+ );
62
+
63
+ const testJson = resolveAutoDiscoverMatcher(
64
+ options.autoDiscover?.jsonPattern,
65
+ DEFAULT_CONFIG.AUTO_DISCOVER.jsonPattern
66
+ );
67
+
68
+ const testCss = resolveAutoDiscoverMatcher(
69
+ options.autoDiscover?.cssPattern,
70
+ DEFAULT_CONFIG.AUTO_DISCOVER.cssPattern
71
+ );
72
+ const testHtml = resolveAutoDiscoverMatcher(
73
+ options.autoDiscover?.htmlPattern,
74
+ DEFAULT_CONFIG.AUTO_DISCOVER.htmlPattern
75
+ );
76
+ const testClientComponents = resolveAutoDiscoverMatcher(
77
+ options.autoDiscover?.clientComponents,
78
+ DEFAULT_CONFIG.AUTO_DISCOVER.clientComponents
79
+ );
80
+ const testServerFunctions = resolveAutoDiscoverMatcher(
81
+ options.autoDiscover?.serverFunctions,
82
+ DEFAULT_CONFIG.AUTO_DISCOVER.serverFunctions
83
+ );
84
+ const testPropsPattern = resolveAutoDiscoverMatcher(
85
+ options.autoDiscover?.propsPattern,
86
+ DEFAULT_CONFIG.AUTO_DISCOVER.propsPattern
87
+ );
88
+ const testPagePattern = resolveAutoDiscoverMatcher(
89
+ options.autoDiscover?.pagePattern,
90
+ DEFAULT_CONFIG.AUTO_DISCOVER.pagePattern
91
+ );
92
+ const testCssModule = resolveAutoDiscoverMatcher(
93
+ options.autoDiscover?.cssModulePattern,
94
+ DEFAULT_CONFIG.AUTO_DISCOVER.cssModulePattern
95
+ );
96
+ const testVendor = resolveAutoDiscoverMatcher(
97
+ options.autoDiscover?.vendorPattern,
98
+ DEFAULT_CONFIG.AUTO_DISCOVER.vendorPattern
99
+ );
100
+ const preserveModulesRoot = options.build?.preserveModulesRoot ?? DEFAULT_CONFIG.BUILD.preserveModulesRoot;
101
+ const hashOption = typeof options.build?.hash === "string" ? options.build.hash : DEFAULT_CONFIG.BUILD.hash
102
+ const hashString = hashOption === '' ? '' : `-[${hashOption}]`;
103
+ const hash = (n: string | null) => {
104
+ if(!n) return '';
105
+ if(hashString === '' || (!isClient && !testClientComponents(n))) return n;
106
+ const extensionIndex = n.lastIndexOf('.')
107
+ if(extensionIndex !== -1 && !testClientComponents(n)) {
108
+ // put hash between extension and filename
109
+ const extension = n.slice(extensionIndex)
110
+ const filename = n.slice(0, extensionIndex)
111
+ return filename + hashString + extension;
112
+ } else {
113
+ return n + hashString;
114
+ }
115
+ }
116
+
117
+ const getOutputPath = (n: string | null) => {
118
+ if(!n) return '';
119
+ // Remove src/ prefix if present
120
+ const path = n.startsWith(moduleBase + "/")
121
+ ? n.slice(moduleBase.length + 1)
122
+ : n;
10
123
 
11
- const build = options.build ?? DEFAULT_CONFIG.BUILD;
124
+ if (testClientComponents(path)) {
125
+ return `${assetsDir}/${path}`;
126
+ }
127
+ if (testCss(path)) {
128
+ return `${path}`;
129
+ }
130
+ if(testHtml(path)) {
131
+ if(options.autoDiscover?.htmlPattern && typeof options.autoDiscover.htmlPattern !== "function") {
132
+ // if it's not a function, use it as an option for replace
133
+ return `${path.replace(options.autoDiscover.htmlPattern, '.js')}`;
134
+ } else {
135
+ return `${path.replace('.html', '')}`;
136
+ }
137
+ }
138
+ if(testJson(path)) {
139
+ if(options.autoDiscover?.jsonPattern && typeof options.autoDiscover.jsonPattern !== "function") {
140
+ // if it's not a function, use it as an option for replace
141
+ return `${path.replace(options.autoDiscover.jsonPattern, '.js')}`;
142
+ } else {
143
+ return `${path}`;
144
+ }
145
+ }
146
+ if (testPropsPattern(path)) {
147
+ return `${path}`;
148
+ }
149
+ if (testPagePattern(path)) {
150
+ return `${path}`;
151
+ }
152
+ if (testServerFunctions(path)) {
153
+ return `${api}/${path}`; //
154
+ }
155
+ if(testCssModule(path)) {
156
+ if(options.autoDiscover?.cssModulePattern && typeof options.autoDiscover.cssModulePattern !== "function") {
157
+ // if it's not a function, use it as an option for replace
158
+ return `${path.replace(options.autoDiscover.cssModulePattern, '.js')}`;
159
+ } else {
160
+ return `${path.replace('.css.js', '.js')}`;
161
+ }
162
+ }
163
+ if(testVendor(path)) {
164
+ return `vendor`;
165
+ }
166
+ return `${path}`;
167
+ };
12
168
 
13
- const moduleBase = typeof options.moduleBase === "string" ? options.moduleBase : DEFAULT_CONFIG.MODULE_BASE;
169
+ const entryFile = (n: PreRenderedChunk) => {
170
+ if(testCss(n.name)) {
171
+ // this is the css.js chunk for ssr, which (if we keep the .css) would go into client, this prevents that.
172
+ const result = `${hash(getOutputPath(ensureModuleBase(n.name + '.js')))}`;
173
+ return result;
174
+ }
175
+ const result = `${hash(getOutputPath(ensureModuleBase(n.name)))}.js`;
176
+ return result;
177
+ }
178
+
179
+ const chunkFile = (n: PreRenderedChunk) => {
180
+ const result = `${hash(getOutputPath(ensureModuleBase(n.name)))}.js`;
181
+ return result;
182
+ }
183
+
184
+ const assetFile = (n: PreRenderedAsset) => {
185
+ const result = `${hash(getOutputPath(ensureModuleBase(n.names[0])))}`;
186
+ return result;
187
+ }
188
+
189
+ const build =
190
+ typeof options.build === "object" && options.build !== null
191
+ ? {
192
+ pages,
193
+ client,
194
+ server,
195
+ static: staticBuild,
196
+ outDir,
197
+ assetsDir,
198
+ api,
199
+ hash: hashOption,
200
+ preserveModulesRoot,
201
+ entryFile:
202
+ typeof options.build?.entryFile === "function"
203
+ ? options.build.entryFile
204
+ : entryFile,
205
+ chunkFile:
206
+ typeof options.build?.chunkFile === "function"
207
+ ? options.build.chunkFile
208
+ : chunkFile,
209
+ assetFile:
210
+ typeof options.build?.assetFile === "function"
211
+ ? options.build.assetFile
212
+ : assetFile,
213
+ }
214
+ : {
215
+ pages,
216
+ client,
217
+ server,
218
+ static: staticBuild,
219
+ outDir,
220
+ assetsDir,
221
+ api,
222
+ hash: hashOption,
223
+ preserveModulesRoot,
224
+ entryFile,
225
+ chunkFile,
226
+ assetFile,
227
+ };
228
+
229
+ const moduleBase =
230
+ typeof options.moduleBase === "string"
231
+ ? options.moduleBase
232
+ : DEFAULT_CONFIG.MODULE_BASE;
14
233
  const moduleBasePath =
15
234
  typeof options.moduleBasePath === "string"
16
235
  ? options.moduleBasePath
@@ -21,28 +240,47 @@ export const resolveOptions = (
21
240
  typeof options.moduleBaseURL === "string"
22
241
  ? options.moduleBaseURL
23
242
  : moduleBasePath ?? DEFAULT_CONFIG.MODULE_BASE_URL;
243
+
244
+ const autoDiscover = {
245
+ modulePattern: testModulePattern,
246
+ cssPattern: testCss,
247
+ jsonPattern: testJson,
248
+ clientComponents: testClientComponents,
249
+ serverFunctions: testServerFunctions,
250
+ propsPattern: testPropsPattern,
251
+ pagePattern: testPagePattern,
252
+ cssModulePattern: testCssModule,
253
+ vendorPattern: testVendor,
254
+ };
255
+
256
+ const moduleId = typeof options.moduleId === "function" ? options.moduleId : createModuleIdGenerator({
257
+ isProduction: process.env['NODE_ENV'] === "production",
258
+ inputRoot: projectRoot,
259
+ client: client,
260
+ server: server,
261
+ moduleBase: moduleBase,
262
+ preserveModulesRoot: preserveModulesRoot,
263
+ removeExtension: DEFAULT_CONFIG.FILE_REGEX,
264
+ imports: {},
265
+ });
266
+
24
267
  try {
25
268
  return {
26
269
  type: "success",
27
270
  userOptions: {
28
271
  projectRoot,
272
+ moduleId,
29
273
  moduleBase,
30
274
  moduleBasePath,
31
275
  moduleBaseURL,
32
- build: {
33
- pages: build.pages ?? DEFAULT_CONFIG.BUILD.pages,
34
- client: build.client ?? DEFAULT_CONFIG.BUILD.client,
35
- server: build.server ?? DEFAULT_CONFIG.BUILD.server,
36
- static: build.static ?? DEFAULT_CONFIG.BUILD.static,
37
- },
276
+ build: build,
38
277
  Page: options.Page ?? DEFAULT_CONFIG.PAGE,
39
278
  props: options.props ?? DEFAULT_CONFIG.PROPS,
40
279
  Html: options.Html ?? DEFAULT_CONFIG.HTML,
41
- pageExportName: options.pageExportName ?? DEFAULT_CONFIG.PAGE_EXPORT,
42
- propsExportName: options.propsExportName ?? DEFAULT_CONFIG.PROPS_EXPORT,
280
+ pageExportName: pageExportName,
281
+ propsExportName: propsExportName,
43
282
  collectCss: options.collectCss ?? DEFAULT_CONFIG.COLLECT_CSS,
44
283
  collectAssets: options.collectAssets ?? DEFAULT_CONFIG.COLLECT_ASSETS,
45
- assetsDir: options.assetsDir ?? DEFAULT_CONFIG.CLIENT_ASSETS_DIR,
46
284
  htmlWorkerPath:
47
285
  options.htmlWorkerPath ?? DEFAULT_CONFIG.HTML_WORKER_PATH,
48
286
  rscWorkerPath: options.rscWorkerPath ?? DEFAULT_CONFIG.RSC_WORKER_PATH,
@@ -50,19 +288,9 @@ export const resolveOptions = (
50
288
  clientEntry: options.clientEntry ?? DEFAULT_CONFIG.CLIENT_ENTRY,
51
289
  serverEntry: options.serverEntry ?? DEFAULT_CONFIG.SERVER_ENTRY,
52
290
  moduleBaseExceptions: options.moduleBaseExceptions ?? [],
53
- autoDiscover: {
54
- pagePattern:
55
- options.autoDiscover?.pagePattern ??
56
- DEFAULT_CONFIG.AUTO_DISCOVER.pagePattern,
57
- propsPattern:
58
- options.autoDiscover?.propsPattern ??
59
- DEFAULT_CONFIG.AUTO_DISCOVER.propsPattern,
60
- clientComponents:
61
- options.autoDiscover?.clientComponents ??
62
- DEFAULT_CONFIG.AUTO_DISCOVER.clientComponents,
63
- serverFunctions:
64
- options.autoDiscover?.serverFunctions ??
65
- DEFAULT_CONFIG.AUTO_DISCOVER.serverFunctions,
291
+ autoDiscover: autoDiscover,
292
+ pipableStreamOptions: options.pipableStreamOptions ?? {
293
+ bootstrapModules: [options.clientEntry ?? DEFAULT_CONFIG.CLIENT_ENTRY],
66
294
  },
67
295
  },
68
296
  };
@@ -2,7 +2,7 @@ import type { ResolvedUserOptions } from '../types.js';
2
2
 
3
3
  export async function resolvePages(
4
4
  pages: ResolvedUserOptions["build"]["pages"]
5
- ): Promise<{ type: "success"; pages: string[] } | { type: "error"; error: Error }> {
5
+ ): Promise<{ type: "success"; error?:never; pages: string[] } | { type: "error"; error: Error; pages?:never }> {
6
6
  if (!pages) {
7
7
  return { type: "success", pages: [] };
8
8
  }
@@ -1,96 +1,231 @@
1
1
  import type { ConfigEnv, UserConfig } from "vite";
2
- import type { CheckFilesExistReturn, ResolvedUserConfig, ResolvedUserOptions } from "../types.js";
3
- import { DEFAULT_CONFIG } from "./defaults.js";
2
+ import type {
3
+ CheckFilesExistReturn,
4
+ ResolvedUserConfig,
5
+ ResolvedUserOptions,
6
+ } from "../types.js";
4
7
  import { createInputNormalizer } from "../helpers/inputNormalizer.js";
5
-
8
+ import { join } from "path";
9
+ // @ts-ignore
10
+ import { globSync } from "fs";
11
+ import type { OutputOptions } from "rollup";
6
12
  export type ResolveUserConfigProps = {
7
- condition: "react-client" | "react-server" | "";
13
+ isClient?: boolean;
8
14
  config: UserConfig;
9
15
  configEnv: ConfigEnv;
10
16
  userOptions: ResolvedUserOptions;
11
17
  files?: CheckFilesExistReturn;
12
18
  };
13
19
 
14
- export type ResolveUserConfigReturn =
20
+ export type ResolveUserConfigReturn =
15
21
  | { type: "success"; userConfig: ResolvedUserConfig }
16
22
  | { type: "error"; error: Error };
17
23
 
18
24
  export function resolveUserConfig({
19
- condition,
25
+ isClient = false,
20
26
  config,
21
27
  configEnv,
22
28
  userOptions,
23
- files
29
+ files,
24
30
  }: ResolveUserConfigProps): ResolveUserConfigReturn {
25
-
26
-
27
31
  try {
28
32
  // Get existing inputs
29
33
  const root = config.root ?? userOptions.projectRoot ?? process.cwd();
30
- const existingInput = config.build?.rollupOptions?.input || {};
31
- const currentInputs = typeof existingInput === 'string' ? { default: existingInput } : existingInput;
32
- const normalizer = createInputNormalizer(root);
33
34
 
34
- const serverEntry = userOptions.serverEntry ? [userOptions.serverEntry, userOptions.serverEntry] : [];
35
- // Add inputs based on condition
36
- const inputs = {
37
- ...currentInputs,
38
- ...(condition === 'react-server' && files ? {
39
- 'index.html': '/index.html',
40
- ...Object.fromEntries([
41
- ...serverEntry,
42
- ...Array.from(files.pageMap.entries()),
43
- ...Array.from(files.propsMap.entries())
44
- ].map(normalizer))
45
- } : {
46
- client: userOptions.clientEntry
47
- })
48
- };
35
+ const normalizer = createInputNormalizer({
36
+ root,
37
+ preserveModulesRoot: undefined,
38
+ });
49
39
 
50
- const userConfig = {
51
- ...config,
52
- root: root,
53
- mode: configEnv.command === 'build' ? 'production' : 'development',
54
- build: {
55
- ...config.build,
56
- outDir: condition === 'react-server' ? userOptions.build.server : userOptions.build.client,
57
- assetsDir: condition === 'react-server' ? "" : DEFAULT_CONFIG.CLIENT_ASSETS_DIR,
58
- ssr: condition === 'react-server',
59
- target: condition === 'react-server' ? 'node18' : 'es2020',
60
- minify: condition === 'react-server' ? false : true,
61
- manifest: true,
62
- ssrManifest: false,
63
- ssrEmitAssets: true,
64
- rollupOptions: {
65
- ...config.build?.rollupOptions,
66
- input: inputs,
67
- preserveEntrySignatures: 'strict',
68
- output: condition === 'react-server' ? {
69
- preserveModules: true,
70
- entryFileNames: '[name].js',
71
- assetFileNames: '[name].[ext]',
72
- chunkFileNames: '[name].[ext]',
73
- format: 'esm',
74
- exports: 'named',
75
- hoistTransitiveImports: false,
76
- generatedCode: {
77
- constBindings: true,
78
- objectShorthand: true
79
- },
80
- interop: 'auto'
81
- } : undefined
40
+ const serverEntry = userOptions.serverEntry
41
+ ? Object.fromEntries([
42
+ normalizer([userOptions.serverEntry, userOptions.serverEntry]),
43
+ ])
44
+ : null;
45
+ const clientEntry = userOptions.clientEntry
46
+ ? Object.fromEntries(
47
+ [
48
+ [userOptions.clientEntry, userOptions.clientEntry],
49
+ ["index.html", "index.html"],
50
+ ].map(normalizer)
51
+ )
52
+ : { "index.html": "index.html" };
53
+
54
+ const autoDiscoveredClientFiles = (inputs: Record<string, string>) => {
55
+ const allFiles = globSync(`**/*.client.*`, {
56
+ cwd: join(root, userOptions.moduleBase),
57
+ });
58
+ for (const [key, value] of Object.entries(allFiles).map(([n, path]) => {
59
+ if (typeof path !== "string") {
60
+ throw new Error(`Invalid path: ${JSON.stringify(path)}`);
61
+ }
62
+ const [key, value] = normalizer(join(userOptions.moduleBase, path));
63
+ return [key, value];
64
+ })) {
65
+ // check if not already in the inputs
66
+ if (!inputs[key]) {
67
+ inputs[key] = value;
68
+ }
69
+ }
70
+ return inputs;
71
+ };
72
+ const autoDiscoveredServerFiles = (inputs: Record<string, string>) => {
73
+ const allFiles = globSync(`${userOptions.moduleBase}/**/*.server.*`, {
74
+ cwd: join(root, userOptions.moduleBase),
75
+ });
76
+ for (const [key, value] of Object.entries(allFiles).map(normalizer)) {
77
+ if (!inputs[key]) {
78
+ inputs[key] = value;
82
79
  }
83
80
  }
81
+ return inputs;
82
+ };
83
+ const autoDiscoveredFiles = (inputs: Record<string, string>) => {
84
+ if(!files) {
85
+ return inputs;
86
+ }
87
+ for (const [key, value] of Object.entries( Array.from(files.pageMap.entries())).map(normalizer)) {
88
+ if (!inputs[key]) {
89
+ inputs[key] = value;
90
+ }
91
+ }
92
+ for (const [key, value] of Object.entries( Array.from(files.propsMap.entries())).map(normalizer)) {
93
+ if (!inputs[key]) {
94
+ inputs[key] = value;
95
+ }
96
+ }
97
+ return inputs;
84
98
  };
85
99
 
100
+ // Add inputs based on condition
101
+ let inputs = isClient
102
+ ? autoDiscoveredClientFiles(clientEntry)
103
+ : autoDiscoveredServerFiles(autoDiscoveredFiles(serverEntry ?? {}));
104
+
105
+ if (userOptions.build.preserveModulesRoot) {
106
+ inputs = Object.fromEntries(
107
+ Object.entries(inputs).map(([key, value]) => {
108
+ if (key.startsWith(userOptions.moduleBase + "/")) {
109
+ return [key.slice(userOptions.moduleBase.length + 1), value];
110
+ }
111
+ return [key, value];
112
+ })
113
+ );
114
+ }
115
+
116
+ const envDir = isClient
117
+ ? userOptions.build.client
118
+ : userOptions.build.server;
119
+ if (isClient) {
120
+ return {
121
+ type: "success",
122
+ userConfig: {
123
+ ...config,
124
+ root: root,
125
+ mode: configEnv.command === "build" ? "production" : "development",
126
+ resolve: {
127
+ external: ["react", "react-dom"],
128
+ alias: {
129
+ [userOptions.moduleBasePath]: join(root, userOptions.moduleBasePath),
130
+ [userOptions.moduleBase]: join(root, userOptions.moduleBase),
131
+ ['index.html']: join(root, 'index.html')
132
+ }
133
+ },
134
+ ssr: {
135
+ target: "node",
136
+ external: ["react", "react-dom", "react-server-dom-esm/client.browser"],
137
+ resolve: {
138
+ conditions: ["react-server"],
139
+ externalConditions: ["react-server"],
140
+ },
141
+ },
142
+ build: {
143
+ ...config.build,
144
+ emptyOutDir: config.build?.emptyOutDir ?? true,
145
+ outDir: join(userOptions.build.outDir, envDir),
146
+ assetsDir: userOptions.build.assetsDir,
147
+ // modern browsers
148
+ target: ["esnext"],
149
+ minify: true,
150
+ ssr: true,
151
+ manifest: config.build?.manifest ?? `.vite/manifest.json`,
152
+ ssrManifest: config.build?.ssrManifest ?? `.vite/ssr-manifest.json`,
153
+ ssrEmitAssets: true,
154
+ rollupOptions: {
155
+ ...config.build?.rollupOptions,
156
+ input: inputs,
157
+ output: {
158
+ ...config.build?.rollupOptions?.output,
159
+ preserveModulesRoot: userOptions.build.preserveModulesRoot
160
+ ? userOptions.moduleBase
161
+ : undefined,
162
+ },
163
+ },
164
+ },
165
+ },
166
+ };
167
+ }
168
+ const mergeOutputArray = (Array.isArray(config.build?.rollupOptions?.output))
169
+ const pluginOutput = {
170
+ preserveModules: true,
171
+ preserveModulesRoot: userOptions.build.preserveModulesRoot
172
+ ? userOptions.moduleBase
173
+ : undefined,
174
+ entryFileNames: userOptions.build.entryFile,
175
+ assetFileNames: userOptions.build.assetFile,
176
+ chunkFileNames: userOptions.build.chunkFile,
177
+ format: "esm",
178
+ exports: "named",
179
+ hoistTransitiveImports: false,
180
+ generatedCode: {
181
+ constBindings: true,
182
+ objectShorthand: true,
183
+ },
184
+ interop: "auto",
185
+ } satisfies OutputOptions;
186
+ const newOutput = Array.isArray(config.build?.rollupOptions?.output) ?
187
+ [...config.build?.rollupOptions?.output, pluginOutput]
188
+ : typeof config.build?.rollupOptions?.output === 'object' && config.build?.rollupOptions?.output !== null ?
189
+ [config.build?.rollupOptions?.output, pluginOutput]
190
+ : pluginOutput
86
191
  return {
87
192
  type: "success",
88
- userConfig: userConfig as ResolvedUserConfig
193
+ userConfig: {
194
+ ...config,
195
+ root: root,
196
+ mode: configEnv.command === "build" ? "production" : "development",
197
+ resolve: {
198
+ alias: {
199
+ [userOptions.moduleBasePath]: join(root, userOptions.moduleBasePath),
200
+ [userOptions.moduleBase]: join(root, userOptions.moduleBase),
201
+ ['index.html']: join(root, 'index.html')
202
+ }
203
+ },
204
+ build: {
205
+ ...config.build,
206
+ emptyOutDir: config.build?.emptyOutDir ?? true,
207
+ outDir: join(userOptions.build.outDir, envDir),
208
+ target: "node18",
209
+ minify: true,
210
+ ssr: true,
211
+ manifest: config.build?.manifest ?? `.vite/manifest.json`,
212
+ ssrManifest: config.build?.ssrManifest ?? `.vite/ssr-manifest.json`,
213
+ ssrEmitAssets: true,
214
+ assetsDir: config.build?.assetsDir ?? userOptions.build.assetsDir,
215
+ rollupOptions: {
216
+ ...config.build?.rollupOptions,
217
+ input: inputs,
218
+ preserveEntrySignatures: "strict",
219
+ output: newOutput,
220
+ },
221
+ },
222
+ },
89
223
  };
90
224
  } catch (error) {
91
225
  return {
92
226
  type: "error",
93
- error: error instanceof Error ? error : new Error("Failed to resolve config")
227
+ error:
228
+ error instanceof Error ? error : new Error("Failed to resolve config"),
94
229
  };
95
230
  }
96
- }
231
+ }
@@ -0,0 +1,75 @@
1
+ import type {
2
+ NormalizedOutputOptions,
3
+ OutputBundle,
4
+ PluginContext,
5
+ } from "rollup";
6
+ import { createInputNormalizer } from "./inputNormalizer.js";
7
+ import { join } from "path";
8
+
9
+ interface BundleManifestEntry {
10
+ file: string;
11
+ name: string;
12
+ src?: string;
13
+ isEntry?: boolean;
14
+ imports?: string[];
15
+ dynamicImports?: string[];
16
+ css?: string[];
17
+ }
18
+
19
+ /**
20
+ * Get the bundle manifest from the plugin context. Will only work during production build
21
+ * @param pluginContext - The plugin context
22
+ * @param bundle - The bundle
23
+ * @param preserveModulesRoot - The preserve modules root
24
+ * @returns The bundle manifest
25
+ */
26
+ export function getBundleManifest(
27
+ pluginContext: PluginContext,
28
+ bundle: OutputBundle,
29
+ preserveModulesRoot: string | undefined
30
+ ): Record<string, BundleManifestEntry> {
31
+
32
+ const normalizer = createInputNormalizer({
33
+ root: pluginContext.environment.config.root,
34
+ removeExtension: false,
35
+ preserveModulesRoot:
36
+ typeof preserveModulesRoot === "string" ? preserveModulesRoot : undefined,
37
+ });
38
+
39
+ if (!bundle) {
40
+ return {};
41
+ }
42
+
43
+ return Object.fromEntries(
44
+ Object.entries(bundle)
45
+ .map(([fileName, chunk]) => {
46
+ if (chunk.type !== "chunk") return null as never;
47
+
48
+ // Normalize both the module ID and file path
49
+ const [moduleId, sourcePath] = normalizer(
50
+ chunk.facadeModuleId || chunk.moduleIds[0] || fileName
51
+ );
52
+ return [
53
+ moduleId,
54
+ {
55
+ file: join( pluginContext.environment.config.build.outDir, fileName),
56
+ name: moduleId,
57
+ src: sourcePath.startsWith(pluginContext.environment.config.root) ? sourcePath.slice(pluginContext.environment.config.root.length + 1) : sourcePath,
58
+ isEntry: chunk.isEntry,
59
+ ...(Object.keys(chunk.imports).length > 0
60
+ ? { imports: chunk.imports }
61
+ : {}),
62
+ ...(Object.keys(chunk.dynamicImports).length > 0
63
+ ? { dynamicImports: chunk.dynamicImports }
64
+ : {}),
65
+ ...(chunk.viteMetadata?.importedCss
66
+ ? {
67
+ css: Array.from(chunk.viteMetadata.importedCss),
68
+ }
69
+ : {}),
70
+ },
71
+ ];
72
+ })
73
+ .filter(Boolean)
74
+ );
75
+ }