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
|
@@ -3,89 +3,69 @@
|
|
|
3
3
|
* Copyright (c) Nico Brinkkemper
|
|
4
4
|
* MIT License
|
|
5
5
|
*/
|
|
6
|
-
import {
|
|
6
|
+
import { resolveOptions } from '../config/resolveOptions.js';
|
|
7
7
|
import { createClientComponentTransformer } from './transformer-client-components.js';
|
|
8
|
-
import
|
|
8
|
+
import 'vite';
|
|
9
|
+
import { createInputNormalizer } from '../helpers/inputNormalizer.js';
|
|
10
|
+
import { resolveUserConfig } from '../config/resolveUserConfig.js';
|
|
9
11
|
|
|
12
|
+
let userOptions;
|
|
13
|
+
let userConfig;
|
|
14
|
+
let resolvedConfig;
|
|
15
|
+
let resolvedConfigEnv;
|
|
10
16
|
function reactTransformPlugin(options) {
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
console.log(process.env["NODE_OPTIONS"]);
|
|
15
|
-
throw new Error(
|
|
16
|
-
'react-server condition not found, set NODE_OPTIONS="--conditions react-server"'
|
|
17
|
-
);
|
|
17
|
+
const resolvedUserOptions = resolveOptions(options);
|
|
18
|
+
if (resolvedUserOptions.type === "error") {
|
|
19
|
+
throw resolvedUserOptions.error;
|
|
18
20
|
}
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
let transformServerAction;
|
|
22
|
-
const clientComponents = /* @__PURE__ */ new Set();
|
|
21
|
+
userOptions = resolvedUserOptions.userOptions;
|
|
22
|
+
const pendingEmits = /* @__PURE__ */ new Map();
|
|
23
23
|
return {
|
|
24
|
-
name: "vite:react-
|
|
24
|
+
name: "vite:react-transform",
|
|
25
25
|
enforce: "post",
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
projectRoot,
|
|
39
|
-
output: {
|
|
40
|
-
dir: config.build?.outDir ?? DEFAULT_CONFIG.BUILD.server
|
|
41
|
-
},
|
|
42
|
-
isProduction: config.isProduction
|
|
43
|
-
})
|
|
44
|
-
}).transform;
|
|
45
|
-
},
|
|
46
|
-
config(config) {
|
|
47
|
-
const existingInput = config.build?.rollupOptions?.input || {};
|
|
48
|
-
const currentInputs = typeof existingInput === "string" ? { default: existingInput } : existingInput;
|
|
49
|
-
const entries = Array.from(clientComponents).reduce(
|
|
50
|
-
(acc, path) => ({
|
|
51
|
-
...acc,
|
|
52
|
-
[path.replace(DEFAULT_CONFIG.FILE_REGEX, "")]: path
|
|
53
|
-
}),
|
|
54
|
-
{}
|
|
55
|
-
);
|
|
56
|
-
return {
|
|
57
|
-
build: {
|
|
58
|
-
rollupOptions: {
|
|
59
|
-
input: {
|
|
60
|
-
...currentInputs,
|
|
61
|
-
...entries
|
|
62
|
-
}
|
|
63
|
-
}
|
|
64
|
-
}
|
|
65
|
-
};
|
|
26
|
+
config(config, configEnv) {
|
|
27
|
+
const resolvedUserConfig = resolveUserConfig({
|
|
28
|
+
isClient: false,
|
|
29
|
+
config,
|
|
30
|
+
configEnv,
|
|
31
|
+
userOptions
|
|
32
|
+
});
|
|
33
|
+
if (resolvedUserConfig.type === "error") {
|
|
34
|
+
throw resolvedUserConfig.error;
|
|
35
|
+
}
|
|
36
|
+
userConfig = resolvedUserConfig.userConfig;
|
|
37
|
+
resolvedConfigEnv = configEnv;
|
|
66
38
|
},
|
|
67
|
-
|
|
68
|
-
|
|
39
|
+
configResolved(config) {
|
|
40
|
+
resolvedConfig = config;
|
|
69
41
|
},
|
|
70
|
-
|
|
42
|
+
transform(code, id, opt = {}) {
|
|
71
43
|
const hasClientDirective = code.match(/^["']use client["'];?/);
|
|
72
|
-
if (!hasClientDirective)
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
return
|
|
44
|
+
if (!hasClientDirective) return null;
|
|
45
|
+
const moduleInfo = this.getModuleInfo(id);
|
|
46
|
+
if (!moduleInfo) return null;
|
|
47
|
+
if (resolvedConfigEnv.command === "serve") {
|
|
48
|
+
const transformer2 = createClientComponentTransformer(userOptions, userConfig, resolvedConfigEnv);
|
|
49
|
+
return transformer2.transform.bind(this)(code, id, opt);
|
|
78
50
|
}
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
51
|
+
const normalizer = createInputNormalizer({
|
|
52
|
+
root: resolvedConfig.root,
|
|
53
|
+
preserveModulesRoot: undefined
|
|
54
|
+
});
|
|
55
|
+
const [normalizedId, normalizedPath] = normalizer(id);
|
|
56
|
+
const referenceId = this.emitFile({
|
|
57
|
+
type: "chunk",
|
|
58
|
+
id: normalizedPath,
|
|
59
|
+
name: normalizedId
|
|
60
|
+
});
|
|
61
|
+
pendingEmits.set(normalizedId, { referenceId });
|
|
62
|
+
const transformer = createClientComponentTransformer(userOptions, userConfig, resolvedConfigEnv);
|
|
63
|
+
return transformer.transform.bind(this)(code, id, opt);
|
|
82
64
|
},
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
Array.from(clientComponents)
|
|
88
|
-
);
|
|
65
|
+
renderDynamicImport() {
|
|
66
|
+
for (const [normalizedId, { referenceId }] of pendingEmits) {
|
|
67
|
+
this.getFileName(referenceId);
|
|
68
|
+
}
|
|
89
69
|
}
|
|
90
70
|
};
|
|
91
71
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"plugin.js","sources":["../../../plugin/transformer/plugin.ts"],"sourcesContent":["import {
|
|
1
|
+
{"version":3,"file":"plugin.js","sources":["../../../plugin/transformer/plugin.ts"],"sourcesContent":["import { resolveOptions } from \"../config/resolveOptions.js\";\nimport type {\n ResolvedUserConfig,\n ResolvedUserOptions,\n StreamPluginOptions,\n} from \"../types.js\";\nimport { createClientComponentTransformer } from \"./transformer-client-components.js\";\nimport { createModuleIdGenerator } from \"../config/createModuleIdGenerator.js\";\nimport { type ConfigEnv, type Plugin, type ResolvedConfig } from \"vite\";\nimport { createInputNormalizer } from \"../helpers/inputNormalizer.js\";\nimport { resolveUserConfig } from \"../config/resolveUserConfig.js\";\n\nlet userOptions: ResolvedUserOptions;\nlet userConfig: ResolvedUserConfig;\nlet resolvedConfig: ResolvedConfig;\nlet resolvedConfigEnv: ConfigEnv;\n\n/**\n * Plugin for transforming React Client Components.\n *\n * Core responsibilities:\n * 1. Detects \"use client\" directives\n * 2. Transforms client components for RSC boundaries\n * 3. Adds client reference metadata for RSC\n *\n * When a component is marked with \"use client\", it:\n * - Gets transformed into a client reference\n * - Maintains module ID for RSC boundaries\n * - Preserves class/function behavior\n *\n * @example\n * ```ts\n * export default defineConfig({\n * plugins: [\n * viteReactClientTransformPlugin({\n * projectRoot: process.cwd(),\n * })\n * ]\n * });\n * ```\n */\n\nexport function reactTransformPlugin(options: StreamPluginOptions): Plugin {\n const resolvedUserOptions = resolveOptions(options);\n if (resolvedUserOptions.type === \"error\") {\n throw resolvedUserOptions.error;\n }\n userOptions = resolvedUserOptions.userOptions;\n\n let manifest: Record<string, { file: string }> = {};\n const pendingEmits = new Map<string, { referenceId: string }>();\n\n return {\n name: \"vite:react-transform\",\n enforce: \"post\",\n\n config(config, configEnv) {\n const resolvedUserConfig = resolveUserConfig({\n isClient: false,\n config,\n configEnv,\n userOptions,\n });\n if (resolvedUserConfig.type === \"error\") {\n throw resolvedUserConfig.error;\n }\n userConfig = resolvedUserConfig.userConfig;\n resolvedConfigEnv = configEnv;\n },\n configResolved(config) {\n resolvedConfig = config;\n },\n\n transform(code: string, id: string, opt: { ssr?: boolean } = {}) {\n const hasClientDirective = code.match(/^[\"']use client[\"'];?/);\n if (!hasClientDirective) return null;\n\n const moduleInfo = this.getModuleInfo(id);\n if (!moduleInfo) return null;\n\n // In dev mode, just transform the code without emitting\n if (resolvedConfigEnv.command === 'serve') {\n const transformer = createClientComponentTransformer(userOptions, userConfig, resolvedConfigEnv);\n return transformer.transform!.bind(this)(code, id, opt);\n }\n\n // Production build logic...\n const normalizer = createInputNormalizer({\n root: resolvedConfig.root,\n preserveModulesRoot: undefined,\n });\n const [normalizedId, normalizedPath] = normalizer(id);\n\n // Add to manifest immediately with a temporary path\n manifest[normalizedId] = { file: normalizedPath };\n \n const referenceId = this.emitFile({\n type: \"chunk\",\n id: normalizedPath,\n name: normalizedId,\n });\n pendingEmits.set(normalizedId, { referenceId });\n\n const transformer = createClientComponentTransformer(userOptions, userConfig, resolvedConfigEnv);\n return transformer.transform!.bind(this)(code, id, opt);\n },\n\n renderDynamicImport() {\n // Update manifest with real file names\n for (const [normalizedId, { referenceId }] of pendingEmits) {\n const fileName = this.getFileName(referenceId);\n manifest[normalizedId] = { file: fileName };\n }\n },\n };\n}\n"],"names":["transformer"],"mappings":";;;;;;;;;;;AAYA,IAAI,WAAA;AACJ,IAAI,UAAA;AACJ,IAAI,cAAA;AACJ,IAAI,iBAAA;AA2BG,SAAS,qBAAqB,OAAsC,EAAA;AACzE,EAAM,MAAA,mBAAA,GAAsB,eAAe,OAAO,CAAA;AAClD,EAAI,IAAA,mBAAA,CAAoB,SAAS,OAAS,EAAA;AACxC,IAAA,MAAM,mBAAoB,CAAA,KAAA;AAAA;AAE5B,EAAA,WAAA,GAAc,mBAAoB,CAAA,WAAA;AAGlC,EAAM,MAAA,YAAA,uBAAmB,GAAqC,EAAA;AAE9D,EAA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A;AAKpD,MAAM,MAAA,WAAA,GAAc,KAAK,QAAS,CAAA;AAAA,QAChC,IAAM,EAAA,OAAA;AAAA,QACN,EAAI,EAAA,cAAA;AAAA,QACJ,IAAM,EAAA;AAAA,OACP,CAAA;AACD,MAAA,YAAA,CAAa,GAAI,CAAA,YAAA,EAAc,EAAE,WAAA,EAAa,CAAA;AAE9C,MAAA,MAAM,WAAc,GAAA,gCAAA,CAAiC,WAAa,EAAA,UAAA,EAAY,iBAAiB,CAAA;AAC/F,MAAA,OAAO,YAAY,SAAW,CAAA,IAAA,CAAK,IAAI,CAAE,CAAA,IAAA,EAAM,IAAI,GAAG,CAAA;AAAA,KACxD;AAAA,IAEA,mBAAsB,GAAA;AAEpB,MAAA,KAAA,MAAW,CAAC,YAAc,EAAA,EAAE,WAAY,EAAC,KAAK,YAAc,EAAA;AAC1D,QAAiB,IAAK,CAAA,WAAA,CAAY,WAAW;AACH;AAC5C;AACF,GACF;AACF;;;;"}
|
|
@@ -1,6 +1,15 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
import type {
|
|
3
|
-
|
|
1
|
+
import type { PluginContext } from "rollup";
|
|
2
|
+
import type { ResolvedUserConfig, ResolvedUserOptions } from "../types.js";
|
|
3
|
+
import type { ConfigEnv } from "vite";
|
|
4
|
+
export declare function createClientComponentTransformer({ moduleBase, projectRoot, moduleId, build, }: ResolvedUserOptions, config: ResolvedUserConfig, configEnv: ConfigEnv): {
|
|
5
|
+
readonly name: "vite-plugin-react-server:client-components-transformer";
|
|
6
|
+
readonly transform: (this: PluginContext, code: string, id: string, options?: {
|
|
7
|
+
ssr?: boolean;
|
|
8
|
+
}) => Promise<{
|
|
9
|
+
code: string;
|
|
10
|
+
map: null;
|
|
11
|
+
} | null>;
|
|
12
|
+
};
|
|
4
13
|
/**
|
|
5
14
|
* transformedCode += `
|
|
6
15
|
const ${exportName}Ref = Object.defineProperties(
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"transformer-client-components.d.ts","sourceRoot":"","sources":["../../../plugin/transformer/transformer-client-components.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,
|
|
1
|
+
{"version":3,"file":"transformer-client-components.d.ts","sourceRoot":"","sources":["../../../plugin/transformer/transformer-client-components.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,QAAQ,CAAC;AAI5C,OAAO,KAAK,EAAE,kBAAkB,EAAE,mBAAmB,EAAE,MAAM,aAAa,CAAC;AAC3E,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,MAAM,CAAC;AAItC,wBAAgB,gCAAgC,CAC9C,EACE,UAAuC,EACvC,WAAW,EACX,QAAQ,EACR,KAAK,GACN,EAAE,mBAAmB,EACtB,MAAM,EAAE,kBAAkB,EAC1B,SAAS,EAAE,SAAS;;+BAsBV,aAAa,QACb,MAAM,MACR,MAAM,YACA;QAAE,GAAG,CAAC,EAAE,OAAO,CAAA;KAAE;;;;EA2GhC;AAED;;;;;;;;;;;;;;;;GAgBG"}
|
|
@@ -3,10 +3,30 @@
|
|
|
3
3
|
* Copyright (c) Nico Brinkkemper
|
|
4
4
|
* MIT License
|
|
5
5
|
*/
|
|
6
|
+
import { DEFAULT_CONFIG } from '../config/defaults.js';
|
|
7
|
+
import { createInputNormalizer } from '../helpers/inputNormalizer.js';
|
|
8
|
+
import { join } from 'path';
|
|
9
|
+
import { tryManifest } from '../helpers/tryManifest.js';
|
|
10
|
+
|
|
6
11
|
function createClientComponentTransformer({
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
12
|
+
moduleBase = DEFAULT_CONFIG.MODULE_BASE,
|
|
13
|
+
projectRoot,
|
|
14
|
+
moduleId,
|
|
15
|
+
build
|
|
16
|
+
}, config, configEnv) {
|
|
17
|
+
let moduleIdFn = moduleId;
|
|
18
|
+
const normalizer = createInputNormalizer({
|
|
19
|
+
root: projectRoot,
|
|
20
|
+
preserveModulesRoot: moduleBase,
|
|
21
|
+
removeExtension: true
|
|
22
|
+
});
|
|
23
|
+
config.build.outDir?.endsWith(build.client);
|
|
24
|
+
const isServer = config.build.outDir?.endsWith(build.server);
|
|
25
|
+
isServer ? tryManifest({
|
|
26
|
+
root: projectRoot,
|
|
27
|
+
outDir: join(build.outDir, build.client),
|
|
28
|
+
ssrManifest: false
|
|
29
|
+
}).manifest : null;
|
|
10
30
|
return {
|
|
11
31
|
name: "vite-plugin-react-server:client-components-transformer",
|
|
12
32
|
async transform(code, id, options) {
|
|
@@ -14,6 +34,28 @@ function createClientComponentTransformer({
|
|
|
14
34
|
if (id.includes("node_modules") || id.includes("vite/dist")) {
|
|
15
35
|
return null;
|
|
16
36
|
}
|
|
37
|
+
let rest = id.split("/");
|
|
38
|
+
let fileName = rest.pop();
|
|
39
|
+
let dir = rest.join("/");
|
|
40
|
+
if (dir.startsWith(projectRoot)) {
|
|
41
|
+
dir = dir.replace(projectRoot, "");
|
|
42
|
+
}
|
|
43
|
+
const index = dir.indexOf(moduleBase);
|
|
44
|
+
if (index !== -1) {
|
|
45
|
+
if (configEnv.command === "build") {
|
|
46
|
+
if (isServer) {
|
|
47
|
+
dir = join(build.assetsDir, dir.slice(index + moduleBase.length));
|
|
48
|
+
} else {
|
|
49
|
+
dir = new URL(
|
|
50
|
+
dir,
|
|
51
|
+
`file://${config?.server?.host ?? "localhost"}:${config?.server?.port ?? configEnv.isPreview ? 4173 : 5173}`
|
|
52
|
+
).pathname;
|
|
53
|
+
}
|
|
54
|
+
} else if (configEnv.command === "serve") {
|
|
55
|
+
dir = "";
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
if (!fileName) fileName = id;
|
|
17
59
|
const info = this?.getModuleInfo(id);
|
|
18
60
|
const hasDirective = code.match(/^["']use client["'];?/);
|
|
19
61
|
const isClientComponent = hasDirective || info?.meta?.["directives"]?.includes("use client");
|
|
@@ -21,7 +63,8 @@ function createClientComponentTransformer({
|
|
|
21
63
|
return null;
|
|
22
64
|
}
|
|
23
65
|
let transformedCode = code;
|
|
24
|
-
const
|
|
66
|
+
const moduleId2 = moduleIdFn(id, configEnv.isSsrBuild ?? false);
|
|
67
|
+
const [key, value] = normalizer([moduleId2, id]);
|
|
25
68
|
const exportMatches = Array.from(
|
|
26
69
|
code.matchAll(/export\s+(?:const|let|var|function|class)\s+(\w+)/g)
|
|
27
70
|
);
|
|
@@ -30,27 +73,45 @@ function createClientComponentTransformer({
|
|
|
30
73
|
}
|
|
31
74
|
for (const [fullMatch, exportName] of exportMatches) {
|
|
32
75
|
if (!exportName) continue;
|
|
76
|
+
console.log("exportName", dir, fileName, exportName);
|
|
33
77
|
const isClass = fullMatch.includes("class");
|
|
34
78
|
transformedCode = transformedCode.replace(
|
|
35
79
|
fullMatch,
|
|
36
80
|
fullMatch.replace("export ", "")
|
|
37
81
|
);
|
|
38
|
-
|
|
39
|
-
const
|
|
40
|
-
|
|
82
|
+
let path = `import.meta.url`;
|
|
83
|
+
const { manifest, error } = tryManifest({
|
|
84
|
+
root: projectRoot,
|
|
85
|
+
outDir: join(build.outDir, build.client),
|
|
86
|
+
ssrManifest: false
|
|
87
|
+
});
|
|
88
|
+
if (error) {
|
|
89
|
+
console.error(`[RSC] Error getting manifest: ${error}`);
|
|
90
|
+
continue;
|
|
91
|
+
}
|
|
92
|
+
const fileFromManifest = manifest[join(moduleBase, value)]?.file;
|
|
93
|
+
if (fileFromManifest) {
|
|
94
|
+
path = JSON.stringify(fileFromManifest);
|
|
95
|
+
} else {
|
|
96
|
+
console.error(`[RSC] File not found in manifest: ${value}`);
|
|
97
|
+
continue;
|
|
98
|
+
}
|
|
99
|
+
transformedCode += `const ${exportName}Ref = Object.defineProperties(${isClass ? `class extends ${exportName} {
|
|
41
100
|
constructor(...args) { super(...args); }
|
|
42
101
|
}` : `function(...args) { return ${exportName}.apply(null, args); }`},
|
|
43
102
|
{
|
|
44
103
|
$$typeof: { value: Symbol.for("react.client.reference") },
|
|
45
|
-
$$id: { value: ${
|
|
46
|
-
$$filepath: { value: ${JSON.stringify(id)} }
|
|
104
|
+
$$id: { value: \`\${${path}}${"#" + exportName}\` }
|
|
47
105
|
}
|
|
48
106
|
);
|
|
49
107
|
export { ${exportName}Ref as ${exportName} };`;
|
|
50
108
|
}
|
|
51
109
|
return { code: transformedCode, map: null };
|
|
52
110
|
} catch (error) {
|
|
53
|
-
console.error(
|
|
111
|
+
console.error(
|
|
112
|
+
`[RSC] Error transforming client component: ${id}`,
|
|
113
|
+
error
|
|
114
|
+
);
|
|
54
115
|
throw error;
|
|
55
116
|
}
|
|
56
117
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"transformer-client-components.js","sources":["../../../plugin/transformer/transformer-client-components.ts"],"sourcesContent":["import type {
|
|
1
|
+
{"version":3,"file":"transformer-client-components.js","sources":["../../../plugin/transformer/transformer-client-components.ts"],"sourcesContent":["import type { PluginContext } from \"rollup\";\nimport type { TransformerOptions } from \"./types.js\";\nimport { DEFAULT_CONFIG } from \"../config/defaults.js\";\nimport { createInputNormalizer } from \"../helpers/inputNormalizer.js\";\nimport type { ResolvedUserConfig, ResolvedUserOptions } from \"../types.js\";\nimport type { ConfigEnv } from \"vite\";\nimport { join } from \"path\";\nimport { tryManifest } from \"../helpers/tryManifest.js\";\n\nexport function createClientComponentTransformer(\n {\n moduleBase = DEFAULT_CONFIG.MODULE_BASE,\n projectRoot,\n moduleId,\n build,\n }: ResolvedUserOptions,\n config: ResolvedUserConfig,\n configEnv: ConfigEnv\n) {\n let moduleIdFn = moduleId;\n const normalizer = createInputNormalizer({\n root: projectRoot,\n preserveModulesRoot: moduleBase,\n removeExtension: true,\n });\n const isClient = config.build.outDir?.endsWith(build.client);\n const isServer = config.build.outDir?.endsWith(build.server);\n const references = isServer\n ? tryManifest({\n root: projectRoot,\n outDir: join(build.outDir, build.client),\n ssrManifest: false,\n }).manifest\n : null;\n\n return {\n name: \"vite-plugin-react-server:client-components-transformer\",\n\n async transform(\n this: PluginContext,\n code: string,\n id: string,\n options?: { ssr?: boolean }\n ) {\n try {\n // Skip node_modules and vite internal files\n if (id.includes(\"node_modules\") || id.includes(\"vite/dist\")) {\n return null;\n }\n\n // Check if this is a client component from metadata or directive\n let rest = id.split(\"/\");\n let fileName = rest.pop() as string;\n let dir = rest.join(\"/\");\n if (dir.startsWith(projectRoot)) {\n dir = dir.replace(projectRoot, \"\");\n }\n const index = dir.indexOf(moduleBase);\n if (index !== -1) {\n if (configEnv.command === \"build\") {\n if (isServer) {\n dir = join(build.assetsDir, dir.slice(index + moduleBase.length));\n } else {\n dir = new URL(\n dir,\n `file://${config?.server?.host ?? \"localhost\"}:${\n config?.server?.port ?? configEnv.isPreview ? 4173 : 5173\n }`\n ).pathname;\n }\n } else if (configEnv.command === \"serve\") {\n dir = \"\";\n }\n }\n if (!fileName) fileName = id;\n const info = this?.getModuleInfo(id);\n const hasDirective = code.match(/^[\"']use client[\"'];?/);\n const isClientComponent =\n hasDirective || info?.meta?.[\"directives\"]?.includes(\"use client\");\n\n if (!isClientComponent) {\n return null; // Not a client component, skip\n }\n\n let transformedCode = code;\n const moduleId = moduleIdFn!(id, configEnv.isSsrBuild ?? false);\n const [key, value] = normalizer([moduleId, id]);\n // Find all named exports\n const exportMatches = Array.from(\n code.matchAll(/export\\s+(?:const|let|var|function|class)\\s+(\\w+)/g)\n );\n\n if (!exportMatches.length) {\n return null;\n }\n\n // Transform each export\n for (const [fullMatch, exportName] of exportMatches) {\n if (!exportName) continue;\n console.log(\"exportName\", dir, fileName, exportName);\n const isClass = fullMatch.includes(\"class\");\n // Remove export keyword\n transformedCode = transformedCode.replace(\n fullMatch,\n fullMatch.replace(\"export \", \"\")\n );\n let path = `import.meta.url`;\n // path from moduleBase to value\n const { manifest, error } = tryManifest({\n root: projectRoot,\n outDir: join(build.outDir, build.client),\n ssrManifest: false,\n });\n if (error) {\n console.error(`[RSC] Error getting manifest: ${error}`);\n continue;\n }\n const fileFromManifest = manifest[join(moduleBase, value)]?.file;\n if (fileFromManifest) {\n path = JSON.stringify(fileFromManifest);\n } else {\n console.error(`[RSC] File not found in manifest: ${value}`);\n continue;\n }\n\n transformedCode += `const ${exportName}Ref = Object.defineProperties(${\n isClass\n ? `class extends ${exportName} {\n constructor(...args) { super(...args); }\n }`\n : `function(...args) { return ${exportName}.apply(null, args); }`\n },\n {\n $$typeof: { value: Symbol.for(\"react.client.reference\") },\n $$id: { value: \\`\\${${path}}${\"#\" + exportName}\\` }\n }\n);\nexport { ${exportName}Ref as ${exportName} };`;\n }\n return { code: transformedCode, map: null };\n } catch (error) {\n console.error(\n `[RSC] Error transforming client component: ${id}`,\n error\n );\n throw error;\n }\n },\n } as const;\n}\n\n/**\n * transformedCode += `\nconst ${exportName}Ref = Object.defineProperties(\n ${\n isClass\n ? `class extends ${exportName} {\n constructor(...args) { super(...args); }\n }`\n : `function(...args) { return ${exportName}.apply(null, args); }`\n },\n {\n $$typeof: { value: Symbol.for(\"react.client.reference\") },\n $$id: { value: ${JSON.stringify(moduleId + \"#\" + exportName)} }\n }\n);\nexport { ${exportName}Ref as ${exportName} };`;\n */\n"],"names":["moduleId"],"mappings":";;;;;;;;;;AASO,SAAS,gCACd,CAAA;AAAA,EACE,aAAa,cAAe,CAAA,WAAA;AAAA,EAC5B,WAAA;AAAA,EACA,QAAA;AAAA,EACA;AACF,CAAA,EACA,QACA,SACA,EAAA;AACA,EAAA,IAAI,UAAa,GAAA,QAAA;AACjB,EAAA,MAAM,aAAa,qBAAsB,CAAA;AAAA,IACvC,IAAM,EAAA,WAAA;AAAA,IACN,mBAAqB,EAAA,UAAA;AAAA,IACrB,eAAiB,EAAA;AAAA,GAClB,CAAA;AACD,EAAiB,MAAO,CAAA,KAAA,CAAM,MAAQ,EAAA,QAAA,CAAS,MAAM,MAAM;AAC3D,EAAA,MAAM,WAAW,MAAO,CAAA,KAAA,CAAM,MAAQ,EAAA,QAAA,CAAS,MAAM,MAAM,CAAA;AAC3D,EAAmB,WACf,WAAY,CAAA;AAAA,IACV,IAAM,EAAA,WAA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,EAAE,CAAA,CAAA;AAAA,UAChD;AAAA,SACF;AACA,QAAM,MAAA,KAAA;AAAA;AACR;AACF,GACF;AACF;;;;"}
|
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
|
|
1
|
+
import type { ResolvedUserOptions, ResolvedUserConfig } from "../types.js";
|
|
2
|
+
import type { ConfigEnv } from "vite";
|
|
3
|
+
export declare function createServerActionTransformer({ moduleBase, projectRoot, moduleId, }: ResolvedUserOptions, config: ResolvedUserConfig, configEnv: ConfigEnv): {
|
|
3
4
|
name: string;
|
|
4
|
-
enforce: "post";
|
|
5
5
|
transform(code: string, path: string, { ssr }?: {
|
|
6
6
|
ssr: boolean;
|
|
7
7
|
}): Promise<{
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"transformer-server-actions.d.ts","sourceRoot":"","sources":["../../../plugin/transformer/transformer-server-actions.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"transformer-server-actions.d.ts","sourceRoot":"","sources":["../../../plugin/transformer/transformer-server-actions.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,mBAAmB,EAAE,kBAAkB,EAAE,MAAM,aAAa,CAAC;AAC3E,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,MAAM,CAAC;AAGtC,wBAAgB,6BAA6B,CAAC,EAC5C,UAAuC,EACvC,WAAW,EACX,QAAQ,GACT,EAAE,mBAAmB,EAAE,MAAM,EAAE,kBAAkB,EAAE,SAAS,EAAE,SAAS;;oBAW5D,MAAM,QACN,MAAM,YACD;QAAE,GAAG,EAAE,OAAO,CAAA;KAAE;;;;EAkFhC;AAED;;;;;;;;;;;;;;;;GAgBG"}
|
|
@@ -1,76 +1,90 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
1
|
+
import { SourceMapGenerator } from "source-map";
|
|
2
|
+
import { DEFAULT_CONFIG } from "../config/defaults.js";
|
|
3
|
+
import { createInputNormalizer } from "../helpers/inputNormalizer.js";
|
|
4
|
+
export function createServerActionTransformer({ moduleBase = DEFAULT_CONFIG.MODULE_BASE, projectRoot, moduleId, }, config, configEnv) {
|
|
5
|
+
let moduleIdFn = moduleId;
|
|
6
|
+
const normalizer = createInputNormalizer({
|
|
7
|
+
root: projectRoot,
|
|
8
|
+
preserveModulesRoot: moduleBase,
|
|
9
|
+
removeExtension: DEFAULT_CONFIG.FILE_REGEX,
|
|
10
|
+
});
|
|
11
|
+
return {
|
|
12
|
+
name: "vite-plugin-react-server:server-actions-transformer",
|
|
13
|
+
async transform(code, path, { ssr } = { ssr: false }) {
|
|
14
|
+
try {
|
|
15
|
+
let transformedCode = code;
|
|
16
|
+
const directiveMatch = code.match(/^["']use server["'];?/);
|
|
17
|
+
const moduleId = moduleIdFn(path, ssr);
|
|
18
|
+
const [key, value] = normalizer(moduleId);
|
|
19
|
+
// Log the path transformation
|
|
20
|
+
console.log("[RSC Transform] Module path transformation:", {
|
|
21
|
+
original: path,
|
|
22
|
+
transformed: moduleId,
|
|
23
|
+
});
|
|
24
|
+
// Find all named exports
|
|
25
|
+
const exportMatches = Array.from(code.matchAll(/export\s+(?:const|let|var|function|class)\s+(\w+)/g));
|
|
26
|
+
if (!exportMatches.length) {
|
|
27
|
+
console.warn(`[RSC] No exports found in server action module: ${path}`);
|
|
28
|
+
return null;
|
|
29
|
+
}
|
|
30
|
+
// Transform each export
|
|
31
|
+
for (const [fullMatch, exportName] of exportMatches) {
|
|
32
|
+
if (!exportName) {
|
|
33
|
+
console.warn(`[RSC] Invalid export in server action module: ${path}`);
|
|
34
|
+
continue;
|
|
35
|
+
}
|
|
36
|
+
const isClass = fullMatch.includes("class");
|
|
37
|
+
// Remove export keyword
|
|
38
|
+
transformedCode = transformedCode.replace(fullMatch, fullMatch.replace("export ", ""));
|
|
39
|
+
if (!directiveMatch || directiveMatch.index !== 0) {
|
|
40
|
+
// Server action
|
|
41
|
+
}
|
|
42
|
+
else {
|
|
43
|
+
// Client component
|
|
44
|
+
transformedCode += `
|
|
45
45
|
const ${exportName}Ref = Object.defineProperties(
|
|
46
|
-
${isClass
|
|
46
|
+
${isClass
|
|
47
|
+
? `class extends ${exportName} {
|
|
47
48
|
constructor(...args) { super(...args); }
|
|
48
|
-
}`
|
|
49
|
+
}`
|
|
50
|
+
: `function(...args) { return ${exportName}.apply(null, args); }`},
|
|
49
51
|
{
|
|
50
52
|
$$typeof: { value: Symbol.for("react.server.reference") },
|
|
51
|
-
$$
|
|
52
|
-
$$
|
|
53
|
+
$$src: { value: ${JSON.stringify(value)} },
|
|
54
|
+
$$id: { value: import.meta.url.slice(import.meta.url.indexOf("/${key}")}) + ${JSON.stringify("#" + exportName)} },
|
|
53
55
|
$$async: { value: true }
|
|
54
56
|
}
|
|
55
57
|
);
|
|
56
58
|
export { ${exportName}Ref as ${exportName} };
|
|
57
59
|
`;
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
}
|
|
72
|
-
};
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
return {
|
|
63
|
+
code: transformedCode,
|
|
64
|
+
map: new SourceMapGenerator({ file: path }).toString(),
|
|
65
|
+
};
|
|
66
|
+
}
|
|
67
|
+
catch (error) {
|
|
68
|
+
console.error(`[RSC] Error transforming client component: ${path}`, error);
|
|
69
|
+
throw error;
|
|
70
|
+
}
|
|
71
|
+
},
|
|
72
|
+
};
|
|
73
73
|
}
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
74
|
+
/**
|
|
75
|
+
* transformedCode += `
|
|
76
|
+
const ${exportName}Ref = Object.defineProperties(
|
|
77
|
+
${
|
|
78
|
+
isClass
|
|
79
|
+
? `class extends ${exportName} {
|
|
80
|
+
constructor(...args) { super(...args); }
|
|
81
|
+
}`
|
|
82
|
+
: `function(...args) { return ${exportName}.apply(null, args); }`
|
|
83
|
+
},
|
|
84
|
+
{
|
|
85
|
+
$$typeof: { value: Symbol.for("react.client.reference") },
|
|
86
|
+
$$id: { value: ${JSON.stringify(moduleId + "#" + exportName)} }
|
|
87
|
+
}
|
|
88
|
+
);
|
|
89
|
+
export { ${exportName}Ref as ${exportName} };`;
|
|
90
|
+
*/
|
|
@@ -11,5 +11,9 @@ export interface TransformerOptions {
|
|
|
11
11
|
* Optional validation function for module IDs
|
|
12
12
|
*/
|
|
13
13
|
validateModuleId?: (moduleId: string) => boolean;
|
|
14
|
+
/**
|
|
15
|
+
* The directory to use for the module IDs
|
|
16
|
+
*/
|
|
17
|
+
moduleBase: string;
|
|
14
18
|
}
|
|
15
19
|
//# sourceMappingURL=types.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../../plugin/transformer/types.ts"],"names":[],"mappings":"AAAA,MAAM,WAAW,+BAA+B;IAC9C,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB,QAAQ,CAAC,EAAE,CAAC,IAAI,EAAE,MAAM,EAAE,GAAG,EAAE,OAAO,KAAK,MAAM,CAAC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