vite-plugin-react-server 1.1.17 → 1.1.18

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 (124) hide show
  1. package/README.md +2 -2
  2. package/dist/package.json +5 -4
  3. package/dist/plugin/components/css-collector-elements.d.ts.map +1 -1
  4. package/dist/plugin/components/html.d.ts.map +1 -1
  5. package/dist/plugin/config/autoDiscover/createGlobAutoDiscover.d.ts.map +1 -1
  6. package/dist/plugin/config/autoDiscover/customWorkerFiles.d.ts.map +1 -1
  7. package/dist/plugin/config/autoDiscover/pageAndPropFiles.d.ts.map +1 -1
  8. package/dist/plugin/config/autoDiscover/resolveAutoDiscover.d.ts.map +1 -1
  9. package/dist/plugin/config/autoDiscover/resolveAutoDiscover.js +7 -12
  10. package/dist/plugin/config/autoDiscover/resolveAutoDiscover.js.map +1 -1
  11. package/dist/plugin/config/autoDiscover/resolveBuildPages.js +2 -2
  12. package/dist/plugin/config/autoDiscover/resolveBuildPages.js.map +1 -1
  13. package/dist/plugin/config/createModuleID.d.ts.map +1 -1
  14. package/dist/plugin/config/defaults.js +16 -2
  15. package/dist/plugin/config/defaults.js.map +1 -1
  16. package/dist/plugin/config/extMap.d.ts.map +1 -1
  17. package/dist/plugin/config/getCondition.d.ts.map +1 -1
  18. package/dist/plugin/config/getPaths.d.ts.map +1 -1
  19. package/dist/plugin/config/mimeTypes.d.ts.map +1 -1
  20. package/dist/plugin/config/mimeTypes.js +67 -0
  21. package/dist/plugin/config/mimeTypes.js.map +1 -1
  22. package/dist/plugin/config/resolveAutoDiscoverMatcher.d.ts.map +1 -1
  23. package/dist/plugin/config/resolveEnv.d.ts.map +1 -1
  24. package/dist/plugin/config/resolveOptions.d.ts.map +1 -1
  25. package/dist/plugin/config/resolveOptions.js +1 -1
  26. package/dist/plugin/config/resolveOptions.js.map +1 -1
  27. package/dist/plugin/config/resolveUserConfig.d.ts.map +1 -1
  28. package/dist/plugin/config/resolveUserConfig.js +39 -22
  29. package/dist/plugin/config/resolveUserConfig.js.map +1 -1
  30. package/dist/plugin/env/plugin.d.ts.map +1 -1
  31. package/dist/plugin/env/plugin.js +35 -2
  32. package/dist/plugin/env.js +37 -3
  33. package/dist/plugin/env.js.map +1 -1
  34. package/dist/plugin/error/toError.js +4 -4
  35. package/dist/plugin/error/toError.js.map +1 -1
  36. package/dist/plugin/helpers/cleanObject.js +2 -2
  37. package/dist/plugin/helpers/cleanObject.js.map +1 -1
  38. package/dist/plugin/helpers/collectManifestCss.js +2 -2
  39. package/dist/plugin/helpers/collectManifestCss.js.map +1 -1
  40. package/dist/plugin/helpers/collectViteModuleGraphCss.d.ts +1 -1
  41. package/dist/plugin/helpers/collectViteModuleGraphCss.d.ts.map +1 -1
  42. package/dist/plugin/helpers/collectViteModuleGraphCss.js +3 -1
  43. package/dist/plugin/helpers/collectViteModuleGraphCss.js.map +1 -1
  44. package/dist/plugin/helpers/createCssProps.d.ts +1 -1
  45. package/dist/plugin/helpers/createCssProps.d.ts.map +1 -1
  46. package/dist/plugin/helpers/createCssProps.js +12 -1
  47. package/dist/plugin/helpers/createCssProps.js.map +1 -1
  48. package/dist/plugin/helpers/createEventHandler.d.ts.map +1 -1
  49. package/dist/plugin/helpers/createEventHandler.js +1 -1
  50. package/dist/plugin/helpers/getBundleManifest.js +1 -1
  51. package/dist/plugin/helpers/getRouteFiles.d.ts.map +1 -1
  52. package/dist/plugin/helpers/getRouteFiles.js +2 -2
  53. package/dist/plugin/helpers/getRouteFiles.js.map +1 -1
  54. package/dist/plugin/helpers/inputNormalizer.js +1 -1
  55. package/dist/plugin/helpers/inputNormalizer.js.map +1 -1
  56. package/dist/plugin/helpers/requestInfo.js +1 -1
  57. package/dist/plugin/helpers/requestInfo.js.map +1 -1
  58. package/dist/plugin/helpers/resolvePage.d.ts.map +1 -1
  59. package/dist/plugin/helpers/resolveProps.d.ts.map +1 -1
  60. package/dist/plugin/helpers/serializeUserOptions.d.ts.map +1 -1
  61. package/dist/plugin/helpers/sourceMap.d.ts.map +1 -1
  62. package/dist/plugin/helpers/stashReturnValue.d.ts.map +1 -1
  63. package/dist/plugin/helpers/tryManifest.d.ts +3 -1
  64. package/dist/plugin/helpers/tryManifest.d.ts.map +1 -1
  65. package/dist/plugin/helpers/tryManifest.js +14 -8
  66. package/dist/plugin/helpers/tryManifest.js.map +1 -1
  67. package/dist/plugin/loader/transformModuleIfNeeded.d.ts.map +1 -1
  68. package/dist/plugin/loader/transformModuleIfNeeded.js +1 -2
  69. package/dist/plugin/loader/transformModuleIfNeeded.js.map +1 -1
  70. package/dist/plugin/metrics/formatMetrics.d.ts.map +1 -1
  71. package/dist/plugin/react-client/configureWorkerRequestHandler.d.ts.map +1 -1
  72. package/dist/plugin/react-client/configureWorkerRequestHandler.js +14 -18
  73. package/dist/plugin/react-client/configureWorkerRequestHandler.js.map +1 -1
  74. package/dist/plugin/react-client/createMessageHandlers.d.ts.map +1 -1
  75. package/dist/plugin/react-server/configureReactServer.d.ts.map +1 -1
  76. package/dist/plugin/react-server/configureReactServer.js +0 -12
  77. package/dist/plugin/react-server/configureReactServer.js.map +1 -1
  78. package/dist/plugin/react-static/configurePreviewServer.d.ts.map +1 -1
  79. package/dist/plugin/react-static/configurePreviewServer.js +1 -1
  80. package/dist/plugin/react-static/configurePreviewServer.js.map +1 -1
  81. package/dist/plugin/react-static/plugin.d.ts.map +1 -1
  82. package/dist/plugin/react-static/plugin.js +5 -14
  83. package/dist/plugin/react-static/plugin.js.map +1 -1
  84. package/dist/plugin/react-static/renderPages.js +1 -1
  85. package/dist/plugin/react-static/renderPages.js.map +1 -1
  86. package/dist/plugin/source-map/createMappingsSerializer.d.ts.map +1 -1
  87. package/dist/plugin/transformer/plugin.server.d.ts +24 -0
  88. package/dist/plugin/transformer/plugin.server.d.ts.map +1 -1
  89. package/dist/plugin/transformer/plugin.server.js +18 -15
  90. package/dist/plugin/transformer/plugin.server.js.map +1 -1
  91. package/dist/plugin/types.d.ts +4 -2
  92. package/dist/plugin/types.d.ts.map +1 -1
  93. package/dist/plugin/utils/createCallServer.d.ts.map +1 -1
  94. package/dist/plugin/utils/envUrls.node.d.ts.map +1 -1
  95. package/dist/plugin/utils/urls.d.ts.map +1 -1
  96. package/dist/plugin/vendor/types.d.ts +2 -0
  97. package/dist/plugin/vendor/types.d.ts.map +1 -0
  98. package/dist/plugin/vendor/types.js +1 -0
  99. package/dist/plugin/worker/createWorker.js +1 -1
  100. package/dist/plugin/worker/createWorker.js.map +1 -1
  101. package/dist/plugin/worker/rsc/state.d.ts +1 -1
  102. package/dist/plugin/worker/rsc/state.d.ts.map +1 -1
  103. package/dist/plugin/worker/rsc/state.js +31 -17
  104. package/dist/plugin/worker/rsc/state.js.map +1 -1
  105. package/dist/tsconfig.tsbuildinfo +1 -1
  106. package/package.json +5 -6
  107. package/plugin/config/autoDiscover/resolveAutoDiscover.ts +6 -14
  108. package/plugin/config/mimeTypes.ts +75 -1
  109. package/plugin/config/resolveUserConfig.ts +46 -21
  110. package/plugin/env/plugin.ts +39 -3
  111. package/plugin/helpers/collectManifestCss.ts +2 -2
  112. package/plugin/helpers/collectViteModuleGraphCss.ts +3 -0
  113. package/plugin/helpers/createCssProps.tsx +20 -3
  114. package/plugin/helpers/requestInfo.ts +1 -1
  115. package/plugin/helpers/tryManifest.ts +20 -10
  116. package/plugin/loader/transformModuleIfNeeded.ts +1 -4
  117. package/plugin/react-client/configureWorkerRequestHandler.ts +16 -20
  118. package/plugin/react-server/configureReactServer.ts +0 -12
  119. package/plugin/react-static/configurePreviewServer.ts +1 -2
  120. package/plugin/react-static/plugin.ts +20 -27
  121. package/plugin/transformer/plugin.server.ts +22 -15
  122. package/plugin/types.ts +8 -6
  123. package/plugin/vendor/types.ts +0 -0
  124. package/plugin/worker/rsc/state.ts +53 -28
