vite-plugin-react-server 0.3.10 → 0.3.12
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.
- package/dist/client.d.ts +1 -1
- package/dist/client.d.ts.map +1 -1
- package/dist/client.js +1 -1
- package/dist/index.d.ts +3 -6
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +2 -2
- package/dist/index.js.map +1 -1
- package/dist/package.json +16 -14
- package/dist/plugin/assertServerCondition.d.ts +5 -1
- package/dist/plugin/assertServerCondition.d.ts.map +1 -1
- package/dist/plugin/assertServerCondition.js +1 -3
- package/dist/plugin/checkFilesExist.d.ts +2 -2
- package/dist/plugin/checkFilesExist.d.ts.map +1 -1
- package/dist/plugin/checkFilesExist.js +43 -58
- package/dist/plugin/checkFilesExist.js.map +1 -1
- package/dist/plugin/collect-css-manifest.d.ts.map +1 -1
- package/dist/plugin/collect-css-manifest.js +5 -0
- package/dist/plugin/collect-css-manifest.js.map +1 -1
- package/dist/plugin/components.js +10 -15
- package/dist/plugin/config/createModuleIdGenerator.d.ts +11 -0
- package/dist/plugin/config/createModuleIdGenerator.d.ts.map +1 -0
- package/dist/plugin/config/createModuleIdGenerator.js +44 -0
- package/dist/plugin/config/createModuleIdGenerator.js.map +1 -0
- package/dist/plugin/config/defaults.d.ts +27 -24
- package/dist/plugin/config/defaults.d.ts.map +1 -1
- package/dist/plugin/config/defaults.js +26 -31
- package/dist/plugin/config/defaults.js.map +1 -1
- package/dist/plugin/config/getPaths.js +1 -1
- package/dist/plugin/config/resolveOptions.d.ts +1 -1
- package/dist/plugin/config/resolveOptions.d.ts.map +1 -1
- package/dist/plugin/config/resolveOptions.js +223 -16
- package/dist/plugin/config/resolveOptions.js.map +1 -1
- package/dist/plugin/config/resolvePages.d.ts +2 -0
- package/dist/plugin/config/resolvePages.d.ts.map +1 -1
- package/dist/plugin/config/resolvePages.js.map +1 -1
- package/dist/plugin/config/resolveUserConfig.d.ts +2 -2
- package/dist/plugin/config/resolveUserConfig.d.ts.map +1 -1
- package/dist/plugin/config/resolveUserConfig.js +149 -50
- package/dist/plugin/config/resolveUserConfig.js.map +1 -1
- package/dist/plugin/helpers/getBundleManifest.d.ts +25 -0
- package/dist/plugin/helpers/getBundleManifest.d.ts.map +1 -0
- package/dist/plugin/helpers/getBundleManifest.js +72 -0
- package/dist/plugin/helpers/getBundleManifest.js.map +1 -0
- package/dist/plugin/helpers/inputNormalizer.d.ts +14 -1
- package/dist/plugin/helpers/inputNormalizer.d.ts.map +1 -1
- package/dist/plugin/helpers/inputNormalizer.js +128 -16
- package/dist/plugin/helpers/inputNormalizer.js.map +1 -1
- package/dist/plugin/helpers/tryManifest.d.ts +3 -1
- package/dist/plugin/helpers/tryManifest.d.ts.map +1 -1
- package/dist/plugin/helpers/tryManifest.js +1 -1
- package/dist/plugin/helpers/tryManifest.js.map +1 -1
- package/dist/plugin/loader/createBuildLoader.d.ts +6 -2
- package/dist/plugin/loader/createBuildLoader.d.ts.map +1 -1
- package/dist/plugin/loader/createBuildLoader.js +37 -9
- package/dist/plugin/loader/createBuildLoader.js.map +1 -1
- package/dist/plugin/loader/createPageLoader.d.ts.map +1 -1
- package/dist/plugin/loader/createPageLoader.js +0 -7
- package/dist/plugin/loader/css-loader.d.ts +16 -0
- package/dist/plugin/loader/css-loader.d.ts.map +1 -0
- package/dist/plugin/loader/css-loader.js +70 -0
- package/dist/plugin/loader/css-loader.js.map +1 -0
- package/dist/plugin/loader/react-loader.d.ts +17 -0
- package/dist/plugin/loader/react-loader.d.ts.map +1 -0
- package/dist/plugin/loader/react-loader.js +647 -0
- package/dist/plugin/loader/react-loader.js.map +1 -0
- package/dist/plugin/loader/rsc/messageHandler.d.ts +2 -0
- package/dist/plugin/loader/rsc/messageHandler.d.ts.map +1 -0
- package/dist/plugin/loader/rsc/messageHandler.js +1 -0
- package/dist/plugin/loader/rsc/rsc-worker.development.d.ts +2 -0
- package/dist/plugin/loader/rsc/rsc-worker.development.d.ts.map +1 -0
- package/dist/plugin/loader/rsc/rsc-worker.development.js +1 -0
- package/dist/plugin/plugin.d.ts +0 -1
- package/dist/plugin/plugin.d.ts.map +1 -1
- package/dist/plugin/plugin.js +2 -1
- package/dist/plugin/preserver/plugin.d.ts.map +1 -1
- package/dist/plugin/preserver/plugin.js +3 -2
- package/dist/plugin/preserver/plugin.js.map +1 -1
- package/dist/plugin/react-client/index.d.ts +2 -1
- package/dist/plugin/react-client/index.d.ts.map +1 -1
- package/dist/plugin/react-client/index.js +19 -1
- package/dist/plugin/react-client/index.js.map +1 -0
- package/dist/plugin/react-client/plugin.d.ts +2 -2
- package/dist/plugin/react-client/plugin.d.ts.map +1 -1
- package/dist/plugin/react-client/plugin.js +283 -10
- package/dist/plugin/react-client/plugin.js.map +1 -1
- package/dist/plugin/react-server/createHandler.d.ts +2 -2
- package/dist/plugin/react-server/createHandler.d.ts.map +1 -1
- package/dist/plugin/react-server/createHandler.js +15 -9
- package/dist/plugin/react-server/createHandler.js.map +1 -1
- package/dist/plugin/react-server/createRscStream.d.ts +15 -3
- package/dist/plugin/react-server/createRscStream.d.ts.map +1 -1
- package/dist/plugin/react-server/createRscStream.js +53 -36
- package/dist/plugin/react-server/createRscStream.js.map +1 -1
- package/dist/plugin/react-server/createSsrHandler.d.ts +2 -2
- package/dist/plugin/react-server/createSsrHandler.d.ts.map +1 -1
- package/dist/plugin/react-server/createSsrHandler.js +5 -12
- package/dist/plugin/react-server/index.js +18 -9
- package/dist/plugin/react-server/index.js.map +1 -0
- package/dist/plugin/react-server/plugin.d.ts.map +1 -1
- package/dist/plugin/react-server/plugin.js +147 -137
- package/dist/plugin/react-server/plugin.js.map +1 -1
- package/dist/plugin/transformer/plugin.d.ts +2 -1
- package/dist/plugin/transformer/plugin.d.ts.map +1 -1
- package/dist/plugin/transformer/plugin.js +68 -75
- package/dist/plugin/transformer/plugin.js.map +1 -1
- package/dist/plugin/transformer/types.d.ts +4 -0
- package/dist/plugin/transformer/types.d.ts.map +1 -1
- package/dist/plugin/types.d.ts +56 -20
- package/dist/plugin/types.d.ts.map +1 -1
- package/dist/plugin/utils/logger.d.ts +9 -0
- package/dist/plugin/utils/logger.d.ts.map +1 -0
- package/dist/plugin/utils/logger.js +68 -0
- package/dist/plugin/utils/logger.js.map +1 -0
- package/dist/plugin/worker/createWorker.d.ts +1 -0
- package/dist/plugin/worker/createWorker.d.ts.map +1 -1
- package/dist/plugin/worker/createWorker.js +23 -36
- package/dist/plugin/worker/createWorker.js.map +1 -1
- package/dist/plugin/worker/html/html-worker.production.js +5 -1
- package/dist/plugin/worker/html/html-worker.production.js.map +1 -1
- package/dist/plugin/worker/html/messageHandler.d.ts.map +1 -1
- package/dist/plugin/worker/html/messageHandler.js +33 -28
- package/dist/plugin/worker/html/messageHandler.js.map +1 -1
- package/dist/plugin/worker/html/plugin.d.ts.map +1 -1
- package/dist/plugin/worker/html/plugin.js +10 -5
- package/dist/plugin/worker/html/renderPages.d.ts +7 -6
- package/dist/plugin/worker/html/renderPages.d.ts.map +1 -1
- package/dist/plugin/worker/html/renderPages.js +147 -93
- package/dist/plugin/worker/html/renderPages.js.map +1 -1
- package/dist/plugin/worker/loader.d.ts +1 -11
- package/dist/plugin/worker/loader.d.ts.map +1 -1
- package/dist/plugin/worker/loader.js +2 -2
- package/dist/plugin/worker/loader.js.map +1 -1
- package/dist/plugin/worker/plugin.d.ts +10 -1
- package/dist/plugin/worker/plugin.d.ts.map +1 -1
- package/dist/plugin/worker/plugin.js +10 -1
- package/dist/plugin/worker/rsc/index.d.ts +1 -3
- package/dist/plugin/worker/rsc/index.d.ts.map +1 -1
- package/dist/plugin/worker/rsc/index.js +1 -9
- package/dist/plugin/worker/rsc/index.js.map +1 -1
- package/dist/plugin/worker/rsc/messageHandler.d.ts +3 -0
- package/dist/plugin/worker/rsc/messageHandler.d.ts.map +1 -0
- package/dist/plugin/worker/rsc/messageHandler.js +107 -0
- package/dist/plugin/worker/rsc/messageHandler.js.map +1 -0
- package/dist/plugin/worker/rsc/plugin.d.ts.map +1 -1
- package/dist/plugin/worker/rsc/plugin.js +74 -80
- package/dist/plugin/worker/rsc/rsc-worker.development.d.ts +32 -0
- package/dist/plugin/worker/rsc/rsc-worker.development.d.ts.map +1 -0
- package/dist/plugin/worker/rsc/rsc-worker.development.js +43 -0
- package/dist/plugin/worker/rsc/rsc-worker.development.js.map +1 -0
- package/dist/plugin/worker/rsc/rsc-worker.js +4 -106
- package/dist/plugin/worker/rsc/rsc-worker.production.d.ts +2 -0
- package/dist/plugin/worker/rsc/rsc-worker.production.d.ts.map +1 -0
- package/dist/plugin/worker/rsc/rsc-worker.production.js +14 -0
- package/dist/plugin/worker/rsc/rsc-worker.production.js.map +1 -0
- package/dist/plugin/worker/rsc/state.d.ts +11 -0
- package/dist/plugin/worker/rsc/state.d.ts.map +1 -0
- package/dist/plugin/worker/rsc/state.js +12 -0
- package/dist/plugin/worker/rsc/state.js.map +1 -0
- package/dist/plugin/worker/types.d.ts +62 -35
- package/dist/plugin/worker/types.d.ts.map +1 -1
- package/dist/server.d.ts +1 -3
- package/dist/server.d.ts.map +1 -1
- package/dist/server.js +1 -3
- package/dist/server.js.map +1 -1
- package/dist/tsconfig.tsbuildinfo +1 -1
- package/package.json +16 -14
- package/plugin/assertServerCondition.ts +2 -3
- package/plugin/checkFilesExist.ts +46 -66
- package/plugin/collect-css-manifest.ts +5 -1
- package/plugin/config/createModuleIdGenerator.ts +52 -0
- package/plugin/config/defaults.ts +27 -20
- package/plugin/config/resolveOptions.ts +311 -25
- package/plugin/config/resolvePages.ts +1 -1
- package/plugin/config/resolveUserConfig.ts +195 -61
- package/plugin/helpers/getBundleManifest.ts +113 -0
- package/plugin/helpers/inputNormalizer.ts +186 -25
- package/plugin/helpers/tryManifest.ts +3 -1
- package/plugin/loader/createBuildLoader.ts +50 -9
- package/plugin/loader/createPageLoader.ts +1 -7
- package/plugin/loader/css-loader.ts +96 -0
- package/plugin/loader/react-loader.ts +945 -0
- package/plugin/loader/rsc/messageHandler.tsx +1 -0
- package/plugin/loader/rsc/rsc-worker.development.ts +1 -0
- package/plugin/plugin.ts +2 -1
- package/plugin/preserver/plugin.ts +2 -1
- package/plugin/react-client/index.ts +12 -1
- package/plugin/react-client/plugin.ts +351 -11
- package/plugin/react-server/createHandler.ts +16 -18
- package/plugin/react-server/createRscStream.ts +75 -42
- package/plugin/react-server/createSsrHandler.ts +7 -26
- package/plugin/react-server/plugin.ts +192 -155
- package/plugin/transformer/plugin.ts +70 -104
- package/plugin/transformer/types.ts +4 -0
- package/plugin/types/global.d.ts +8 -0
- package/plugin/types.ts +120 -64
- package/plugin/utils/logger.ts +52 -0
- package/plugin/worker/createWorker.ts +43 -44
- package/plugin/worker/html/html-worker.production.tsx +7 -2
- package/plugin/worker/html/messageHandler.ts +42 -35
- package/plugin/worker/html/plugin.ts +15 -11
- package/plugin/worker/html/renderPages.ts +177 -123
- package/plugin/worker/loader.ts +4 -13
- package/plugin/worker/plugin.ts +10 -1
- package/plugin/worker/rsc/index.ts +4 -13
- package/plugin/worker/rsc/messageHandler.tsx +143 -0
- package/plugin/worker/rsc/plugin.ts +38 -37
- package/plugin/worker/rsc/rsc-worker.development.ts +107 -0
- package/plugin/worker/rsc/rsc-worker.production.ts +13 -0
- package/plugin/worker/rsc/rsc-worker.tsx +5 -128
- package/plugin/worker/rsc/state.ts +37 -0
- package/plugin/worker/types.ts +83 -38
- package/scripts/check-react-version.mjs +17 -7
- package/scripts/react+0.0.0-experimental-b3a95caf-20250113.patch +143 -4170
- package/scripts/react-dom+0.0.0-experimental-b3a95caf-20250113.patch +14271 -90079
- package/dist/node_modules/magic-string/dist/magic-string.es.js +0 -1283
- package/dist/node_modules/magic-string/dist/magic-string.es.js.map +0 -1
- package/dist/plugin/build/createClientBuildConfig.d.ts +0 -3
- package/dist/plugin/build/createClientBuildConfig.d.ts.map +0 -1
- package/dist/plugin/build/createClientBuildConfig.js +0 -14
- package/dist/plugin/build/createServerBuildConfig.d.ts +0 -12
- package/dist/plugin/build/createServerBuildConfig.d.ts.map +0 -1
- package/dist/plugin/build/createServerBuildConfig.js +0 -40
- package/dist/plugin/build/createSharedBuildConfig.d.ts +0 -5
- package/dist/plugin/build/createSharedBuildConfig.d.ts.map +0 -1
- package/dist/plugin/build/createSharedBuildConfig.js +0 -28
- package/dist/plugin/build/mergeInputs.d.ts +0 -9
- package/dist/plugin/build/mergeInputs.d.ts.map +0 -1
- package/dist/plugin/build/mergeInputs.js +0 -56
- package/dist/plugin/components.js.map +0 -1
- package/dist/plugin/config/moduleIdDefault.d.ts +0 -8
- package/dist/plugin/config/moduleIdDefault.d.ts.map +0 -1
- package/dist/plugin/config/moduleIdDefault.js +0 -23
- package/dist/plugin/config/moduleIdDefault.js.map +0 -1
- package/dist/plugin/helpers/createClientInputNormalizer.d.ts +0 -8
- package/dist/plugin/helpers/createClientInputNormalizer.d.ts.map +0 -1
- package/dist/plugin/helpers/createClientInputNormalizer.js +0 -35
- package/dist/plugin/helpers/createServerInputNormalizer.d.ts +0 -9
- package/dist/plugin/helpers/createServerInputNormalizer.d.ts.map +0 -1
- package/dist/plugin/helpers/createServerInputNormalizer.js +0 -37
- package/dist/plugin/helpers/createStaticInputNormalizer.d.ts +0 -7
- package/dist/plugin/helpers/createStaticInputNormalizer.d.ts.map +0 -1
- package/dist/plugin/helpers/createStaticInputNormalizer.js +0 -18
- package/dist/plugin/helpers/getModuleManifest.d.ts +0 -12
- package/dist/plugin/helpers/getModuleManifest.d.ts.map +0 -1
- package/dist/plugin/helpers/getModuleManifest.js +0 -24
- package/dist/plugin/helpers/getModuleManifest.js.map +0 -1
- package/dist/plugin/helpers/inputNormalizerWorker.d.ts +0 -11
- package/dist/plugin/helpers/inputNormalizerWorker.d.ts.map +0 -1
- package/dist/plugin/helpers/inputNormalizerWorker.js +0 -30
- package/dist/plugin/helpers/normalizedRelativePath.d.ts +0 -11
- package/dist/plugin/helpers/normalizedRelativePath.d.ts.map +0 -1
- package/dist/plugin/helpers/normalizedRelativePath.js +0 -36
- package/dist/plugin/helpers/resolveFilePath.d.ts +0 -13
- package/dist/plugin/helpers/resolveFilePath.d.ts.map +0 -1
- package/dist/plugin/helpers/resolveFilePath.js +0 -74
- package/dist/plugin/helpers/resolveWorkerModule.d.ts +0 -6
- package/dist/plugin/helpers/resolveWorkerModule.d.ts.map +0 -1
- package/dist/plugin/helpers/resolveWorkerModule.js +0 -24
- package/dist/plugin/helpers/validateModuleBase.d.ts +0 -3
- package/dist/plugin/helpers/validateModuleBase.d.ts.map +0 -1
- package/dist/plugin/helpers/validateModuleBase.js +0 -16
- package/dist/plugin/helpers/validateResolvedConfig.d.ts +0 -3
- package/dist/plugin/helpers/validateResolvedConfig.d.ts.map +0 -1
- package/dist/plugin/helpers/validateResolvedConfig.js +0 -17
- package/dist/plugin/react-server/createDevMiddleware.d.ts +0 -8
- package/dist/plugin/react-server/createDevMiddleware.d.ts.map +0 -1
- package/dist/plugin/react-server/createDevMiddleware.js +0 -68
- package/dist/plugin/react-server/createDevServer.d.ts +0 -4
- package/dist/plugin/react-server/createDevServer.d.ts.map +0 -1
- package/dist/plugin/react-server/createDevServer.js +0 -4
- package/dist/plugin/react-server/createReactNodeStreamer.d.ts +0 -10
- package/dist/plugin/react-server/createReactNodeStreamer.d.ts.map +0 -1
- package/dist/plugin/react-server/createReactNodeStreamer.js +0 -7
- package/dist/plugin/transformer/transformer-client-components.d.ts +0 -21
- package/dist/plugin/transformer/transformer-client-components.d.ts.map +0 -1
- package/dist/plugin/transformer/transformer-client-components.js +0 -61
- package/dist/plugin/transformer/transformer-client-components.js.map +0 -1
- package/dist/plugin/transformer/transformer-server-actions.d.ts +0 -29
- package/dist/plugin/transformer/transformer-server-actions.d.ts.map +0 -1
- package/dist/plugin/transformer/transformer-server-actions.js +0 -76
- package/dist/plugin/transformer/transformer-server-actions.js.map +0 -1
- package/dist/plugin/worker/rsc/createRscStream.d.ts +0 -5
- package/dist/plugin/worker/rsc/createRscStream.d.ts.map +0 -1
- package/dist/plugin/worker/rsc/createRscStream.js +0 -39
- package/dist/plugin/worker/rsc/createRscStream.js.map +0 -1
- package/dist/plugin/worker/rsc/development.d.ts +0 -5
- package/dist/plugin/worker/rsc/development.d.ts.map +0 -1
- package/dist/plugin/worker/rsc/development.js +0 -13
- package/dist/plugin/worker/rsc/development.js.map +0 -1
- package/dist/plugin/worker/rsc/plugin.js.map +0 -1
- package/dist/plugin/worker/rsc/production.d.ts +0 -5
- package/dist/plugin/worker/rsc/production.d.ts.map +0 -1
- package/dist/plugin/worker/rsc/production.js +0 -13
- package/dist/plugin/worker/rsc/production.js.map +0 -1
- package/plugin/build/createClientBuildConfig.ts +0 -21
- package/plugin/build/createServerBuildConfig.ts +0 -66
- package/plugin/build/createSharedBuildConfig.ts +0 -35
- package/plugin/build/mergeInputs.ts +0 -58
- package/plugin/config/moduleIdDefault.ts +0 -23
- package/plugin/helpers/createClientInputNormalizer.ts +0 -48
- package/plugin/helpers/createServerInputNormalizer.ts +0 -52
- package/plugin/helpers/createStaticInputNormalizer.ts +0 -26
- package/plugin/helpers/getModuleManifest.ts +0 -31
- package/plugin/helpers/inputNormalizerWorker.ts +0 -47
- package/plugin/helpers/normalizedRelativePath.ts +0 -59
- package/plugin/helpers/resolveFilePath.ts +0 -108
- package/plugin/helpers/resolveWorkerModule.ts +0 -41
- package/plugin/helpers/validateModuleBase.ts +0 -30
- package/plugin/helpers/validateResolvedConfig.ts +0 -21
- package/plugin/react-server/createDevMiddleware.ts +0 -91
- package/plugin/react-server/createDevServer.ts +0 -9
- package/plugin/react-server/createReactNodeStreamer.ts +0 -26
- package/plugin/transformer/transformer-client-components.ts +0 -94
- package/plugin/transformer/transformer-server-actions.ts +0 -110
- package/plugin/worker/rsc/createRscStream.ts +0 -42
- package/plugin/worker/rsc/development.ts +0 -6
- package/plugin/worker/rsc/production.ts +0 -6
- package/scripts/react-server-dom-esm+0.0.1.patch +0 -24775
|
@@ -0,0 +1 @@
|
|
|
1
|
+
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
|
package/plugin/plugin.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import "./assertServerCondition.js";
|
|
1
|
+
import { assertServerCondition } from "./assertServerCondition.js";
|
|
2
2
|
export { reactServerPlugin } from "./react-server/plugin.js";
|
|
3
3
|
export { reactClientPlugin } from "./react-client/plugin.js";
|
|
4
4
|
export { reactTransformPlugin } from "./transformer/plugin.js";
|
|
@@ -6,3 +6,4 @@ export { reactPreservePlugin } from "./preserver/plugin.js";
|
|
|
6
6
|
export { vitePluginReactServer } from "./react-server/index.js";
|
|
7
7
|
// the main plugin is version is the server version, if you want the client version, use the `vite-plugin-react-server/client` import
|
|
8
8
|
// this is because the workflow assumes main thread = react server condition
|
|
9
|
+
assertServerCondition(process);
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import type { Node } from "estree";
|
|
2
2
|
import MagicString from "magic-string";
|
|
3
3
|
import type { StreamPluginOptions } from "../types.js";
|
|
4
|
+
import { DEFAULT_CONFIG } from "../config/defaults.js";
|
|
4
5
|
|
|
5
6
|
const REACT_DIRECTIVES = new Set(["use client", "use server"]);
|
|
6
7
|
|
|
@@ -15,7 +16,7 @@ export function reactPreservePlugin(_options: StreamPluginOptions): import("vite
|
|
|
15
16
|
order: "post", // Ensure this runs last in transform phase
|
|
16
17
|
handler(code: string, id: string) {
|
|
17
18
|
// Skip node_modules and vite files
|
|
18
|
-
if (id.includes("node_modules") || id.includes("vite/dist")) {
|
|
19
|
+
if (id.includes("node_modules") || id.includes("vite/dist") || !id.match(DEFAULT_CONFIG.FILE_REGEX)) {
|
|
19
20
|
return null;
|
|
20
21
|
}
|
|
21
22
|
|
|
@@ -1 +1,12 @@
|
|
|
1
|
-
|
|
1
|
+
import { reactPreservePlugin } from "../plugin.js";
|
|
2
|
+
import { reactTransformPlugin } from "../transformer/plugin.js";
|
|
3
|
+
import type { StreamPluginOptions } from "../types.js";
|
|
4
|
+
import { reactClientPlugin } from "./plugin.js";
|
|
5
|
+
|
|
6
|
+
export function vitePluginReactClient(options = {} as StreamPluginOptions): import("vite").Plugin[] {
|
|
7
|
+
return [
|
|
8
|
+
reactClientPlugin(options),
|
|
9
|
+
reactTransformPlugin(options),
|
|
10
|
+
reactPreservePlugin(options),
|
|
11
|
+
];
|
|
12
|
+
}
|
|
@@ -1,33 +1,373 @@
|
|
|
1
|
-
import type
|
|
2
|
-
|
|
3
|
-
|
|
1
|
+
import { type Manifest, type Plugin, type ResolvedConfig } from "vite";
|
|
2
|
+
import type {
|
|
3
|
+
CheckFilesExistReturn,
|
|
4
|
+
ResolvedUserConfig,
|
|
5
|
+
ResolvedUserOptions,
|
|
6
|
+
StreamPluginOptions,
|
|
7
|
+
} from "../types.js";
|
|
4
8
|
import { resolveOptions } from "../config/resolveOptions.js";
|
|
5
9
|
import { resolveUserConfig } from "../config/resolveUserConfig.js";
|
|
10
|
+
import { mkdir, readFile, stat, writeFile } from "node:fs/promises";
|
|
11
|
+
import { dirname, join } from "node:path";
|
|
12
|
+
import { getBundleManifest } from "../helpers/getBundleManifest.js";
|
|
13
|
+
import { checkFilesExist } from "../checkFilesExist.js";
|
|
14
|
+
import { resolvePages } from "../config/resolvePages.js";
|
|
15
|
+
import { tryManifest } from "../helpers/tryManifest.js";
|
|
16
|
+
import { createInputNormalizer } from "../helpers/inputNormalizer.js";
|
|
17
|
+
import { createWorker } from "../worker/createWorker.js";
|
|
18
|
+
import type { Worker } from "node:worker_threads";
|
|
19
|
+
import { getPluginRoot } from "../config/getPaths.js";
|
|
20
|
+
import { DEFAULT_CONFIG } from "../config/defaults.js";
|
|
21
|
+
import type {
|
|
22
|
+
RscRenderMessage,
|
|
23
|
+
RscWorkerMessage,
|
|
24
|
+
RscWorkerResponse,
|
|
25
|
+
} from "../worker/types.js";
|
|
26
|
+
import { createLogger } from "../utils/logger.js";
|
|
6
27
|
|
|
28
|
+
const log = createLogger("react-client");
|
|
7
29
|
let userOptions: ResolvedUserOptions;
|
|
30
|
+
let userConfig: ResolvedUserConfig;
|
|
31
|
+
let clientManifest: Manifest = {};
|
|
32
|
+
let resolvedConfig: ResolvedConfig;
|
|
33
|
+
let root: string;
|
|
34
|
+
let loader: (id: string) => Promise<Record<string, any>> = (id: string) =>
|
|
35
|
+
import(id);
|
|
36
|
+
let worker: Worker;
|
|
37
|
+
let files: CheckFilesExistReturn;
|
|
8
38
|
|
|
9
|
-
export function reactClientPlugin(options: StreamPluginOptions):
|
|
10
|
-
const resolvedOptions = resolveOptions(options);
|
|
39
|
+
export function reactClientPlugin(options: StreamPluginOptions): Plugin {
|
|
40
|
+
const resolvedOptions = resolveOptions(options, true);
|
|
11
41
|
if (resolvedOptions.type === "error") {
|
|
12
42
|
throw resolvedOptions.error;
|
|
13
43
|
}
|
|
14
44
|
userOptions = resolvedOptions.userOptions;
|
|
45
|
+
root = userOptions.projectRoot;
|
|
46
|
+
const rscWorkerPath = join(getPluginRoot(), DEFAULT_CONFIG.RSC_WORKER_PATH);
|
|
47
|
+
|
|
48
|
+
log.info("RSC worker path:" + rscWorkerPath);
|
|
15
49
|
|
|
16
50
|
return {
|
|
17
|
-
name: "vite:react-
|
|
18
|
-
|
|
51
|
+
name: "vite:react-client",
|
|
52
|
+
|
|
53
|
+
async config(config, configEnv) {
|
|
54
|
+
if (
|
|
55
|
+
typeof config.root === "string" &&
|
|
56
|
+
config.root !== root &&
|
|
57
|
+
config.root !== process.cwd() &&
|
|
58
|
+
config.root !== ""
|
|
59
|
+
) {
|
|
60
|
+
root = config.root;
|
|
61
|
+
console.log("[vite:react-client] Root updated:", root);
|
|
62
|
+
}
|
|
63
|
+
if (configEnv.command === "serve" && !configEnv.isPreview && !worker) {
|
|
64
|
+
worker = await createWorker({
|
|
65
|
+
projectRoot: root,
|
|
66
|
+
workerPath: rscWorkerPath,
|
|
67
|
+
reverseCondition: true,
|
|
68
|
+
});
|
|
69
|
+
}
|
|
70
|
+
const pages = await resolvePages(userOptions.build.pages);
|
|
71
|
+
if (pages.type === "error") {
|
|
72
|
+
throw pages.error;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
if (pages.pages.length > 0) {
|
|
76
|
+
files = await checkFilesExist(pages.pages, userOptions, root);
|
|
77
|
+
} else {
|
|
78
|
+
files = {
|
|
79
|
+
pageMap: new Map(),
|
|
80
|
+
propsMap: new Map(),
|
|
81
|
+
propsSet: new Set(),
|
|
82
|
+
pageSet: new Set(),
|
|
83
|
+
urlMap: new Map(),
|
|
84
|
+
errors: [],
|
|
85
|
+
};
|
|
86
|
+
}
|
|
87
|
+
|
|
19
88
|
const resolvedConfig = resolveUserConfig({
|
|
20
|
-
|
|
89
|
+
isClient: true,
|
|
21
90
|
config,
|
|
22
|
-
configEnv
|
|
91
|
+
configEnv,
|
|
23
92
|
userOptions,
|
|
93
|
+
files,
|
|
24
94
|
});
|
|
25
95
|
|
|
26
96
|
if (resolvedConfig.type === "error") {
|
|
27
97
|
throw resolvedConfig.error;
|
|
28
98
|
}
|
|
29
99
|
|
|
30
|
-
|
|
31
|
-
|
|
100
|
+
userConfig = resolvedConfig.userConfig;
|
|
101
|
+
return userConfig;
|
|
102
|
+
},
|
|
103
|
+
|
|
104
|
+
configResolved(config) {
|
|
105
|
+
resolvedConfig = config;
|
|
106
|
+
},
|
|
107
|
+
|
|
108
|
+
async generateBundle(options, bundle) {
|
|
109
|
+
// Create manifest entries for each chunk
|
|
110
|
+
clientManifest = getBundleManifest({
|
|
111
|
+
pluginContext: this,
|
|
112
|
+
bundle,
|
|
113
|
+
moduleBase: userOptions.moduleBase,
|
|
114
|
+
preserveModulesRoot: userOptions.build.preserveModulesRoot,
|
|
115
|
+
});
|
|
116
|
+
|
|
117
|
+
// Write manifest immediately after generation
|
|
118
|
+
const manifestPath = join(
|
|
119
|
+
root,
|
|
120
|
+
resolvedConfig.environments["client"].build.outDir as string,
|
|
121
|
+
resolvedConfig.environments["client"].build.manifest as string
|
|
122
|
+
);
|
|
123
|
+
await mkdir(dirname(manifestPath), { recursive: true });
|
|
124
|
+
|
|
125
|
+
return await writeFile(
|
|
126
|
+
manifestPath,
|
|
127
|
+
JSON.stringify(clientManifest, null, 2)
|
|
128
|
+
);
|
|
129
|
+
},
|
|
130
|
+
|
|
131
|
+
async configurePreviewServer(server) {
|
|
132
|
+
if (root !== server.config.root) {
|
|
133
|
+
root = server.config.root;
|
|
134
|
+
}
|
|
135
|
+
if (typeof loader !== "function") {
|
|
136
|
+
loader = (id: string) => import(id);
|
|
137
|
+
}
|
|
138
|
+
const normalize = createInputNormalizer({
|
|
139
|
+
root,
|
|
140
|
+
removeExtension: false,
|
|
141
|
+
preserveModulesRoot: userOptions.build.preserveModulesRoot
|
|
142
|
+
? userOptions.moduleBase
|
|
143
|
+
: undefined,
|
|
144
|
+
});
|
|
145
|
+
server.middlewares.use(async (req, res, next) => {
|
|
146
|
+
const [key, value] = normalize(req.url);
|
|
147
|
+
const fileRoot = key.startsWith("node_modules")
|
|
148
|
+
? root
|
|
149
|
+
: join(root, userOptions.build.outDir, userOptions.build.client);
|
|
150
|
+
if (value.endsWith(".js")) {
|
|
151
|
+
try {
|
|
152
|
+
const stats = await stat(join(fileRoot, value));
|
|
153
|
+
if (stats.isFile()) {
|
|
154
|
+
const content = await readFile(join(fileRoot, value), "utf-8");
|
|
155
|
+
res.setHeader("Content-Type", "application/javascript");
|
|
156
|
+
res.end(content);
|
|
157
|
+
return;
|
|
158
|
+
} else {
|
|
159
|
+
next();
|
|
160
|
+
}
|
|
161
|
+
} catch (error) {
|
|
162
|
+
const { manifest: clientManifest } = tryManifest({
|
|
163
|
+
root,
|
|
164
|
+
outDir: join(userOptions.build.outDir, userOptions.build.client),
|
|
165
|
+
});
|
|
166
|
+
const { manifest: serverManifest } = tryManifest({
|
|
167
|
+
root,
|
|
168
|
+
outDir: join(userOptions.build.outDir, userOptions.build.server),
|
|
169
|
+
});
|
|
170
|
+
if (clientManifest && value in clientManifest) {
|
|
171
|
+
res.setHeader("Content-Type", "application/javascript");
|
|
172
|
+
res.end(clientManifest[value]);
|
|
173
|
+
return;
|
|
174
|
+
} else if (serverManifest && value in serverManifest) {
|
|
175
|
+
res.setHeader("Content-Type", "application/javascript");
|
|
176
|
+
res.end(serverManifest[value]);
|
|
177
|
+
return;
|
|
178
|
+
}
|
|
179
|
+
const foundClient =
|
|
180
|
+
clientManifest &&
|
|
181
|
+
Object.entries(clientManifest).find(
|
|
182
|
+
([key, value]) =>
|
|
183
|
+
value === key ||
|
|
184
|
+
value === value.file ||
|
|
185
|
+
value === value.src ||
|
|
186
|
+
value === value.name
|
|
187
|
+
);
|
|
188
|
+
if (foundClient) {
|
|
189
|
+
res.setHeader("Content-Type", "application/javascript");
|
|
190
|
+
res.end(foundClient);
|
|
191
|
+
}
|
|
192
|
+
const foundServer =
|
|
193
|
+
serverManifest &&
|
|
194
|
+
Object.entries(serverManifest).find(
|
|
195
|
+
([key, value]) =>
|
|
196
|
+
value === key ||
|
|
197
|
+
value === value.file ||
|
|
198
|
+
value === value.src ||
|
|
199
|
+
value === value.name
|
|
200
|
+
);
|
|
201
|
+
|
|
202
|
+
if (foundServer) {
|
|
203
|
+
res.setHeader("Content-Type", "application/javascript");
|
|
204
|
+
res.end(foundServer);
|
|
205
|
+
return;
|
|
206
|
+
}
|
|
207
|
+
next();
|
|
208
|
+
}
|
|
209
|
+
} else {
|
|
210
|
+
next();
|
|
211
|
+
}
|
|
212
|
+
});
|
|
213
|
+
},
|
|
214
|
+
// setup dev server
|
|
215
|
+
async configureServer(server) {
|
|
216
|
+
if (typeof loader !== "function") {
|
|
217
|
+
loader = server.ssrLoadModule;
|
|
218
|
+
}
|
|
219
|
+
if (!worker) {
|
|
220
|
+
log.info("Creating RSC worker...");
|
|
221
|
+
worker = await createWorker({
|
|
222
|
+
projectRoot: root,
|
|
223
|
+
workerPath: rscWorkerPath,
|
|
224
|
+
condition: "react-client",
|
|
225
|
+
});
|
|
226
|
+
log.info("RSC worker created");
|
|
227
|
+
}
|
|
228
|
+
const normalize = createInputNormalizer({
|
|
229
|
+
root,
|
|
230
|
+
removeExtension: false,
|
|
231
|
+
preserveModulesRoot: userOptions.build.preserveModulesRoot
|
|
232
|
+
? userOptions.moduleBase
|
|
233
|
+
: undefined,
|
|
234
|
+
});
|
|
235
|
+
server.middlewares.use(async (req, res, next) => {
|
|
236
|
+
if (!req.url) {
|
|
237
|
+
next();
|
|
238
|
+
return;
|
|
239
|
+
}
|
|
240
|
+
if (
|
|
241
|
+
req.url.endsWith(".rsc") ||
|
|
242
|
+
req.headers.accept?.includes("text/x-component")
|
|
243
|
+
) {
|
|
244
|
+
try {
|
|
245
|
+
const path = req.url?.includes("index.rsc")
|
|
246
|
+
? req.url.replace("index.rsc", "")
|
|
247
|
+
: req.url?.replace(".rsc", "");
|
|
248
|
+
let [key, value] = normalize(path);
|
|
249
|
+
|
|
250
|
+
let pageImport = DEFAULT_CONFIG.PAGE as string;
|
|
251
|
+
let propsImport = DEFAULT_CONFIG.PROPS as string;
|
|
252
|
+
// PAGE
|
|
253
|
+
// no trailing slash
|
|
254
|
+
const pathNoTrailing = path?.replace(/\/$/, '');
|
|
255
|
+
if (files.urlMap.has(req.url)) {
|
|
256
|
+
pageImport = files.urlMap.get(req.url)!.page;
|
|
257
|
+
propsImport = files.urlMap.get(req.url)!.props;
|
|
258
|
+
} else if (files.urlMap.has(pathNoTrailing)) {
|
|
259
|
+
pageImport = files.urlMap.get(pathNoTrailing)!.page;
|
|
260
|
+
propsImport = files.urlMap.get(pathNoTrailing)!.props;
|
|
261
|
+
} else if (files.urlMap.has(path)) {
|
|
262
|
+
pageImport = files.urlMap.get(path)!.page;
|
|
263
|
+
propsImport = files.urlMap.get(path)!.props;
|
|
264
|
+
} else if (files.urlMap.has(value)) {
|
|
265
|
+
pageImport = files.urlMap.get(value)!.page;
|
|
266
|
+
propsImport = files.urlMap.get(value)!.props;
|
|
267
|
+
} else if (files.urlMap.has(key)) {
|
|
268
|
+
pageImport = files.urlMap.get(key)!.page;
|
|
269
|
+
propsImport = files.urlMap.get(key)!.props;
|
|
270
|
+
} else {
|
|
271
|
+
console.warn(`Page/props import not found for any of the following (in order of priority): ${[req.url, pathNoTrailing, path, value, key].filter(Boolean).join(', ')} available pages:${Array.from(files.urlMap.keys()).join(', ')}`);
|
|
272
|
+
}
|
|
273
|
+
// Set headers early
|
|
274
|
+
res.setHeader("Content-Type", "text/x-component");
|
|
275
|
+
res.setHeader("Transfer-Encoding", "chunked");
|
|
276
|
+
res.setHeader("Connection", "keep-alive");
|
|
277
|
+
|
|
278
|
+
let hasError = false;
|
|
279
|
+
const timeout = setTimeout(() => {
|
|
280
|
+
if (!hasError) {
|
|
281
|
+
hasError = true;
|
|
282
|
+
res.statusCode = 500;
|
|
283
|
+
res.end("RSC render timeout");
|
|
284
|
+
}
|
|
285
|
+
}, 5000);
|
|
286
|
+
|
|
287
|
+
const messageHandler = (
|
|
288
|
+
message: RscWorkerMessage | RscWorkerResponse
|
|
289
|
+
) => {
|
|
290
|
+
try {
|
|
291
|
+
switch (message.type) {
|
|
292
|
+
case "RSC_CHUNK":
|
|
293
|
+
// Write chunk directly to response
|
|
294
|
+
if (!hasError) {
|
|
295
|
+
res.write(message.chunk);
|
|
296
|
+
}
|
|
297
|
+
break;
|
|
298
|
+
|
|
299
|
+
case "RSC_END":
|
|
300
|
+
clearTimeout(timeout);
|
|
301
|
+
if (!hasError) {
|
|
302
|
+
res.end();
|
|
303
|
+
}
|
|
304
|
+
worker.off("message", messageHandler);
|
|
305
|
+
break;
|
|
306
|
+
|
|
307
|
+
case "ERROR":
|
|
308
|
+
clearTimeout(timeout);
|
|
309
|
+
log.error("Render error", message);
|
|
310
|
+
if (!hasError) {
|
|
311
|
+
hasError = true;
|
|
312
|
+
res.statusCode = 500;
|
|
313
|
+
res.end(message.error);
|
|
314
|
+
}
|
|
315
|
+
worker.off("message", messageHandler);
|
|
316
|
+
break;
|
|
317
|
+
}
|
|
318
|
+
} catch (error) {
|
|
319
|
+
clearTimeout(timeout);
|
|
320
|
+
if (!hasError) {
|
|
321
|
+
hasError = true;
|
|
322
|
+
res.statusCode = 500;
|
|
323
|
+
res.end(
|
|
324
|
+
error instanceof Error ? error.message : String(error)
|
|
325
|
+
);
|
|
326
|
+
}
|
|
327
|
+
worker.off("message", messageHandler);
|
|
328
|
+
}
|
|
329
|
+
};
|
|
330
|
+
|
|
331
|
+
worker.on("message", messageHandler);
|
|
332
|
+
worker.once("error", (error) => {
|
|
333
|
+
clearTimeout(timeout);
|
|
334
|
+
if (!hasError) {
|
|
335
|
+
hasError = true;
|
|
336
|
+
res.statusCode = 500;
|
|
337
|
+
res.end(error instanceof Error ? error.message : String(error));
|
|
338
|
+
}
|
|
339
|
+
worker.off("message", messageHandler);
|
|
340
|
+
});
|
|
341
|
+
worker.postMessage({
|
|
342
|
+
type: "RSC_RENDER",
|
|
343
|
+
id: value,
|
|
344
|
+
pageImport,
|
|
345
|
+
propsImport,
|
|
346
|
+
url: req.url ?? "/",
|
|
347
|
+
pageExportName:
|
|
348
|
+
userOptions.pageExportName ?? DEFAULT_CONFIG.PAGE_EXPORT_NAME,
|
|
349
|
+
propsExportName:
|
|
350
|
+
userOptions.propsExportName ?? DEFAULT_CONFIG.PROPS_EXPORT_NAME,
|
|
351
|
+
outDir: userOptions.build.outDir,
|
|
352
|
+
projectRoot: root,
|
|
353
|
+
moduleRootPath:
|
|
354
|
+
userOptions.build.preserveModulesRoot === true
|
|
355
|
+
? userOptions.moduleBase
|
|
356
|
+
: "",
|
|
357
|
+
moduleBaseURL: ``,
|
|
358
|
+
moduleBasePath: '/',
|
|
359
|
+
pipableStreamOptions: userOptions.pipableStreamOptions,
|
|
360
|
+
cssFiles: []
|
|
361
|
+
} satisfies RscRenderMessage);
|
|
362
|
+
} catch (error) {
|
|
363
|
+
log.error("Middleware error:", error);
|
|
364
|
+
res.statusCode = 500;
|
|
365
|
+
res.end(error instanceof Error ? error.message : String(error));
|
|
366
|
+
}
|
|
367
|
+
} else {
|
|
368
|
+
next();
|
|
369
|
+
}
|
|
370
|
+
});
|
|
371
|
+
},
|
|
32
372
|
};
|
|
33
373
|
}
|
|
@@ -7,7 +7,7 @@ import {
|
|
|
7
7
|
import { DEFAULT_CONFIG } from "../config/defaults.js";
|
|
8
8
|
import { resolvePage } from "../resolvePage.js";
|
|
9
9
|
import { resolveProps } from "../resolveProps.js";
|
|
10
|
-
import type { CreateHandlerOptions,
|
|
10
|
+
import type { CreateHandlerOptions, ResolvedUserOptions } from "../types.js";
|
|
11
11
|
import { createRscStream } from "./createRscStream.js";
|
|
12
12
|
|
|
13
13
|
type CreateHandlerResult =
|
|
@@ -22,22 +22,16 @@ interface HandlerAssets {
|
|
|
22
22
|
|
|
23
23
|
export async function createHandler<T>(
|
|
24
24
|
url: string,
|
|
25
|
-
pluginOptions:
|
|
26
|
-
StreamPluginOptions,
|
|
27
|
-
"Page" | "props" | "build" | "Html" | "pageExportName" | "propsExportName"
|
|
28
|
-
> &
|
|
29
|
-
Required<
|
|
30
|
-
Pick<StreamPluginOptions, "moduleBase" | "moduleBasePath" | "projectRoot">
|
|
31
|
-
>,
|
|
25
|
+
pluginOptions: ResolvedUserOptions,
|
|
32
26
|
streamOptions: CreateHandlerOptions<T>
|
|
33
27
|
): Promise<CreateHandlerResult> {
|
|
34
28
|
const root = pluginOptions.projectRoot ?? process.cwd();
|
|
35
29
|
|
|
36
30
|
const Html = pluginOptions.Html ?? DEFAULT_CONFIG.HTML;
|
|
37
31
|
const pageExportName =
|
|
38
|
-
pluginOptions.pageExportName ?? DEFAULT_CONFIG.
|
|
32
|
+
pluginOptions.pageExportName ?? DEFAULT_CONFIG.PAGE_EXPORT_NAME;
|
|
39
33
|
const propsExportName =
|
|
40
|
-
pluginOptions.propsExportName ?? DEFAULT_CONFIG.
|
|
34
|
+
pluginOptions.propsExportName ?? DEFAULT_CONFIG.PROPS_EXPORT_NAME;
|
|
41
35
|
const controller = new AbortController();
|
|
42
36
|
|
|
43
37
|
const cssFiles = streamOptions.cssFiles;
|
|
@@ -52,13 +46,13 @@ export async function createHandler<T>(
|
|
|
52
46
|
|
|
53
47
|
const cssModules = new Set<string>();
|
|
54
48
|
|
|
55
|
-
if (!(streamOptions.
|
|
56
|
-
throw new Error("Missing manifest or moduleGraph, pass it to options.");
|
|
49
|
+
if (!(streamOptions.serverManifest || streamOptions.moduleGraph))
|
|
50
|
+
throw new Error("Missing server manifest or moduleGraph, pass it to options.");
|
|
57
51
|
|
|
58
|
-
const getCss = streamOptions.
|
|
52
|
+
const getCss = streamOptions.serverManifest
|
|
59
53
|
? (id: string) =>
|
|
60
54
|
collectManifestCss(
|
|
61
|
-
streamOptions.
|
|
55
|
+
streamOptions.serverManifest!,
|
|
62
56
|
root,
|
|
63
57
|
id,
|
|
64
58
|
streamOptions.onCssFile
|
|
@@ -69,7 +63,13 @@ export async function createHandler<T>(
|
|
|
69
63
|
try {
|
|
70
64
|
const mod = await streamOptions.loader(id);
|
|
71
65
|
const pageCss = await Promise.resolve(getCss(id));
|
|
72
|
-
Array.from(pageCss.keys()).forEach((css) =>
|
|
66
|
+
Array.from(pageCss.keys()).forEach((css) => {
|
|
67
|
+
cssModules.add(css);
|
|
68
|
+
// Notify about new CSS file if callback exists
|
|
69
|
+
if (streamOptions.onCssFile) {
|
|
70
|
+
streamOptions.onCssFile(css);
|
|
71
|
+
}
|
|
72
|
+
});
|
|
73
73
|
return mod as Record<string, any>;
|
|
74
74
|
} catch (e: any) {
|
|
75
75
|
if (e.message?.includes("module runner has been closed")) {
|
|
@@ -126,12 +126,11 @@ export async function createHandler<T>(
|
|
|
126
126
|
if (streamOptions.cssFiles) {
|
|
127
127
|
streamOptions.cssFiles.forEach((css) => cssModules.add(css));
|
|
128
128
|
}
|
|
129
|
-
|
|
130
129
|
const stream = createRscStream({
|
|
131
130
|
Html: Html,
|
|
132
131
|
Page: Page,
|
|
133
132
|
props: props,
|
|
134
|
-
moduleBasePath:
|
|
133
|
+
moduleBasePath: '',
|
|
135
134
|
logger: streamOptions.logger ?? createLogger(),
|
|
136
135
|
cssFiles: Array.from(cssModules),
|
|
137
136
|
route: url,
|
|
@@ -145,7 +144,6 @@ export async function createHandler<T>(
|
|
|
145
144
|
});
|
|
146
145
|
|
|
147
146
|
if (!stream) {
|
|
148
|
-
console.log("[createHandler] No stream created for route:", url);
|
|
149
147
|
return { type: "skip" as const };
|
|
150
148
|
}
|
|
151
149
|
|
|
@@ -1,49 +1,82 @@
|
|
|
1
1
|
import * as React from "react";
|
|
2
|
-
import type { PipeableStream } from "react-dom/server";
|
|
3
2
|
// @ts-ignore
|
|
4
3
|
import { renderToPipeableStream } from "react-server-dom-esm/server.node";
|
|
5
|
-
import {
|
|
6
|
-
import type {
|
|
4
|
+
import type { PipeableStreamOptions } from "../worker/types.js";
|
|
5
|
+
import type { Logger } from "vite";
|
|
7
6
|
|
|
8
|
-
export function createRscStream(
|
|
9
|
-
streamOptions: RscStreamOptions
|
|
10
|
-
): PipeableStream {
|
|
11
|
-
const {
|
|
12
|
-
Html,
|
|
13
|
-
Page,
|
|
14
|
-
props,
|
|
15
|
-
logger,
|
|
16
|
-
cssFiles,
|
|
17
|
-
moduleBasePath,
|
|
18
|
-
pipableStreamOptions,
|
|
19
|
-
htmlProps,
|
|
20
|
-
} = streamOptions;
|
|
21
7
|
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
),
|
|
41
|
-
|
|
42
|
-
{
|
|
43
|
-
onError: logger?.error ?? console.error,
|
|
44
|
-
onPostpone: logger?.info ?? console.info,
|
|
45
|
-
environmentName: "Server",
|
|
46
|
-
...pipableStreamOptions
|
|
47
|
-
}
|
|
8
|
+
// CSS collector component
|
|
9
|
+
function CssCollector({
|
|
10
|
+
children,
|
|
11
|
+
cssFiles,
|
|
12
|
+
}: {
|
|
13
|
+
children?: React.ReactNode;
|
|
14
|
+
cssFiles: string[];
|
|
15
|
+
}) {
|
|
16
|
+
return React.createElement(
|
|
17
|
+
React.Fragment,
|
|
18
|
+
null,
|
|
19
|
+
...cssFiles.map((css) => {
|
|
20
|
+
const url = css.startsWith('/') || css.startsWith('http') || css.startsWith('./') ? css : '/'+css
|
|
21
|
+
return React.createElement('link', {
|
|
22
|
+
key: css,
|
|
23
|
+
rel: 'stylesheet',
|
|
24
|
+
href: url
|
|
25
|
+
})
|
|
26
|
+
}),
|
|
27
|
+
children
|
|
48
28
|
);
|
|
49
29
|
}
|
|
30
|
+
|
|
31
|
+
export function createRscStream({
|
|
32
|
+
Html,
|
|
33
|
+
Page,
|
|
34
|
+
props,
|
|
35
|
+
moduleBasePath,
|
|
36
|
+
logger,
|
|
37
|
+
cssFiles = [],
|
|
38
|
+
route,
|
|
39
|
+
url,
|
|
40
|
+
pipableStreamOptions,
|
|
41
|
+
htmlProps,
|
|
42
|
+
}: {
|
|
43
|
+
Html: React.ComponentType<any>;
|
|
44
|
+
Page: React.ComponentType<any>;
|
|
45
|
+
props: any;
|
|
46
|
+
moduleBasePath: string;
|
|
47
|
+
logger: Logger;
|
|
48
|
+
cssFiles?: string[];
|
|
49
|
+
route: string;
|
|
50
|
+
url: string;
|
|
51
|
+
pipableStreamOptions?: PipeableStreamOptions;
|
|
52
|
+
htmlProps?: any;
|
|
53
|
+
}) {
|
|
54
|
+
|
|
55
|
+
const htmlIsFragment = Html == React.Fragment;
|
|
56
|
+
const withCss = React.createElement(
|
|
57
|
+
CssCollector,
|
|
58
|
+
{ cssFiles },
|
|
59
|
+
React.createElement(Page, props)
|
|
60
|
+
)
|
|
61
|
+
// Otherwise wrap with Html component
|
|
62
|
+
const content = htmlIsFragment
|
|
63
|
+
? withCss
|
|
64
|
+
: React.createElement(Html, htmlProps, withCss);
|
|
65
|
+
try {
|
|
66
|
+
return renderToPipeableStream(
|
|
67
|
+
content,
|
|
68
|
+
moduleBasePath,
|
|
69
|
+
{
|
|
70
|
+
onError: (error: Error) => {
|
|
71
|
+
logger.error(`Stream error at ${route}.`, {error});
|
|
72
|
+
},
|
|
73
|
+
onPostpone: logger.info ?? console.info,
|
|
74
|
+
environmentName: "Server",
|
|
75
|
+
...pipableStreamOptions,
|
|
76
|
+
}
|
|
77
|
+
);
|
|
78
|
+
} catch (error) {
|
|
79
|
+
logger.error(`Failed to create stream for ${route}.`, {error: error as Error});
|
|
80
|
+
return null;
|
|
81
|
+
}
|
|
82
|
+
}
|