vite-plugin-react-server 0.3.19 → 1.0.0

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 (108) hide show
  1. package/README.md +219 -141
  2. package/dist/package.json +13 -6
  3. package/dist/plugin/collect-manifest-client-files.d.ts.map +1 -1
  4. package/dist/plugin/collect-manifest-client-files.js +25 -11
  5. package/dist/plugin/collect-manifest-client-files.js.map +1 -1
  6. package/dist/plugin/components.d.ts +3 -13
  7. package/dist/plugin/components.d.ts.map +1 -1
  8. package/dist/plugin/components.js +3 -54
  9. package/dist/plugin/config/defaults.d.ts +9 -2
  10. package/dist/plugin/config/defaults.d.ts.map +1 -1
  11. package/dist/plugin/config/defaults.js +3 -2
  12. package/dist/plugin/config/defaults.js.map +1 -1
  13. package/dist/plugin/config/resolveOptions.d.ts +2 -2
  14. package/dist/plugin/config/resolveOptions.d.ts.map +1 -1
  15. package/dist/plugin/config/resolveOptions.js +6 -0
  16. package/dist/plugin/config/resolveOptions.js.map +1 -1
  17. package/dist/plugin/config/resolveUserConfig.d.ts.map +1 -1
  18. package/dist/plugin/config/resolveUserConfig.js +178 -149
  19. package/dist/plugin/config/resolveUserConfig.js.map +1 -1
  20. package/dist/plugin/css-collector-inline.d.ts +10 -0
  21. package/dist/plugin/css-collector-inline.d.ts.map +1 -0
  22. package/dist/plugin/css-collector-inline.js +55 -0
  23. package/dist/plugin/css-collector-inline.js.map +1 -0
  24. package/dist/plugin/css-collector.d.ts +14 -0
  25. package/dist/plugin/css-collector.d.ts.map +1 -0
  26. package/dist/plugin/css-collector.js +49 -0
  27. package/dist/plugin/css-collector.js.map +1 -0
  28. package/dist/plugin/helpers/createHandler.d.ts +3 -8
  29. package/dist/plugin/helpers/createHandler.d.ts.map +1 -1
  30. package/dist/plugin/helpers/createHandler.js +52 -55
  31. package/dist/plugin/helpers/createHandler.js.map +1 -1
  32. package/dist/plugin/helpers/createRscStream.d.ts +14 -4
  33. package/dist/plugin/helpers/createRscStream.d.ts.map +1 -1
  34. package/dist/plugin/helpers/createRscStream.js +25 -16
  35. package/dist/plugin/helpers/createRscStream.js.map +1 -1
  36. package/dist/plugin/html.d.ts +5 -0
  37. package/dist/plugin/html.d.ts.map +1 -0
  38. package/dist/plugin/html.js +11 -0
  39. package/dist/plugin/html.js.map +1 -0
  40. package/dist/plugin/react-client/plugin.d.ts.map +1 -1
  41. package/dist/plugin/react-client/plugin.js +3 -2
  42. package/dist/plugin/react-client/plugin.js.map +1 -1
  43. package/dist/plugin/react-server/plugin.d.ts.map +1 -1
  44. package/dist/plugin/react-server/plugin.js +47 -16
  45. package/dist/plugin/react-server/plugin.js.map +1 -1
  46. package/dist/plugin/react-static/plugin.d.ts.map +1 -1
  47. package/dist/plugin/react-static/plugin.js +20 -12
  48. package/dist/plugin/react-static/plugin.js.map +1 -1
  49. package/dist/plugin/react-static/types.d.ts +2 -0
  50. package/dist/plugin/react-static/types.d.ts.map +1 -0
  51. package/dist/plugin/react-static/types.js +1 -0
  52. package/dist/plugin/types.d.ts +48 -11
  53. package/dist/plugin/types.d.ts.map +1 -1
  54. package/dist/plugin/worker/html/messageHandler.d.ts.map +1 -1
  55. package/dist/plugin/worker/html/messageHandler.js +4 -1
  56. package/dist/plugin/worker/html/messageHandler.js.map +1 -1
  57. package/dist/plugin/worker/html/renderPages.d.ts +8 -25
  58. package/dist/plugin/worker/html/renderPages.d.ts.map +1 -1
  59. package/dist/plugin/worker/html/renderPages.js +61 -44
  60. package/dist/plugin/worker/html/renderPages.js.map +1 -1
  61. package/dist/plugin/worker/rsc/messageHandler.d.ts.map +1 -1
  62. package/dist/plugin/worker/rsc/messageHandler.js +37 -22
  63. package/dist/plugin/worker/rsc/messageHandler.js.map +1 -1
  64. package/dist/plugin/worker/types.d.ts +3 -0
  65. package/dist/plugin/worker/types.d.ts.map +1 -1
  66. package/dist/tsconfig.tsbuildinfo +1 -1
  67. package/package.json +12 -4
  68. package/plugin/collect-manifest-client-files.ts +25 -10
  69. package/plugin/components.ts +3 -0
  70. package/plugin/config/defaults.tsx +10 -9
  71. package/plugin/config/resolveOptions.ts +10 -5
  72. package/plugin/config/resolveUserConfig.ts +218 -177
  73. package/plugin/css-collector-inline.tsx +60 -0
  74. package/plugin/css-collector.tsx +62 -0
  75. package/plugin/helpers/createHandler.ts +73 -83
  76. package/plugin/helpers/createRscStream.ts +49 -21
  77. package/plugin/html.tsx +9 -0
  78. package/plugin/react-client/plugin.ts +3 -2
  79. package/plugin/react-server/plugin.ts +58 -22
  80. package/plugin/react-static/plugin.ts +20 -11
  81. package/plugin/react-static/types.ts +3 -0
  82. package/plugin/types.ts +53 -11
  83. package/plugin/worker/html/messageHandler.ts +5 -2
  84. package/plugin/worker/html/renderPages.ts +82 -78
  85. package/plugin/worker/rsc/messageHandler.tsx +41 -26
  86. package/plugin/worker/types.ts +3 -0
  87. package/dist/plugin/components.js.map +0 -1
  88. package/dist/plugin/getEnv.d.ts +0 -19
  89. package/dist/plugin/getEnv.d.ts.map +0 -1
  90. package/dist/plugin/getEnv.js +0 -107
  91. package/dist/plugin/module-graph.d.ts +0 -10
  92. package/dist/plugin/module-graph.d.ts.map +0 -1
  93. package/dist/plugin/module-graph.js +0 -35
  94. package/dist/plugin/worker/html/plugin.d.ts +0 -4
  95. package/dist/plugin/worker/html/plugin.d.ts.map +0 -1
  96. package/dist/plugin/worker/html/plugin.js +0 -93
  97. package/dist/plugin/worker/plugin.d.ts +0 -19
  98. package/dist/plugin/worker/plugin.d.ts.map +0 -1
  99. package/dist/plugin/worker/plugin.js +0 -23
  100. package/dist/plugin/worker/rsc/plugin.d.ts +0 -4
  101. package/dist/plugin/worker/rsc/plugin.d.ts.map +0 -1
  102. package/dist/plugin/worker/rsc/plugin.js +0 -75
  103. package/plugin/components.tsx +0 -59
  104. package/plugin/getEnv.ts +0 -135
  105. package/plugin/module-graph.ts +0 -48
  106. package/plugin/worker/html/plugin.ts +0 -100
  107. package/plugin/worker/plugin.ts +0 -26
  108. package/plugin/worker/rsc/plugin.ts +0 -83