@@ -1,17 +1,91 @@
1
-
2
1
  export const MIME_TYPES: Record<string, string> = {
2
+ // HTML and Web
3
3
  '.html': 'text/html',
4
+ '.htm': 'text/html',
5
+ '.xhtml': 'application/xhtml+xml',
6
+ '.xml': 'application/xml',
4
7
  '.css': 'text/css',
5
8
  '.js': 'application/javascript',
9
+ '.mjs': 'application/javascript',
10
+ '.cjs': 'application/javascript',
11
+ '.jsx': 'application/javascript',
12
+ '.ts': 'application/javascript',
13
+ '.tsx': 'application/javascript',
6
14
  '.json': 'application/json',
15
+ '.map': 'application/json',
16
+ '.wasm': 'application/wasm',
17
+ '.webmanifest': 'application/manifest+json',
18
+
19
+ // Images
7
20
  '.png': 'image/png',
8
21
  '.jpg': 'image/jpeg',
9
22
  '.jpeg': 'image/jpeg',
10
23
  '.gif': 'image/gif',
11
24
  '.svg': 'image/svg+xml',
12
25
  '.ico': 'image/x-icon',
26
+ '.webp': 'image/webp',
27
+ '.avif': 'image/avif',
28
+ '.bmp': 'image/bmp',
29
+ '.tiff': 'image/tiff',
30
+ '.tif': 'image/tiff',
31
+ '.heic': 'image/heic',
32
+ '.heif': 'image/heif',
33
+ '.apng': 'image/apng',
34
+
35
+ // Fonts
13
36
  '.woff': 'font/woff',
14
37
  '.woff2': 'font/woff2',
15
38
  '.ttf': 'font/ttf',
39
+ '.otf': 'font/otf',
40
+ '.eot': 'application/vnd.ms-fontobject',
41
+ '.sfnt': 'font/sfnt',
42
+
43
+ // Video
44
+ '.mp4': 'video/mp4',
45
+ '.webm': 'video/webm',
46
+ '.m4v': 'video/x-m4v',
47
+ '.m4s': 'video/iso.segment',
48
+ '.m3u8': 'application/vnd.apple.mpegurl',
49
+ '.mpd': 'application/dash+xml',
50
+ '.mov': 'video/quicktime',
51
+ '.avi': 'video/x-msvideo',
52
+ '.wmv': 'video/x-ms-wmv',
53
+ '.flv': 'video/x-flv',
54
+ '.mkv': 'video/x-matroska',
55
+ '.3gp': 'video/3gpp',
56
+ '.m2ts': 'video/mp2t',
57
+
58
+ // Audio
59
+ '.mp3': 'audio/mpeg',
60
+ '.wav': 'audio/wav',
61
+ '.ogg': 'audio/ogg',
62
+ '.m4a': 'audio/mp4',
63
+ '.aac': 'audio/aac',
64
+ '.flac': 'audio/flac',
65
+
66
+ // Documents
67
+ '.pdf': 'application/pdf',
68
+ '.doc': 'application/msword',
69
+ '.docx': 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
70
+ '.xls': 'application/vnd.ms-excel',
71
+ '.xlsx': 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
72
+ '.ppt': 'application/vnd.ms-powerpoint',
73
+ '.pptx': 'application/vnd.openxmlformats-officedocument.presentationml.presentation',
74
+
75
+ // Archives
76
+ '.zip': 'application/zip',
77
+ '.rar': 'application/x-rar-compressed',
78
+ '.7z': 'application/x-7z-compressed',
79
+ '.tar': 'application/x-tar',
80
+ '.gz': 'application/gzip',
81
+
82
+ // Data
83
+ '.csv': 'text/csv',
84
+ '.txt': 'text/plain',
85
+ '.md': 'text/markdown',
86
+ '.yaml': 'application/yaml',
87
+ '.yml': 'application/yaml',
88
+
89
+ // React specific
16
90
  '.rsc': 'text/x-component'
17
91
  };
