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