@@ -1,93 +0,0 @@
1
- import { join, resolve } from "path";
2
- import { DEFAULT_CONFIG } from "../../config/defaults.js";
3
- import { pluginRoot } from "../../root.js";
4
- import { resolveOptions } from "../../config/resolveOptions.js";
5
- let userOptions;
6
- export function reactHtmlWorkerPlugin(options) {
7
- const resolvedUserOptions = resolveOptions(options, false);
8
- if (resolvedUserOptions.type === 'error') {
9
- throw resolvedUserOptions.error;
10
- }
11
- userOptions = resolvedUserOptions.userOptions;
12
- return {
13
- name: "vite:react-html-worker",
14
- config(config) {
15
- const root = config.root ?? process.cwd();
16
- const htmlWorkerPath = typeof options.htmlWorkerPath === 'string'
17
- ? resolve(root, options.htmlWorkerPath)
18
- : resolve(pluginRoot, DEFAULT_CONFIG.HTML_WORKER_PATH);
19
- const format = 'esm';
20
- // Single worker output for server build
21
- const workerConfig = {
22
- input: {
23
- 'html-worker': htmlWorkerPath,
24
- },
25
- output: {
26
- format,
27
- dir: join(userOptions.build.outDir, userOptions.build.server),
28
- entryFileNames: '[name].js',
29
- preserveModules: true,
30
- preserveModulesRoot: userOptions.build.preserveModulesRoot === true ? userOptions.moduleBase : undefined,
31
- // Add manifest entry
32
- manualChunks: {
33
- 'html-worker': [htmlWorkerPath]
34
- }
35
- }
36
- };
37
- return {
38
- build: {
39
- rollupOptions: {
40
- preserveEntrySignatures: 'strict',
41
- input: {
42
- ...workerConfig.input,
43
- ...(typeof config.build?.rollupOptions?.input === 'object'
44
- ? config.build?.rollupOptions?.input
45
- : {}),
46
- },
47
- external: [
48
- 'vite',
49
- 'rollup',
50
- 'react',
51
- 'react-dom',
52
- 'react-dom/server',
53
- 'react-server-dom-esm',
54
- 'react-server-dom-esm/client.node',
55
- 'react-server-dom-esm/server.node',
56
- 'react-server-dom-esm/node-loader',
57
- 'source-map',
58
- 'acorn-loose',
59
- 'webpack-sources',
60
- 'stream',
61
- 'util',
62
- 'crypto',
63
- 'async_hooks',
64
- 'fs',
65
- 'path',
66
- 'worker_threads',
67
- // if we use node: paths in our code, it should always be catched by below rule.
68
- /^node:.*/,
69
- ],
70
- output: {
71
- ...workerConfig.output,
72
- }
73
- },
74
- manifest: true, // Ensure manifest is generated
75
- minify: false,
76
- sourcemap: true,
77
- }
78
- };
79
- },
80
- // Add this to ensure entry is in manifest
81
- generateBundle(_, bundle) {
82
- const workerEntry = bundle['html-worker.js'];
83
- if (workerEntry) {
84
- Object.defineProperty(workerEntry, 'isEntry', {
85
- value: true,
86
- writable: false,
87
- enumerable: true,
88
- configurable: false
89
- });
90
- }
91
- }
92
- };
93
- }
@@ -1,19 +0,0 @@
1
- import type { StreamPluginOptions } from "../types.js";
2
- import type { Plugin } from "vite";
3
- /**
4
- * This plugin can be used to create your own worker paths. This build should be separated from the main build.
5
- *
6
- *
7
- * ```ts
8
- * @example
9
- *export reactWorkerPluginConfig = {
10
- * htmlWorkerPath: './workers/html.tsx',
11
- * rscWorkerPath: './workers/rsc.tsx',
12
- * }
13
- * ```
14
- *
15
- * @param options
16
- * @returns
17
- */
18
- export declare function reactWorkerPlugin(options: StreamPluginOptions): Plugin[];
19
- //# sourceMappingURL=plugin.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"plugin.d.ts","sourceRoot":"","sources":["../../../plugin/worker/plugin.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,mBAAmB,EAAE,MAAM,aAAa,CAAA;AACtD,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,MAAM,CAAA;AAIlC;;;;;;;;;;;;;;GAcG;AACH,wBAAgB,iBAAiB,CAAC,OAAO,EAAE,mBAAmB,GAAG,MAAM,EAAE,CAKxE"}
@@ -1,23 +0,0 @@
1
- import { reactHtmlWorkerPlugin } from "./html/plugin.js";
2
- import { reactRscWorkerPlugin } from "./rsc/plugin.js";
3
- /**
4
- * This plugin can be used to create your own worker paths. This build should be separated from the main build.
5
- *
6
- *
7
- * ```ts
8
- * @example
9
- *export reactWorkerPluginConfig = {
10
- * htmlWorkerPath: './workers/html.tsx',
11
- * rscWorkerPath: './workers/rsc.tsx',
12
- * }
13
- * ```
14
- *
15
- * @param options
16
- * @returns
17
- */
18
- export function reactWorkerPlugin(options) {
19
- return [
20
- reactHtmlWorkerPlugin(options),
21
- reactRscWorkerPlugin(options)
22
- ];
23
- }
@@ -1,4 +0,0 @@
1
- import type { Plugin } from "vite";
2
- import type { StreamPluginOptions } from "../../types.js";
3
- export declare function reactRscWorkerPlugin(options: StreamPluginOptions): Plugin;
4
- //# sourceMappingURL=plugin.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"plugin.d.ts","sourceRoot":"","sources":["../../../../plugin/worker/rsc/plugin.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,MAAM,CAAC;AAGnC,OAAO,KAAK,EAAE,mBAAmB,EAAE,MAAM,gBAAgB,CAAC;AAI1D,wBAAgB,oBAAoB,CAAC,OAAO,EAAE,mBAAmB,GAAG,MAAM,CA2EzE"}
@@ -1,75 +0,0 @@
1
- import { resolve } from "path";
2
- import { DEFAULT_CONFIG } from "../../config/defaults.js";
3
- import { pluginRoot } from "../../root.js";
4
- export function reactRscWorkerPlugin(options) {
5
- return {
6
- name: "vite:react-rsc-worker",
7
- config(config) {
8
- const root = config.root ?? process.cwd();
9
- const rscWorkerPath = typeof options.rscWorkerPath === "string"
10
- ? resolve(root, options.rscWorkerPath)
11
- : resolve(pluginRoot, DEFAULT_CONFIG.RSC_WORKER_PATH);
12
- const format = "esm";
13
- // Single worker output for server build
14
- const workerConfig = {
15
- input: {
16
- "rsc-worker": rscWorkerPath,
17
- },
18
- output: {
19
- format,
20
- dir: options.build?.server ?? "dist/server", // Output to server directory
21
- entryFileNames: "[name].js",
22
- preserveModules: true,
23
- manualChunks: {
24
- "rsc-worker": [rscWorkerPath],
25
- },
26
- resolve: {
27
- conditions: ["react-server"],
28
- },
29
- },
30
- };
31
- return {
32
- build: {
33
- rollupOptions: {
34
- preserveEntrySignatures: "strict",
35
- input: {
36
- ...workerConfig.input,
37
- ...(typeof config.build?.rollupOptions?.input === "object"
38
- ? config.build?.rollupOptions?.input
39
- : {}),
40
- },
41
- external: [
42
- "vite",
43
- "rollup",
44
- "react",
45
- "react-dom",
46
- "react-dom/server",
47
- "react-server-dom-esm",
48
- "react-server-dom-esm/client.node",
49
- "react-server-dom-esm/server.node",
50
- "react-server-dom-esm/node-loader",
51
- "source-map",
52
- "acorn-loose",
53
- "webpack-sources",
54
- "stream",
55
- "util",
56
- "crypto",
57
- "async_hooks",
58
- "fs",
59
- "path",
60
- "worker_threads",
61
- // if we use node: paths in our code, it should always be catched by below rule.
62
- /^node:.*/,
63
- ],
64
- output: {
65
- ...workerConfig.output,
66
- },
67
- },
68
- manifest: true, // Ensure manifest is generated
69
- minify: false,
70
- sourcemap: true,
71
- },
72
- };
73
- },
74
- };
75
- }
@@ -1,59 +0,0 @@
1
- import React from "react";
2
- /**
3
- * A component that emits <link> tags for CSS files during streaming.
4
- * The high precedence ensures they bubble up to the document head.
5
- */
6
- export function CssCollector({
7
- children,
8
- cssFiles,
9
- moduleBaseUrl,
10
- route = "/"
11
- }: {
12
- children?: React.ReactNode;
13
- cssFiles: string[];
14
- moduleBaseUrl?: string;
15
- route?: string;
16
- }) {
17
- // Calculate depth and prefix based on route
18
- const depth = route.split('/').filter(Boolean).length ;
19
- const prefix = depth > 0 ? '../'.repeat(depth) : './';
20
- const base = typeof moduleBaseUrl === 'string' && moduleBaseUrl !== '' ? moduleBaseUrl : prefix;
21
-
22
- return React.createElement(
23
- React.Fragment,
24
- null,
25
- ...cssFiles.map((css) => {
26
- try {
27
- if(moduleBaseUrl) {
28
- new URL(css, moduleBaseUrl);
29
- } else {
30
- new URL(`file://${base}${css}`);
31
- }
32
- } catch (error) {
33
- return React.createElement('style', {type: 'text/css'},
34
- css
35
- );
36
- }
37
- // Handle different types of paths
38
- let url = css;
39
- if (css.startsWith('http') || css.startsWith('data:')) {
40
- // Keep absolute URLs as is
41
- url = css;
42
- } else if (css.startsWith('/')) {
43
- // Convert absolute paths to relative
44
- url = base + css.slice(1);
45
- } else if (!css.startsWith('./') && !css.startsWith('../')) {
46
- // Add prefix to relative paths that don't start with ./ or ../
47
- url = base + css;
48
- }
49
-
50
- return React.createElement("link", {
51
- key: css,
52
- rel: "stylesheet",
53
- href: url,
54
- precedence: "high",
55
- });
56
- }),
57
- children
58
- );
59
- }
package/plugin/getEnv.ts DELETED
@@ -1,135 +0,0 @@
1
- import { readFileSync } from "node:fs";
2
- import { resolve } from "node:path";
3
- import type { ConfigEnv, UserConfig } from "vite";
4
- import { loadEnv } from "vite";
5
- import { DEFAULT_CONFIG } from "./config/defaults.js";
6
-
7
- /**
8
- * Get environment variables for Vite, sets defaults to ensure the server can start with BASE_URL and PUBLIC_URL
9
- *
10
- * @param config - Vite configuration object
11
- * @param { isPreview: boolean } - Object containing a boolean indicating if the environment is for preview
12
- * @returns An object containing the environment variables
13
- */
14
- export function getEnv(config: UserConfig, configEnv: ConfigEnv) {
15
- const isLocal =
16
- config.mode === "development"
17
-
18
- const envName = isLocal
19
- ? `${config.mode}.local`
20
- : config.mode
21
- ? config.mode
22
- : "production";
23
- const environmentName = config.mode ?? envName ?? "production";
24
-
25
- const env = loadEnv(
26
- environmentName,
27
- config.envDir ?? config.root ?? process.cwd(),
28
- config.envPrefix ?? DEFAULT_CONFIG.ENV_PREFIX ?? "VITE_"
29
- );
30
-
31
- // Get server config
32
- const serverConfig = config.server || {};
33
- const previewConfig = config.preview || {};
34
- const host = configEnv.isPreview
35
- ? previewConfig.host ?? DEFAULT_CONFIG.PREVIEW_HOST
36
- : serverConfig.host ?? DEFAULT_CONFIG.DEV_HOST;
37
- let previewPort = previewConfig.port ?? DEFAULT_CONFIG.PREVIEW_PORT;
38
- let devPort = serverConfig.port ?? DEFAULT_CONFIG.DEV_PORT;
39
-
40
- let homepage = env["VITE_BASE_URL"]
41
- if (configEnv.command === "build" && (!homepage || homepage === "")) {
42
- try {
43
- const packageJson = JSON.parse(
44
- readFileSync(resolve(config.root ?? "", "package.json"), "utf-8")
45
- );
46
- homepage = packageJson.homepage ?? "";
47
- if (!homepage || homepage === "") {
48
- console.warn(
49
- "[RSC] 🔧 For production builds, please set 'homepage' in package.json, or set VITE_BASE_URL in your environment"
50
- );
51
- }
52
- } catch (e) {
53
- console.error(e);
54
- }
55
- }
56
-
57
- let baseUrl =
58
- env["VITE_BASE_URL"] && env["VITE_PUBLIC_URL"] !== ""
59
- ? env["VITE_BASE_URL"]
60
- : configEnv.isPreview
61
- ? `http://${host}:${previewPort}`
62
- : configEnv.command === "serve"
63
- ? `http://${host}:${devPort}`
64
- : homepage;
65
-
66
- let publicUrl =
67
- env["VITE_PUBLIC_URL"] && env["VITE_PUBLIC_URL"] !== ""
68
- ? env["VITE_PUBLIC_URL"]
69
- : "";
70
-
71
- // Determine port and host based on mode
72
- const port = configEnv.isPreview
73
- ? previewConfig.port || DEFAULT_CONFIG.PREVIEW_PORT // Preview server
74
- : serverConfig.port || DEFAULT_CONFIG.DEV_PORT; // Dev server
75
-
76
- // Build base URL
77
- if (configEnv.isPreview && `http://${host}:${port}` !== baseUrl) {
78
- console.log(
79
- `VITE_BASE_URL: \"${baseUrl}\" wasn't configured correctly for this server, overriding to: \"http://${host}:${port}\"`
80
- );
81
- baseUrl = `http://${host}:${port}`;
82
- }
83
-
84
- const envPrefix =
85
- typeof config.envPrefix === "string"
86
- ? config.envPrefix
87
- : Array.isArray(config.envPrefix)
88
- ? config.envPrefix[0]
89
- : DEFAULT_CONFIG.ENV_PREFIX;
90
-
91
- const nodeProcessEnv = {
92
- NODE_ENV: configEnv.command === "build" ? "production" : "development",
93
- };
94
- const defineProcess = Object.entries(nodeProcessEnv)
95
- .map(([key, value]) => {
96
- switch (key) {
97
- case "NODE_ENV":
98
- const isDev =
99
- value === ""
100
- ? configEnv.command === "build"
101
- ? false
102
- : true
103
- : value === "development";
104
- return [`import.meta.env.DEV`, JSON.stringify(isDev)];
105
- default:
106
- return null;
107
- }
108
- })
109
- .filter(Array.isArray);
110
-
111
- const defineImportMeta = Object.entries(env).map(([key, value]) => [
112
- `import.meta.env.${key}`,
113
- key === "VITE_BASE_URL"
114
- ? value
115
- ? JSON.stringify(value)
116
- : JSON.stringify(baseUrl)
117
- : key === "VITE_PUBLIC_URL"
118
- ? value
119
- ? JSON.stringify(value)
120
- : JSON.stringify(publicUrl)
121
- : JSON.stringify(value),
122
- ]);
123
- const define = Object.fromEntries([...defineProcess, ...defineImportMeta]);
124
-
125
- return {
126
- baseUrl,
127
- publicUrl,
128
- port,
129
- host,
130
- envPrefix,
131
- environmentName,
132
- env,
133
- define,
134
- };
135
- }
@@ -1,48 +0,0 @@
1
- import type { ModuleNode, ViteDevServer } from "vite";
2
-
3
- export interface ModuleWithDeps {
4
- id: string;
5
- deps: Set<string>;
6
- css: Set<string>;
7
- }
8
-
9
- export function getModuleGraph(server: ViteDevServer) {
10
- return {
11
- async getModuleWithDeps(id: string): Promise<ModuleWithDeps> {
12
- // Load module first to ensure it's in the module graph
13
- await server.ssrLoadModule(id);
14
-
15
- const resolvedId = await server.pluginContainer.resolveId(id, undefined, {
16
- // Add ssr and react-server conditions
17
- ssr: true,
18
- });
19
-
20
- if (!resolvedId) throw new Error(`Module not found: ${id}`);
21
-
22
- const moduleNode = server.moduleGraph.getModuleById(resolvedId.id);
23
- if (!moduleNode) throw new Error(`Module node not found: ${id}`);
24
-
25
- const deps = new Set<string>();
26
- const css = new Set<string>();
27
-
28
- // Recursively collect dependencies
29
- const collectDeps = (node: ModuleNode) => {
30
- // Track CSS imports
31
- if (node.id?.endsWith(".css") && node.id) {
32
- css.add(node.id);
33
- }
34
-
35
- // Track all dependencies
36
- for (const dep of node.importedModules) {
37
- if (dep.id && !deps.has(dep.id)) {
38
- deps.add(dep.id);
39
- collectDeps(dep);
40
- }
41
- }
42
- };
43
-
44
- collectDeps(moduleNode);
45
- return { id: moduleNode.id ?? "", deps, css };
46
- },
47
- };
48
- }
@@ -1,100 +0,0 @@
1
- import type { Plugin, UserConfig } from "vite";
2
- import { join, resolve } from "path";
3
- import type { ModuleFormat, RollupOptions } from 'rollup';
4
- import type { ResolvedUserOptions, StreamPluginOptions } from "../../types.js";
5
- import { DEFAULT_CONFIG } from "../../config/defaults.js";
6
- import { pluginRoot } from "../../root.js";
7
- import { resolveOptions } from "../../config/resolveOptions.js";
8
-
9
- let userOptions: ResolvedUserOptions;
10
- export function reactHtmlWorkerPlugin(options: StreamPluginOptions): Plugin {
11
- const resolvedUserOptions = resolveOptions(options, false);
12
- if(resolvedUserOptions.type === 'error') {
13
- throw resolvedUserOptions.error
14
- }
15
- userOptions = resolvedUserOptions.userOptions;
16
- return {
17
- name: "vite:react-html-worker",
18
- config(config) {
19
- const root = config.root ?? process.cwd();
20
- const htmlWorkerPath = typeof options.htmlWorkerPath === 'string'
21
- ? resolve(root, options.htmlWorkerPath)
22
- : resolve(pluginRoot, DEFAULT_CONFIG.HTML_WORKER_PATH);
23
-
24
- const format: ModuleFormat = 'esm';
25
-
26
- // Single worker output for server build
27
- const workerConfig = {
28
- input: {
29
- 'html-worker': htmlWorkerPath,
30
- },
31
- output: {
32
- format,
33
- dir: join(userOptions.build.outDir, userOptions.build.server),
34
- entryFileNames: '[name].js',
35
- preserveModules: true,
36
- preserveModulesRoot: userOptions.build.preserveModulesRoot === true ? userOptions.moduleBase : undefined,
37
- // Add manifest entry
38
- manualChunks: {
39
- 'html-worker': [htmlWorkerPath]
40
- }
41
- }
42
- } satisfies RollupOptions;
43
-
44
- return {
45
- build: {
46
- rollupOptions: {
47
- preserveEntrySignatures: 'strict',
48
- input: {
49
- ...workerConfig.input,
50
- ...(typeof config.build?.rollupOptions?.input === 'object'
51
- ? config.build?.rollupOptions?.input
52
- : {}),
53
- },
54
- external: [
55
- 'vite',
56
- 'rollup',
57
- 'react',
58
- 'react-dom',
59
- 'react-dom/server',
60
- 'react-server-dom-esm',
61
- 'react-server-dom-esm/client.node',
62
- 'react-server-dom-esm/server.node',
63
- 'react-server-dom-esm/node-loader',
64
- 'source-map',
65
- 'acorn-loose',
66
- 'webpack-sources',
67
- 'stream',
68
- 'util',
69
- 'crypto',
70
- 'async_hooks',
71
- 'fs',
72
- 'path',
73
- 'worker_threads',
74
- // if we use node: paths in our code, it should always be catched by below rule.
75
- /^node:.*/,
76
- ],
77
- output: {
78
- ...workerConfig.output,
79
- }
80
- },
81
- manifest: true, // Ensure manifest is generated
82
- minify: false,
83
- sourcemap: true,
84
- }
85
- } satisfies UserConfig;
86
- },
87
- // Add this to ensure entry is in manifest
88
- generateBundle(_, bundle) {
89
- const workerEntry = bundle['html-worker.js'];
90
- if (workerEntry) {
91
- Object.defineProperty(workerEntry, 'isEntry', {
92
- value: true,
93
- writable: false,
94
- enumerable: true,
95
- configurable: false
96
- });
97
- }
98
- }
99
- };
100
- }
@@ -1,26 +0,0 @@
1
- import type { StreamPluginOptions } from "../types.js"
2
- import type { Plugin } from "vite"
3
- import { reactHtmlWorkerPlugin } from "./html/plugin.js"
4
- import { reactRscWorkerPlugin } from "./rsc/plugin.js"
5
-
6
- /**
7
- * This plugin can be used to create your own worker paths. This build should be separated from the main build.
8
- *
9
- *
10
- * ```ts
11
- * @example
12
- *export reactWorkerPluginConfig = {
13
- * htmlWorkerPath: './workers/html.tsx',
14
- * rscWorkerPath: './workers/rsc.tsx',
15
- * }
16
- * ```
17
- *
18
- * @param options
19
- * @returns
20
- */
21
- export function reactWorkerPlugin(options: StreamPluginOptions): Plugin[] {
22
- return [
23
- reactHtmlWorkerPlugin(options),
24
- reactRscWorkerPlugin(options)
25
- ]
26
- }
@@ -1,83 +0,0 @@
1
- import type { Plugin } from "vite";
2
- import { resolve } from "path";
3
- import type { ModuleFormat } from "rollup";
4
- import type { StreamPluginOptions } from "../../types.js";
5
- import { DEFAULT_CONFIG } from "../../config/defaults.js";
6
- import { pluginRoot } from "../../root.js";
7
-
8
- export function reactRscWorkerPlugin(options: StreamPluginOptions): Plugin {
9
- return {
10
- name: "vite:react-rsc-worker",
11
- config(config) {
12
- const root = config.root ?? process.cwd();
13
- const rscWorkerPath =
14
- typeof options.rscWorkerPath === "string"
15
- ? resolve(root, options.rscWorkerPath)
16
- : resolve(pluginRoot, DEFAULT_CONFIG.RSC_WORKER_PATH);
17
-
18
- const format: ModuleFormat = "esm";
19
-
20
- // Single worker output for server build
21
- const workerConfig = {
22
- input: {
23
- "rsc-worker": rscWorkerPath,
24
- },
25
- output: {
26
- format,
27
- dir: options.build?.server ?? "dist/server", // Output to server directory
28
- entryFileNames: "[name].js",
29
- preserveModules: true,
30
- manualChunks: {
31
- "rsc-worker": [rscWorkerPath],
32
- },
33
- resolve: {
34
- conditions: ["react-server"],
35
- },
36
- },
37
- };
38
-
39
- return {
40
- build: {
41
- rollupOptions: {
42
- preserveEntrySignatures: "strict",
43
- input: {
44
- ...workerConfig.input,
45
- ...(typeof config.build?.rollupOptions?.input === "object"
46
- ? config.build?.rollupOptions?.input
47
- : {}),
48
- },
49
- external: [
50
- "vite",
51
- "rollup",
52
- "react",
53
- "react-dom",
54
- "react-dom/server",
55
- "react-server-dom-esm",
56
- "react-server-dom-esm/client.node",
57
- "react-server-dom-esm/server.node",
58
- "react-server-dom-esm/node-loader",
59
- "source-map",
60
- "acorn-loose",
61
- "webpack-sources",
62
- "stream",
63
- "util",
64
- "crypto",
65
- "async_hooks",
66
- "fs",
67
- "path",
68
- "worker_threads",
69
- // if we use node: paths in our code, it should always be catched by below rule.
70
- /^node:.*/,
71
- ],
72
- output: {
73
- ...workerConfig.output,
74
- },
75
- },
76
- manifest: true, // Ensure manifest is generated
77
- minify: false,
78
- sourcemap: true,
79
- },
80
- };
81
- },
82
- };
83
- }