@@ -86,9 +86,7 @@ export function resolveUserConfig<
86
86
  info.type === "asset" &&
87
87
  userOptions.autoDiscover.cssPattern(value)
88
88
  ) {
89
- const found = entry.css?.find((css) =>
90
- css.startsWith(id as string)
91
- );
89
+ const found = entry.css?.find((css) => css.startsWith(id as string));
92
90
  if (found) {
93
91
  return found;
94
92
  } else {
@@ -154,7 +152,9 @@ export function resolveUserConfig<
154
152
  }
155
153
  stashedReturns[inputId] = r;
156
154
  }
157
- return stashedReturns[inputId];
155
+ // in the case of empty basePath, it will not be sliced from the path, so, we need to slice it here
156
+ // at the last possible moment as to not confuse the rest of the logic around the basePath
157
+ return stashedReturns[inputId].slice(Number(stashedReturns[inputId].startsWith("/")));
158
158
  }),
159
159
  assetFileNames: process.env["VITEST"]
160
160
  ? undefined
@@ -171,7 +171,9 @@ export function resolveUserConfig<
171
171
  }
172
172
  stashedReturns[inputId] = r;
173
173
  }
174
- return stashedReturns[inputId];
174
+ // in the case of empty basePath, it will not be sliced from the path, so, we need to slice it here
175
+ // at the last possible moment as to not confuse the rest of the logic around the basePath
176
+ return stashedReturns[inputId].slice(Number(stashedReturns[inputId].startsWith("/")));
175
177
  }),
176
178
  chunkFileNames:
177
179
  userDefinedChunkFileNames ??
@@ -194,7 +196,9 @@ export function resolveUserConfig<
194
196
  }
195
197
  stashedReturns[inputId] = r;
196
198
  }
197
- return stashedReturns[inputId];
199
+ // in the case of empty basePath, it will not be sliced from the path, so, we need to slice it here
200
+ // at the last possible moment as to not confuse the rest of the logic around the basePath
201
+ return stashedReturns[inputId].slice(Number(stashedReturns[inputId].startsWith("/")));
198
202
  }),
199
203
  format: "esm",
200
204
  exports: "named",
@@ -207,7 +211,11 @@ export function resolveUserConfig<
207
211
  ? [config.build?.rollupOptions?.output, pluginOutput]
208
212
  : pluginOutput;
209
213
  const vitePrefix = config.envPrefix ?? DEFAULT_CONFIG.ENV_PREFIX;
210
- const mode = config.mode ?? process.env["VITE_MODE"];
214
+ const mode =
215
+ config.mode ??
216
+ process.env[`${vitePrefix}MODE`] ??
217
+ process.env["NODE_ENV"] ??
218
+ "production";
211
219
  const minify = config.build?.minify;
212
220
 
213
221
  const srrConfig = {
@@ -229,15 +237,20 @@ export function resolveUserConfig<
229
237
  },
230
238
  };
231
239
  let publicOrigin =
232
- userOptions.publicOrigin ?? process.env.VITE_PUBLIC_ORIGIN ?? "";
240
+ userOptions.publicOrigin ?? process.env[`${vitePrefix}PUBLIC_ORIGIN`] ?? "";
233
241
  let PROD = mode === "production";
234
242
  let DEV = mode === "development";
243
+ let port =
244
+ typeof config.server?.port === "number" ? config.server?.port : 5173;
245
+ let strictPort = config.server?.strictPort ?? true;
246
+ let host =
247
+ typeof config.server?.host === "string" ? config.server?.host : "localhost";
235
248
  if (configEnv.command === "serve" && !configEnv.isPreview) {
236
- publicOrigin = `http${config.server?.https ? "s" : ""}://${
237
- typeof config.server?.host === "string"
238
- ? config.server?.host
239
- : "localhost"
240
- }:${typeof config.server?.port === "number" ? config.server?.port : 5173}`;
249
+ if (strictPort) {
250
+ publicOrigin = `http${config.server?.https ? "s" : ""}://${host}:${port}`;
251
+ } else {
252
+ publicOrigin = "";
253
+ }
241
254
  }
242
255
  const ssrDefine = ssr
243
256
  ? {
@@ -257,13 +270,13 @@ export function resolveUserConfig<
257
270
  // these will never be cleaned up, because, we are resolving the user config
258
271
  // and it's assumed the thread closes after this and we don't want
259
272
  // it to change after the config has been resolved
260
- if (process.env.VITE_BASE_URL !== userOptions.moduleBaseURL) {
261
- process.env.VITE_BASE_URL = userOptions.moduleBaseURL;
273
+ if (process.env[`${vitePrefix}BASE_URL`] !== userOptions.moduleBaseURL) {
274
+ process.env[`${vitePrefix}BASE_URL`] = userOptions.moduleBaseURL;
262
275
  }
263
- if (process.env.VITE_PUBLIC_ORIGIN !== publicOrigin) {
264
- process.env.VITE_PUBLIC_ORIGIN = publicOrigin;
276
+ if (process.env[`${vitePrefix}PUBLIC_ORIGIN`] !== publicOrigin) {
277
+ process.env[`${vitePrefix}PUBLIC_ORIGIN`] = publicOrigin;
265
278
  }
266
-
279
+
267
280
  if (condition === "react-client") {
268
281
  // client plugin build options (client plugin still outputs server files)
269
282
  const clientConfig = {
@@ -282,6 +295,15 @@ export function resolveUserConfig<
282
295
  },
283
296
  define: define,
284
297
  ssr: srrConfig,
298
+ server: {
299
+ ...config.server,
300
+ // common default for stricter server operations
301
+ // and ensures tests that use a server will fail early
302
+ // also, we can't set the public origin without a port
303
+ port: port,
304
+ strictPort: strictPort,
305
+ host: host,
306
+ },
285
307
  // client build options
286
308
  build: {
287
309
  ...config.build,
@@ -298,9 +320,12 @@ export function resolveUserConfig<
298
320
  minify: minify,
299
321
  rollupOptions: {
300
322
  ...config.build?.rollupOptions,
301
- input: {
302
- ...autoDiscoveredFiles.inputs,
303
- },
323
+ input: Object.fromEntries(
324
+ Object.entries(autoDiscoveredFiles.inputs).map(([key, value]) => [
325
+ key,
326
+ value.slice(Number(value.startsWith("/"))),
327
+ ])
328
+ ),
304
329
  output: newOutput,
305
330
  preserveEntrySignatures:
306
331
  config.build?.rollupOptions?.preserveEntrySignatures ??
@@ -44,12 +44,20 @@ const cleanupInitialUserConfigEnv = resolveEnv(
44
44
  */
45
45
  export function envPlugin(): Plugin {
46
46
  let cleanupEnv: (() => void) | undefined;
47
+ let vitePrefix = DEFAULT_CONFIG.ENV_PREFIX as string;
47
48
  return {
48
49
  name: "vite-plugin-react-server:env",
49
50
  enforce: "pre",
50
51
  config(config, { mode }) {
52
+ if(config.envPrefix) {
53
+ if(typeof config.envPrefix === "string") {
54
+ vitePrefix = config.envPrefix;
55
+ } else if(Array.isArray(config.envPrefix)) {
56
+ vitePrefix = config.envPrefix[0];
57
+ }
58
+ }
51
59
  const invalidEnv =
52
- (mode !== undefined && mode !== process.env.VITE_MODE) ||
60
+ (mode !== undefined && mode !== process.env[`${vitePrefix}MODE`]) ||
53
61
  (config.root !== undefined && config.root !== userProjectRoot);
54
62
  if (invalidEnv) {
55
63
  cleanupInitialUserConfigEnv();
@@ -57,9 +65,9 @@ export function envPlugin(): Plugin {
57
65
  // Clean up any previous env setup
58
66
  const cleanupUserConfigEnv = invalidEnv
59
67
  ? resolveEnv(
60
- mode || process.env.VITE_MODE,
68
+ mode || process.env[`${vitePrefix}MODE`] || "production",
61
69
  config.root ?? userProjectRoot,
62
- config.envPrefix ?? DEFAULT_CONFIG.ENV_PREFIX
70
+ vitePrefix
63
71
  )
64
72
  : cleanupInitialUserConfigEnv;
65
73
  const cleanupUserConfig = resolveConfigDefine(config);
@@ -74,5 +82,33 @@ export function envPlugin(): Plugin {
74
82
  // Clean up environment variables when the bundle is closed
75
83
  cleanupEnv?.();
76
84
  },
85
+ configureServer(server) {
86
+ let envPrefix = Array.isArray(server.config.envPrefix) ? server.config.envPrefix[0] : server.config.envPrefix ?? DEFAULT_CONFIG.ENV_PREFIX;
87
+ let publicOrigin = process.env[`${envPrefix}PUBLIC_ORIGIN`] ?? ""
88
+
89
+ let desiredPort = server.config.server.port;
90
+ let shouldUpdatePublicOrigin = false;
91
+ if (publicOrigin && publicOrigin.includes(`:${desiredPort}`)) {
92
+ shouldUpdatePublicOrigin = true;
93
+ }
94
+ // Listen for when the server actually starts
95
+ if (shouldUpdatePublicOrigin) {
96
+ server.httpServer?.once("listening", () => {
97
+ const address = server.httpServer?.address();
98
+ if (address && typeof address !== "string") {
99
+ const port = address.port;
100
+ if (port !== desiredPort) {
101
+ let envPrefix = Array.isArray(server.config.envPrefix) ? server.config.envPrefix[0] : server.config.envPrefix ?? DEFAULT_CONFIG.ENV_PREFIX;
102
+ const newOrigin = publicOrigin.replace(
103
+ `:${desiredPort}`,
104
+ `:${port}`
105
+ );
106
+ process.env[`${envPrefix}PUBLIC_ORIGIN`] = newOrigin
107
+ console.warn("PUBLIC_ORIGIN did not match the port: " + port);
108
+ }
109
+ }
110
+ });
111
+ }
112
+ }
77
113
  };
78
114
  }
@@ -27,7 +27,7 @@ export function collectManifestCss(
27
27
  // Add CSS files from the css property
28
28
  if (fileInfo.css) {
29
29
  for (const cssFile of fileInfo.css) {
30
- cssInputs[cssFile] = cssFile + '.css';
30
+ cssInputs[cssFile] = cssFile;
31
31
  }
32
32
  }
33
33
 
@@ -46,7 +46,7 @@ export function collectManifestCss(
46
46
  const importedFile = manifest[importPath];
47
47
  if (importedFile?.css) {
48
48
  for (const cssFile of importedFile.css) {
49
- cssInputs[cssFile] = cssFile + '.css';
49
+ cssInputs[cssFile] = cssFile;
50
50
  }
51
51
  }
52
52
  }
@@ -51,6 +51,7 @@ export async function collectViteModuleGraphCss<
51
51
  | "loader"
52
52
  | "normalizer"
53
53
  | "moduleID"
54
+ | "publicOrigin"
54
55
  >;
55
56
  }): Promise<CollectViteModuleGraphCssResult> {
56
57
  const {
@@ -59,6 +60,7 @@ export async function collectViteModuleGraphCss<
59
60
  moduleBasePath,
60
61
  moduleRootPath,
61
62
  projectRoot,
63
+ publicOrigin,
62
64
  css,
63
65
  loader,
64
66
  normalizer,
@@ -113,6 +115,7 @@ export async function collectViteModuleGraphCss<
113
115
  css: css,
114
116
  normalizer: normalizer,
115
117
  moduleID: moduleID,
118
+ publicOrigin: publicOrigin,
116
119
  },
117
120
  });
118
121
  cssFiles.set(mod?.url, cssContent);
@@ -41,6 +41,7 @@ export const createCssProps = <
41
41
  | "projectRoot"
42
42
  | "normalizer"
43
43
  | "moduleID"
44
+ | "publicOrigin"
44
45
  >;
45
46
  }): CssContent<InlineCSS> => {
46
47
  const { css, moduleRootPath } = userOptions;
@@ -77,15 +78,31 @@ export const createCssProps = <
77
78
  : {}),
78
79
  } as CssContent<InlineCSS>;
79
80
  }
81
+ const processEnv = process.env || {};
82
+ const hasEnv= typeof processEnv.VITE_PUBLIC_ORIGIN === "string" &&
83
+ processEnv.VITE_PUBLIC_ORIGIN !== "";
84
+ const importMeta = import.meta || {};
85
+ const hasMetaEnv = 'env' in importMeta && typeof importMeta.env.PUBLIC_ORIGIN === "string" &&
86
+ importMeta.env.PUBLIC_ORIGIN !== "";
87
+ // final public origin check
88
+ if((hasEnv || hasMetaEnv) && userOptions.publicOrigin) {
89
+ // change the public origin to the one from the env
90
+ if(hasEnv && userOptions.publicOrigin !== processEnv.VITE_PUBLIC_ORIGIN) {
91
+ // prefer potentially dynamic process.env
92
+ userOptions.publicOrigin = processEnv.VITE_PUBLIC_ORIGIN;
93
+ } else if(hasMetaEnv && userOptions.publicOrigin !== import.meta.env.PUBLIC_ORIGIN) {
94
+ // static import.meta.env
95
+ userOptions.publicOrigin = import.meta.env.PUBLIC_ORIGIN;
96
+ }
97
+ }
80
98
  // Default case
81
99
  return {
82
100
  id: moduleID,
83
101
  as: "link",
84
102
  rel: "stylesheet",
85
103
  href:
86
- typeof process.env.VITE_PUBLIC_ORIGIN === "string" &&
87
- process.env.VITE_PUBLIC_ORIGIN !== ""
88
- ? new URL(moduleID, process.env.VITE_PUBLIC_ORIGIN).href
104
+ userOptions.publicOrigin !== ""
105
+ ? new URL(moduleID, userOptions.publicOrigin).href
89
106
  : moduleID,
90
107
  precedence: "high",
91
108
  } as CssContent<InlineCSS>;
@@ -119,7 +119,7 @@ export function requestInfo(
119
119
  } else {
120
120
  const mimeType = MIME_TYPES[ext];
121
121
  if (mimeType) {
122
- contentType = mimeType;
122
+ contentType = mimeType + "; charset=utf-8";
123
123
  } else {
124
124
  contentType = "application/octet-stream";
125
125
  }
@@ -8,7 +8,7 @@ type TryManifestOptions<SSR extends boolean = false> = {
8
8
  outDir: string;
9
9
  ssrManifest?: SSR;
10
10
  preserveModulesRoot?: string;
11
- manifestPath?: string | undefined;
11
+ manifestPath?: string | boolean | undefined;
12
12
  };
13
13
 
14
14
  export async function tryManifest<SSR extends boolean>(
@@ -23,16 +23,26 @@ export async function tryManifest<SSR extends boolean>(
23
23
  type: "error";
24
24
  error: Error;
25
25
  manifest?: never;
26
- }> {
27
- const localSsrManifestPath = !options.ssrManifest ? undefined : options.manifestPath ? options.manifestPath : join('.vite', 'ssr-manifest.json');
28
- const localManifestPath = options.ssrManifest ? undefined : options.manifestPath ? options.manifestPath : join('.vite', 'manifest.json');
29
- const manifestPath = resolve(
30
- options.root,
31
- options.outDir,
32
- options.ssrManifest ? localSsrManifestPath as string : localManifestPath as string
33
- );
26
+ }
27
+ | {
28
+ type: "skip";
29
+ }
30
+ > {
31
+ let path = options.manifestPath;
32
+ if (path === false) {
33
+ return {
34
+ type: "skip",
35
+ };
36
+ }
37
+ if (options.ssrManifest) {
38
+ path = join(".vite", "ssr-manifest.json");
39
+ } else {
40
+ path = join(".vite", "manifest.json");
41
+ }
34
42
  try {
35
- const result = JSON.parse(await readFile(manifestPath, "utf-8"));
43
+ const result = JSON.parse(
44
+ await readFile(resolve(options.root, options.outDir, path), "utf-8")
45
+ );
36
46
  return {
37
47
  type: "success",
38
48
  manifest: result,
@@ -10,9 +10,6 @@ export function transformModuleIfNeeded(
10
10
  isClientComponent: boolean | RegExpMatchArray | null = DEFAULT_CONFIG.AUTO_DISCOVER.isClientComponentCode(source),
11
11
  isServerEnvironment = getCondition() === "react-server"
12
12
  ) {
13
- // Parse source and handle source maps
14
- const ast = parse(source);
15
-
16
13
  // Handle environment-specific cases
17
14
  if (
18
15
  (isServerEnvironment && !isServerFunction && !isClientComponent) ||
@@ -24,7 +21,7 @@ export function transformModuleIfNeeded(
24
21
  const result = transformModuleWithPreservedFunctions(
25
22
  source,
26
23
  moduleId,
27
- ast,
24
+ parse(source),
28
25
  isServerFunction,
29
26
  isClientComponent
30
27
  );
@@ -19,6 +19,7 @@ import type { RscWorkerInputMessage } from "../worker/types.js";
19
19
  import { Readable } from "node:stream";
20
20
  import type { ReadableStream } from "node:stream/web";
21
21
  import { PassThrough } from "node:stream";
22
+ import { logError, toError } from "../error/toError.js";
22
23
 
23
24
  /**
24
25
  * Configures the worker request handler.
@@ -102,7 +103,7 @@ export async function configureWorkerRequestHandler<
102
103
  }
103
104
  const body = Buffer.concat(chunks).toString();
104
105
  const parsed = JSON.parse(body);
105
-
106
+
106
107
  // Get action ID and args from the request body
107
108
  let id: string;
108
109
  let args: unknown[];
@@ -153,9 +154,7 @@ export async function configureWorkerRequestHandler<
153
154
  } else if (message.type === "ERROR") {
154
155
  passThrough.end();
155
156
  currentWorker!.removeListener("message", messageHandler);
156
- server.config.logger.error(message.error.message + (message.error.stack ?? ""), {
157
- error: message.error,
158
- });
157
+ logError(message.error, server.config.logger);
159
158
  }
160
159
  };
161
160
 
@@ -163,27 +162,24 @@ export async function configureWorkerRequestHandler<
163
162
 
164
163
  // Handle errors
165
164
  passThrough.on("error", (error) => {
166
- server.config.logger.error(error.message + (error.stack ?? ""), {
167
- error,
168
- });
165
+ logError(error, server.config.logger);
169
166
  res.end();
170
167
  });
171
168
 
172
169
  return;
173
170
  } catch (error) {
174
- if (error instanceof Error) {
175
- server.config.logger.error(error.message + (error.stack ?? ""), {
176
- error,
177
- });
178
- }
171
+ const err = toError(error);
172
+ logError(err, server.config.logger);
179
173
  res.statusCode = 500;
180
- res.end(JSON.stringify({
181
- type: "server-action-response",
182
- returnValue: {
183
- success: false,
184
- error: error instanceof Error ? error.message : String(error)
185
- }
186
- }));
174
+ res.end(
175
+ JSON.stringify({
176
+ type: "server-action-response",
177
+ returnValue: {
178
+ success: false,
179
+ error: err.message,
180
+ },
181
+ })
182
+ );
187
183
  return;
188
184
  }
189
185
  }
@@ -320,5 +316,5 @@ export async function configureWorkerRequestHandler<
320
316
  };
321
317
  // attach handler to the server
322
318
  server.middlewares.use(handler);
323
- // done
319
+ // port check, should be handled by strictPort
324
320
  }
@@ -170,16 +170,4 @@ export async function configureReactServer<
170
170
  }
171
171
  });
172
172
  // Listen for when the server actually starts
173
- server.httpServer?.once("listening", () => {
174
- const address = server.httpServer?.address();
175
- if (address && typeof address !== "string") {
176
- const port = address.port;
177
- const host = server.config.server.host ?? "localhost";
178
- const protocol = server.config.server.https ? "https" : "http";
179
- handlerOptions.publicOrigin = `${protocol}://${host}:${port}`;
180
- if (handlerOptions.publicOrigin !== process.env.VITE_PUBLIC_ORIGIN) {
181
- process.env.VITE_PUBLIC_ORIGIN = handlerOptions.publicOrigin;
182
- }
183
- }
184
- });
185
173
  }
@@ -39,9 +39,8 @@ export async function configurePreviewServer<
39
39
  staticHostDir,
40
40
  server.config.logger
41
41
  );
42
-
43
42
  // Handle static files including CSS
44
- if (filePath) {
43
+ if (filePath && contentType.includes("text/x-component")) {
45
44
  try {
46
45
  const stats = await stat(filePath);
47
46
  if (stats.isFile()) {
@@ -49,7 +49,6 @@ import {
49
49
  } from "../helpers/serializeUserOptions.js";
50
50
  import { collectManifestCss } from "../helpers/collectManifestCss.js";
51
51
  import { createCssProps } from "../helpers/createCssProps.js";
52
- import { tryManifest } from "../helpers/tryManifest.js";
53
52
  import { performance } from "node:perf_hooks";
54
53
  import { DEFAULT_CONFIG } from "../config/defaults.js";
55
54
  import { baseURL } from "../utils/envUrls.node.js";
@@ -101,7 +100,7 @@ export function reactStaticPlugin<
101
100
  if (config.root && config.root !== userOptions.projectRoot) {
102
101
  userOptions.projectRoot = config.root;
103
102
  }
104
- if(configEnv.command !== "build") {
103
+ if (configEnv.command !== "build") {
105
104
  return;
106
105
  }
107
106
  // Initialize autoDiscoveredFiles for both server and client builds
@@ -165,35 +164,29 @@ export function reactStaticPlugin<
165
164
  bundle,
166
165
  normalizer: userOptions.normalizer,
167
166
  });
168
- if (!("source" in bundleManifest[".vite/manifest.json"])) {
167
+ const manifestPath =
168
+ typeof resolvedConfig.build.manifest === "string"
169
+ ? resolvedConfig.build.manifest
170
+ : ".vite/manifest.json";
171
+ if (!("source" in bundleManifest[manifestPath])) {
169
172
  throw new Error("Server manifest not found");
170
173
  }
171
174
 
172
175
  serverManifest = JSON.parse(
173
- bundleManifest[".vite/manifest.json"].source as string
176
+ bundleManifest[manifestPath].source as string
174
177
  );
175
178
 
176
179
  if (!serverManifest) {
177
180
  throw new Error("Failed to parse server manifest");
178
181
  }
179
-
180
- const clientManifestResult = await tryManifest({
181
- root: userOptions.projectRoot,
182
- outDir: join(userOptions.build.outDir, userOptions.build.client),
183
- ssrManifest: false,
184
- });
185
- if (clientManifestResult.type === "error") {
186
- throw clientManifestResult.error;
187
- }
188
- const clientManifest = clientManifestResult.manifest;
189
182
 
190
- buildLoader = await createBuildLoader<T, InlineCSS>(
183
+ buildLoader = await createBuildLoader(
191
184
  {
192
185
  userConfig,
193
- userOptions: userOptions as ResolvedUserOptions<T, InlineCSS>,
186
+ userOptions: userOptions,
194
187
  serverManifest: serverManifest ?? {},
195
188
  staticManifest: autoDiscoveredFiles?.staticManifest ?? {},
196
- clientManifest: clientManifest ?? {},
189
+ clientManifest: {},
197
190
  },
198
191
  bundle
199
192
  );
@@ -230,7 +223,6 @@ export function reactStaticPlugin<
230
223
  transformedServerManifest,
231
224
  props ? [page, props] : page
232
225
  );
233
-
234
226
  // Create a map for this page's CSS files
235
227
  const pageCssMap: Map<string, CssContent> = new Map();
236
228
  // Add global styles if they exist
@@ -240,15 +232,16 @@ export function reactStaticPlugin<
240
232
  String(r.default)
241
233
  );
242
234
  if (cssContent === "undefined" || !cssContent) {
243
- cssContent = await readFile(
244
- join(
245
- userOptions.projectRoot,
246
- userOptions.build.outDir,
247
- userOptions.build.static,
248
- key
249
- ),
250
- "utf-8"
251
- ) ?? ""
235
+ cssContent =
236
+ (await readFile(
237
+ join(
238
+ userOptions.projectRoot,
239
+ userOptions.build.outDir,
240
+ userOptions.build.static,
241
+ key
242
+ ),
243
+ "utf-8"
244
+ )) ?? "";
252
245
  }
253
246
  if (cssContent) {
254
247
  globalCss.set(