@vitejs/plugin-rsc 0.5.17 → 0.5.18
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/{cjs-gAnoDF_x.js → cjs-D2v1gYgq.js} +31 -22
- package/dist/core/plugin.js +1 -1
- package/dist/index.js +2 -2
- package/dist/plugin-BGmSmdwL.js +27 -0
- package/dist/{plugin-cXbi5HUa.js → plugin-Cp12dr0Z.js} +376 -297
- package/dist/plugin.js +2 -2
- package/dist/plugins/cjs.js +1 -1
- package/package.json +5 -3
- package/dist/plugin-9NQn9bDA.js +0 -24
|
@@ -11,7 +11,10 @@ import { walk } from "estree-walker";
|
|
|
11
11
|
import { analyze } from "periscopic";
|
|
12
12
|
|
|
13
13
|
//#region src/transforms/cjs.ts
|
|
14
|
-
|
|
14
|
+
function __cjs_interop__(m) {
|
|
15
|
+
return m.__cjs_module_runner_transform || "default" in m && Object.keys(m).every((k) => k === "default" || m[k] === m.default[k]) ? m.default : m;
|
|
16
|
+
}
|
|
17
|
+
const CJS_INTEROP_HELPER = __cjs_interop__.toString().replace(/\n\s*/g, "");
|
|
15
18
|
function transformCjsToEsm(code, ast, options) {
|
|
16
19
|
const output = new MagicString(code);
|
|
17
20
|
const analyzed = analyze(ast);
|
|
@@ -67,29 +70,35 @@ function cjsModuleRunnerPlugin() {
|
|
|
67
70
|
name: "cjs-module-runner-transform",
|
|
68
71
|
apply: "serve",
|
|
69
72
|
applyToEnvironment: (env) => env.config.dev.moduleRunnerTransform,
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
id
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
73
|
+
transform: {
|
|
74
|
+
filter: {
|
|
75
|
+
id: /\/node_modules\//,
|
|
76
|
+
code: /\b(require|exports)\b/
|
|
77
|
+
},
|
|
78
|
+
async handler(code, id) {
|
|
79
|
+
if (id.includes("/node_modules/") && !id.startsWith(this.environment.config.cacheDir) && /\b(require|exports)\b/.test(code)) {
|
|
80
|
+
id = parseIdQuery(id).filename;
|
|
81
|
+
if (!/\.[cm]?js$/.test(id)) return;
|
|
82
|
+
if (id.endsWith(".mjs")) return;
|
|
83
|
+
if (id.endsWith(".js")) {
|
|
84
|
+
const pkgJsonPath = await findClosestPkgJsonPath(path.dirname(id));
|
|
85
|
+
if (pkgJsonPath) {
|
|
86
|
+
if (JSON.parse(fs.readFileSync(pkgJsonPath, "utf-8")).type === "module") return;
|
|
87
|
+
}
|
|
79
88
|
}
|
|
89
|
+
const [, , , hasModuleSyntax] = esModuleLexer.parse(code);
|
|
90
|
+
if (hasModuleSyntax) return;
|
|
91
|
+
const packageKey = extractPackageKey(id);
|
|
92
|
+
if (!warnedPackages.has(packageKey)) {
|
|
93
|
+
debug(`non-optimized CJS dependency in '${this.environment.name}' environment: ${id}`);
|
|
94
|
+
warnedPackages.add(packageKey);
|
|
95
|
+
}
|
|
96
|
+
const output = transformCjsToEsm(code, await parseAstAsync(code), { id }).output;
|
|
97
|
+
return {
|
|
98
|
+
code: output.toString(),
|
|
99
|
+
map: output.generateMap({ hires: "boundary" })
|
|
100
|
+
};
|
|
80
101
|
}
|
|
81
|
-
const [, , , hasModuleSyntax] = esModuleLexer.parse(code);
|
|
82
|
-
if (hasModuleSyntax) return;
|
|
83
|
-
const packageKey = extractPackageKey(id);
|
|
84
|
-
if (!warnedPackages.has(packageKey)) {
|
|
85
|
-
debug(`non-optimized CJS dependency in '${this.environment.name}' environment: ${id}`);
|
|
86
|
-
warnedPackages.add(packageKey);
|
|
87
|
-
}
|
|
88
|
-
const output = transformCjsToEsm(code, await parseAstAsync(code), { id }).output;
|
|
89
|
-
return {
|
|
90
|
-
code: output.toString(),
|
|
91
|
-
map: output.generateMap({ hires: "boundary" })
|
|
92
|
-
};
|
|
93
102
|
}
|
|
94
103
|
}
|
|
95
104
|
}];
|
package/dist/core/plugin.js
CHANGED
package/dist/index.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import "./cjs-
|
|
2
|
-
import { r as vitePluginRsc, t as getPluginApi } from "./plugin-
|
|
1
|
+
import "./cjs-D2v1gYgq.js";
|
|
2
|
+
import { r as vitePluginRsc, t as getPluginApi } from "./plugin-Cp12dr0Z.js";
|
|
3
3
|
import "./transforms-B2EJTNXG.js";
|
|
4
4
|
import "./rpc-EIuXyQpO.js";
|
|
5
5
|
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
//#region src/core/plugin.ts
|
|
2
|
+
function vitePluginRscCore() {
|
|
3
|
+
return [{
|
|
4
|
+
name: "rsc:patch-react-server-dom-webpack",
|
|
5
|
+
transform: {
|
|
6
|
+
filter: { code: "__webpack_require__" },
|
|
7
|
+
handler(originalCode, _id, _options) {
|
|
8
|
+
let code = originalCode;
|
|
9
|
+
if (code.includes("__webpack_require__.u")) code = code.replaceAll("__webpack_require__.u", "({}).u");
|
|
10
|
+
if (code.includes("__webpack_require__")) code = code.replaceAll("__webpack_require__", "__vite_rsc_require__");
|
|
11
|
+
if (code !== originalCode) return {
|
|
12
|
+
code,
|
|
13
|
+
map: null
|
|
14
|
+
};
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
}, {
|
|
18
|
+
name: "rsc:workaround-linked-dep",
|
|
19
|
+
apply: () => !import.meta.url.includes("/node_modules/"),
|
|
20
|
+
configEnvironment() {
|
|
21
|
+
return { build: { commonjsOptions: { include: [/\/node_modules\//, /\/vendor\/react-server-dom\//] } } };
|
|
22
|
+
}
|
|
23
|
+
}];
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
//#endregion
|
|
27
|
+
export { vitePluginRscCore as t };
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { n as __toESM } from "./chunk-Dj_d7TT4.js";
|
|
2
2
|
import { t as createDebug } from "./dist-yW9-EeG1.js";
|
|
3
|
-
import { t as vitePluginRscCore } from "./plugin-
|
|
4
|
-
import { t as cjsModuleRunnerPlugin } from "./cjs-
|
|
3
|
+
import { t as vitePluginRscCore } from "./plugin-BGmSmdwL.js";
|
|
4
|
+
import { t as cjsModuleRunnerPlugin } from "./cjs-D2v1gYgq.js";
|
|
5
5
|
import { i as toCssVirtual, n as parseIdQuery, r as parseReferenceValidationVirtual, t as parseCssVirtual } from "./shared-CGK4coF3.js";
|
|
6
6
|
import { a as hasDirective, n as transformDirectiveProxyExport, o as transformWrapExport, s as findDirectives, t as transformServerActionServer } from "./transforms-B2EJTNXG.js";
|
|
7
7
|
import { o as generateEncryptionKey, s as toBase64 } from "./encryption-utils-DdqSKS_O.js";
|
|
@@ -11,6 +11,7 @@ import assert from "node:assert";
|
|
|
11
11
|
import fs from "node:fs";
|
|
12
12
|
import path from "node:path";
|
|
13
13
|
import { fileURLToPath, pathToFileURL } from "node:url";
|
|
14
|
+
import { exactRegex, prefixRegex } from "@rolldown/pluginutils";
|
|
14
15
|
import * as esModuleLexer from "es-module-lexer";
|
|
15
16
|
import MagicString from "magic-string";
|
|
16
17
|
import { toNodeHandler } from "srvx/node";
|
|
@@ -176,14 +177,21 @@ function withRollupError(ctx, f) {
|
|
|
176
177
|
};
|
|
177
178
|
}
|
|
178
179
|
function createVirtualPlugin(name, load) {
|
|
179
|
-
|
|
180
|
+
const virtualId = "virtual:" + name;
|
|
181
|
+
const resolvedId = "\0" + virtualId;
|
|
180
182
|
return {
|
|
181
183
|
name: `rsc:virtual-${name}`,
|
|
182
|
-
resolveId
|
|
183
|
-
|
|
184
|
+
resolveId: {
|
|
185
|
+
filter: { id: exactRegex(virtualId) },
|
|
186
|
+
handler(source) {
|
|
187
|
+
if (source === virtualId) return resolvedId;
|
|
188
|
+
}
|
|
184
189
|
},
|
|
185
|
-
load
|
|
186
|
-
|
|
190
|
+
load: {
|
|
191
|
+
filter: { id: exactRegex(resolvedId) },
|
|
192
|
+
handler(id, options) {
|
|
193
|
+
if (id === resolvedId) return load.apply(this, [id, options]);
|
|
194
|
+
}
|
|
187
195
|
}
|
|
188
196
|
};
|
|
189
197
|
}
|
|
@@ -244,11 +252,14 @@ function ensureEnvironmentImportsEntryFallback({ environments }) {
|
|
|
244
252
|
function vitePluginImportEnvironment(manager) {
|
|
245
253
|
return [{
|
|
246
254
|
name: "rsc:import-environment",
|
|
247
|
-
resolveId
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
255
|
+
resolveId: {
|
|
256
|
+
filter: { id: exactRegex(ENV_IMPORTS_MANIFEST_PLACEHOLDER) },
|
|
257
|
+
handler(source) {
|
|
258
|
+
if (source === ENV_IMPORTS_MANIFEST_PLACEHOLDER) return {
|
|
259
|
+
id: ENV_IMPORTS_MANIFEST_PLACEHOLDER,
|
|
260
|
+
external: true
|
|
261
|
+
};
|
|
262
|
+
}
|
|
252
263
|
},
|
|
253
264
|
buildStart() {
|
|
254
265
|
if (this.environment.mode !== "build") return;
|
|
@@ -265,52 +276,55 @@ function vitePluginImportEnvironment(manager) {
|
|
|
265
276
|
}
|
|
266
277
|
}
|
|
267
278
|
},
|
|
268
|
-
transform: {
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
const
|
|
274
|
-
const
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
279
|
+
transform: {
|
|
280
|
+
filter: { code: "import.meta.viteRsc.import" },
|
|
281
|
+
async handler(code, id) {
|
|
282
|
+
if (!code.includes("import.meta.viteRsc.import")) return;
|
|
283
|
+
const { server } = manager;
|
|
284
|
+
const s = new MagicString(code);
|
|
285
|
+
for (const match of stripLiteral(code).matchAll(/import\.meta\.viteRsc\.import\s*(<[\s\S]*?>)?\s*\(([\s\S]*?)\)/dg)) {
|
|
286
|
+
const [argStart, argEnd] = match.indices[2];
|
|
287
|
+
const [specifier, options] = evalValue(`[${code.slice(argStart, argEnd).trim()}]`);
|
|
288
|
+
const environmentName = options.environment;
|
|
289
|
+
let resolvedId;
|
|
290
|
+
if (this.environment.mode === "dev") {
|
|
291
|
+
const targetEnv = server.environments[environmentName];
|
|
292
|
+
assert(targetEnv, `[vite-rsc] unknown environment '${environmentName}'`);
|
|
293
|
+
const resolved = await targetEnv.pluginContainer.resolveId(specifier, id);
|
|
294
|
+
assert(resolved, `[vite-rsc] failed to resolve '${specifier}' in environment '${environmentName}'`);
|
|
295
|
+
resolvedId = resolved.id;
|
|
296
|
+
} else {
|
|
297
|
+
const targetEnvConfig = manager.config.environments[environmentName];
|
|
298
|
+
assert(targetEnvConfig, `[vite-rsc] unknown environment '${environmentName}'`);
|
|
299
|
+
const resolved = await this.resolve(specifier, id);
|
|
300
|
+
assert(resolved, `[vite-rsc] failed to resolve '${specifier}' in environment '${environmentName}'`);
|
|
301
|
+
resolvedId = resolved.id;
|
|
302
|
+
}
|
|
303
|
+
const sourceEnv = this.environment.name;
|
|
304
|
+
const targetEnv = environmentName;
|
|
305
|
+
manager.environmentImportMetaMap[sourceEnv] ??= {};
|
|
306
|
+
manager.environmentImportMetaMap[sourceEnv][targetEnv] ??= {};
|
|
307
|
+
manager.environmentImportMetaMap[sourceEnv][targetEnv][resolvedId] = {
|
|
308
|
+
resolvedId,
|
|
309
|
+
targetEnv,
|
|
310
|
+
sourceEnv,
|
|
311
|
+
specifier
|
|
312
|
+
};
|
|
313
|
+
let replacement;
|
|
314
|
+
if (this.environment.mode === "dev") replacement = `globalThis.__VITE_ENVIRONMENT_RUNNER_IMPORT__(${JSON.stringify(environmentName)}, ${JSON.stringify(resolvedId)})`;
|
|
315
|
+
else {
|
|
316
|
+
const relativeId = manager.toRelativeId(resolvedId);
|
|
317
|
+
replacement = `(await import(${JSON.stringify(ENV_IMPORTS_MANIFEST_PLACEHOLDER)})).default[${JSON.stringify(relativeId)}]()`;
|
|
318
|
+
}
|
|
319
|
+
const [start, end] = match.indices[0];
|
|
320
|
+
s.overwrite(start, end, replacement);
|
|
289
321
|
}
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
manager.environmentImportMetaMap[sourceEnv][targetEnv] ??= {};
|
|
294
|
-
manager.environmentImportMetaMap[sourceEnv][targetEnv][resolvedId] = {
|
|
295
|
-
resolvedId,
|
|
296
|
-
targetEnv,
|
|
297
|
-
sourceEnv,
|
|
298
|
-
specifier
|
|
322
|
+
if (s.hasChanged()) return {
|
|
323
|
+
code: s.toString(),
|
|
324
|
+
map: s.generateMap({ hires: "boundary" })
|
|
299
325
|
};
|
|
300
|
-
let replacement;
|
|
301
|
-
if (this.environment.mode === "dev") replacement = `globalThis.__VITE_ENVIRONMENT_RUNNER_IMPORT__(${JSON.stringify(environmentName)}, ${JSON.stringify(resolvedId)})`;
|
|
302
|
-
else {
|
|
303
|
-
const relativeId = manager.toRelativeId(resolvedId);
|
|
304
|
-
replacement = `(await import(${JSON.stringify(ENV_IMPORTS_MANIFEST_PLACEHOLDER)})).default[${JSON.stringify(relativeId)}]()`;
|
|
305
|
-
}
|
|
306
|
-
const [start, end] = match.indices[0];
|
|
307
|
-
s.overwrite(start, end, replacement);
|
|
308
326
|
}
|
|
309
|
-
|
|
310
|
-
code: s.toString(),
|
|
311
|
-
map: s.generateMap({ hires: "boundary" })
|
|
312
|
-
};
|
|
313
|
-
} },
|
|
327
|
+
},
|
|
314
328
|
renderChunk(code, chunk) {
|
|
315
329
|
if (code.includes(ENV_IMPORTS_MANIFEST_PLACEHOLDER)) {
|
|
316
330
|
const replacement = normalizeRelativePath(path.relative(path.join(chunk.fileName, ".."), ENV_IMPORTS_MANIFEST_NAME));
|
|
@@ -363,10 +377,13 @@ function fromResolvedIdProxy(source) {
|
|
|
363
377
|
function vitePluginResolvedIdProxy() {
|
|
364
378
|
return {
|
|
365
379
|
name: "rsc:resolved-id-proxy",
|
|
366
|
-
resolveId: {
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
380
|
+
resolveId: {
|
|
381
|
+
filter: { id: prefixRegex(RESOLVED_ID_PROXY_PREFIX) },
|
|
382
|
+
handler(source) {
|
|
383
|
+
const originalId = fromResolvedIdProxy(source);
|
|
384
|
+
if (originalId !== void 0) return originalId;
|
|
385
|
+
}
|
|
386
|
+
}
|
|
370
387
|
};
|
|
371
388
|
}
|
|
372
389
|
|
|
@@ -377,13 +394,13 @@ function scanBuildStripPlugin({ manager }) {
|
|
|
377
394
|
name: "rsc:scan-strip",
|
|
378
395
|
apply: "build",
|
|
379
396
|
enforce: "post",
|
|
380
|
-
async
|
|
397
|
+
transform: { async handler(code, _id, _options) {
|
|
381
398
|
if (!manager.isScanBuild) return;
|
|
382
399
|
return {
|
|
383
400
|
code: await transformScanBuildStrip(code),
|
|
384
401
|
map: { mappings: "" }
|
|
385
402
|
};
|
|
386
|
-
}
|
|
403
|
+
} }
|
|
387
404
|
};
|
|
388
405
|
}
|
|
389
406
|
const importGlobRE = /\bimport\.meta\.glob(?:<\w+>)?\s*\(/g;
|
|
@@ -409,6 +426,7 @@ function validateImportPlugin() {
|
|
|
409
426
|
name: "rsc:validate-imports",
|
|
410
427
|
resolveId: {
|
|
411
428
|
order: "pre",
|
|
429
|
+
filter: { id: /^(client-only|server-only)$/ },
|
|
412
430
|
async handler(source, _importer, options) {
|
|
413
431
|
if ("scan" in options && options.scan) return;
|
|
414
432
|
if (source === "client-only" || source === "server-only") {
|
|
@@ -423,12 +441,16 @@ function validateImportPlugin() {
|
|
|
423
441
|
}
|
|
424
442
|
}
|
|
425
443
|
},
|
|
426
|
-
load
|
|
427
|
-
|
|
428
|
-
|
|
444
|
+
load: {
|
|
445
|
+
filter: { id: prefixRegex("\0virtual:vite-rsc/validate-imports/") },
|
|
446
|
+
handler(id) {
|
|
447
|
+
if (id.startsWith("\0virtual:vite-rsc/validate-imports/invalid/")) return `throw new Error("invalid import of '${id.slice(id.lastIndexOf("/") + 1)}'")`;
|
|
448
|
+
if (id.startsWith("\0virtual:vite-rsc/validate-imports/")) return `export {}`;
|
|
449
|
+
}
|
|
429
450
|
},
|
|
430
451
|
transform: {
|
|
431
452
|
order: "post",
|
|
453
|
+
filter: { id: prefixRegex("\0virtual:vite-rsc/validate-imports/invalid/") },
|
|
432
454
|
async handler(_code, id) {
|
|
433
455
|
if (this.environment.mode === "dev") {
|
|
434
456
|
if (id.startsWith(`\0virtual:vite-rsc/validate-imports/invalid/`)) validateImportChain(getImportChainDev(this.environment, id), this.environment.name, this.environment.config.root);
|
|
@@ -550,6 +572,7 @@ function vitePluginRscMinimal(rscPluginOptions = {}, manager = new RscPluginMana
|
|
|
550
572
|
name: "rsc:vite-client-raw-import",
|
|
551
573
|
transform: {
|
|
552
574
|
order: "post",
|
|
575
|
+
filter: { code: "__vite_rsc_raw_import__" },
|
|
553
576
|
handler(code) {
|
|
554
577
|
if (code.includes("__vite_rsc_raw_import__")) return code.replace("__vite_rsc_raw_import__", "import");
|
|
555
578
|
}
|
|
@@ -562,19 +585,22 @@ function vitePluginRscMinimal(rscPluginOptions = {}, manager = new RscPluginMana
|
|
|
562
585
|
{
|
|
563
586
|
name: "rsc:reference-validation",
|
|
564
587
|
apply: "serve",
|
|
565
|
-
load: {
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
|
|
588
|
+
load: {
|
|
589
|
+
filter: { id: prefixRegex("\0virtual:vite-rsc/reference-validation?") },
|
|
590
|
+
handler(id, _options) {
|
|
591
|
+
if (id.startsWith("\0virtual:vite-rsc/reference-validation?")) {
|
|
592
|
+
const parsed = parseReferenceValidationVirtual(id);
|
|
593
|
+
assert(parsed);
|
|
594
|
+
if (parsed.type === "client") {
|
|
595
|
+
if (Object.values(manager.clientReferenceMetaMap).find((meta) => meta.referenceKey === parsed.id)) return `export {}`;
|
|
596
|
+
}
|
|
597
|
+
if (parsed.type === "server") {
|
|
598
|
+
if (Object.values(manager.serverReferenceMetaMap).find((meta) => meta.referenceKey === parsed.id)) return `export {}`;
|
|
599
|
+
}
|
|
600
|
+
this.error(`[vite-rsc] invalid ${parsed.type} reference '${parsed.id}'`);
|
|
574
601
|
}
|
|
575
|
-
this.error(`[vite-rsc] invalid ${parsed.type} reference '${parsed.id}'`);
|
|
576
602
|
}
|
|
577
|
-
}
|
|
603
|
+
}
|
|
578
604
|
},
|
|
579
605
|
scanBuildStripPlugin({ manager }),
|
|
580
606
|
vitePluginResolvedIdProxy()
|
|
@@ -841,6 +867,7 @@ function vitePluginRsc(rscPluginOptions = {}) {
|
|
|
841
867
|
name: "rsc:react-server-dom-webpack-alias",
|
|
842
868
|
resolveId: {
|
|
843
869
|
order: "pre",
|
|
870
|
+
filter: { id: prefixRegex(`${PKG_NAME}/vendor/react-server-dom/`) },
|
|
844
871
|
async handler(source, importer, options) {
|
|
845
872
|
if (hasReactServerDomWebpack && source.startsWith(`${PKG_NAME}/vendor/react-server-dom/`)) {
|
|
846
873
|
const newSource = source.replace(`${PKG_NAME}/vendor/react-server-dom`, "react-server-dom-webpack");
|
|
@@ -854,40 +881,43 @@ function vitePluginRsc(rscPluginOptions = {}) {
|
|
|
854
881
|
},
|
|
855
882
|
{
|
|
856
883
|
name: "rsc:load-environment-module",
|
|
857
|
-
|
|
858
|
-
|
|
859
|
-
|
|
860
|
-
|
|
861
|
-
|
|
862
|
-
const
|
|
863
|
-
const
|
|
864
|
-
|
|
865
|
-
|
|
866
|
-
|
|
867
|
-
|
|
868
|
-
|
|
869
|
-
|
|
870
|
-
|
|
871
|
-
|
|
872
|
-
|
|
873
|
-
|
|
874
|
-
|
|
875
|
-
|
|
876
|
-
|
|
877
|
-
|
|
878
|
-
|
|
879
|
-
|
|
880
|
-
|
|
881
|
-
|
|
882
|
-
|
|
884
|
+
transform: {
|
|
885
|
+
filter: { code: "import.meta.viteRsc.loadModule" },
|
|
886
|
+
async handler(code) {
|
|
887
|
+
if (!code.includes("import.meta.viteRsc.loadModule")) return;
|
|
888
|
+
const { server } = manager;
|
|
889
|
+
const s = new MagicString(code);
|
|
890
|
+
for (const match of stripLiteral(code).matchAll(/import\.meta\.viteRsc\.loadModule\(([\s\S]*?)\)/dg)) {
|
|
891
|
+
const [argStart, argEnd] = match.indices[1];
|
|
892
|
+
const [environmentName, entryName] = evalValue(`[${code.slice(argStart, argEnd).trim()}]`);
|
|
893
|
+
let replacement;
|
|
894
|
+
if (this.environment.mode === "dev" && rscPluginOptions.loadModuleDevProxy) replacement = `import("virtual:vite-rsc/rpc-client").then((module) => module.createRpcClient(${JSON.stringify({
|
|
895
|
+
environmentName,
|
|
896
|
+
entryName
|
|
897
|
+
})}))`;
|
|
898
|
+
else if (this.environment.mode === "dev") {
|
|
899
|
+
const environment = server.environments[environmentName];
|
|
900
|
+
const source = getEntrySource(environment.config, entryName);
|
|
901
|
+
const resolved = await environment.pluginContainer.resolveId(source);
|
|
902
|
+
assert(resolved, `[vite-rsc] failed to resolve entry '${source}'`);
|
|
903
|
+
replacement = `globalThis.__VITE_ENVIRONMENT_RUNNER_IMPORT__(${JSON.stringify(environmentName)}, ${JSON.stringify(resolved.id)})`;
|
|
904
|
+
} else {
|
|
905
|
+
const environment = manager.config.environments[environmentName];
|
|
906
|
+
const targetName = entryName || getFallbackRollupEntry(environment.build.rollupOptions.input).name;
|
|
907
|
+
replacement = JSON.stringify(`__vite_rsc_load_module_start__:` + JSON.stringify({
|
|
908
|
+
fromEnv: this.environment.name,
|
|
909
|
+
toEnv: environmentName,
|
|
910
|
+
targetFileName: `${targetName}.js`
|
|
911
|
+
}) + `:__vite_rsc_load_module_end__`);
|
|
912
|
+
}
|
|
913
|
+
const [start, end] = match.indices[0];
|
|
914
|
+
s.overwrite(start, end, replacement);
|
|
883
915
|
}
|
|
884
|
-
|
|
885
|
-
|
|
916
|
+
if (s.hasChanged()) return {
|
|
917
|
+
code: s.toString(),
|
|
918
|
+
map: s.generateMap({ hires: "boundary" })
|
|
919
|
+
};
|
|
886
920
|
}
|
|
887
|
-
if (s.hasChanged()) return {
|
|
888
|
-
code: s.toString(),
|
|
889
|
-
map: s.generateMap({ hires: "boundary" })
|
|
890
|
-
};
|
|
891
921
|
},
|
|
892
922
|
renderChunk(code, chunk) {
|
|
893
923
|
if (!code.includes("__vite_rsc_load_module")) return;
|
|
@@ -941,15 +971,20 @@ function vitePluginRsc(rscPluginOptions = {}) {
|
|
|
941
971
|
},
|
|
942
972
|
{
|
|
943
973
|
name: "rsc:virtual:vite-rsc/rpc-client",
|
|
944
|
-
resolveId
|
|
945
|
-
|
|
974
|
+
resolveId: {
|
|
975
|
+
filter: { id: exactRegex("virtual:vite-rsc/rpc-client") },
|
|
976
|
+
handler(source) {
|
|
977
|
+
if (source === "virtual:vite-rsc/rpc-client") return `\0${source}`;
|
|
978
|
+
}
|
|
946
979
|
},
|
|
947
|
-
load
|
|
948
|
-
|
|
949
|
-
|
|
950
|
-
|
|
951
|
-
|
|
952
|
-
|
|
980
|
+
load: {
|
|
981
|
+
filter: { id: exactRegex("\0virtual:vite-rsc/rpc-client") },
|
|
982
|
+
handler(id) {
|
|
983
|
+
if (id === "\0virtual:vite-rsc/rpc-client") {
|
|
984
|
+
const { server } = manager;
|
|
985
|
+
const origin = server.resolvedUrls?.local[0];
|
|
986
|
+
assert(origin, "[vite-rsc] no server for loadModuleDevProxy");
|
|
987
|
+
return `\
|
|
953
988
|
import * as __vite_rsc_rpc from "@vitejs/plugin-rsc/utils/rpc";
|
|
954
989
|
export function createRpcClient(params) {
|
|
955
990
|
const endpoint =
|
|
@@ -959,30 +994,37 @@ export function createRpcClient(params) {
|
|
|
959
994
|
return __vite_rsc_rpc.createRpcClient({ endpoint });
|
|
960
995
|
}
|
|
961
996
|
`;
|
|
997
|
+
}
|
|
962
998
|
}
|
|
963
999
|
}
|
|
964
1000
|
},
|
|
965
1001
|
{
|
|
966
1002
|
name: "rsc:virtual:vite-rsc/assets-manifest",
|
|
967
|
-
resolveId
|
|
968
|
-
|
|
969
|
-
|
|
970
|
-
|
|
971
|
-
|
|
972
|
-
|
|
973
|
-
|
|
1003
|
+
resolveId: {
|
|
1004
|
+
filter: { id: exactRegex("virtual:vite-rsc/assets-manifest") },
|
|
1005
|
+
handler(source) {
|
|
1006
|
+
if (source === "virtual:vite-rsc/assets-manifest") {
|
|
1007
|
+
if (this.environment.mode === "build") return {
|
|
1008
|
+
id: source,
|
|
1009
|
+
external: true
|
|
1010
|
+
};
|
|
1011
|
+
return `\0` + source;
|
|
1012
|
+
}
|
|
974
1013
|
}
|
|
975
1014
|
},
|
|
976
|
-
load
|
|
977
|
-
|
|
978
|
-
|
|
979
|
-
|
|
980
|
-
|
|
981
|
-
|
|
982
|
-
|
|
983
|
-
|
|
984
|
-
|
|
985
|
-
|
|
1015
|
+
load: {
|
|
1016
|
+
filter: { id: exactRegex("\0virtual:vite-rsc/assets-manifest") },
|
|
1017
|
+
handler(id) {
|
|
1018
|
+
if (id === "\0virtual:vite-rsc/assets-manifest") {
|
|
1019
|
+
assert(this.environment.name !== "client");
|
|
1020
|
+
assert(this.environment.mode === "dev");
|
|
1021
|
+
const manifest = {
|
|
1022
|
+
bootstrapScriptContent: `import(${serializeValueWithRuntime(assetsURL("@id/__x00__" + VIRTUAL_ENTRIES.browser, manager))})`,
|
|
1023
|
+
clientReferenceDeps: {},
|
|
1024
|
+
cssLinkPrecedence: rscPluginOptions.cssLinkPrecedence
|
|
1025
|
+
};
|
|
1026
|
+
return `export default ${JSON.stringify(manifest, null, 2)}`;
|
|
1027
|
+
}
|
|
986
1028
|
}
|
|
987
1029
|
},
|
|
988
1030
|
generateBundle(_options, bundle) {
|
|
@@ -1050,25 +1092,28 @@ export default assetsManifest.bootstrapScriptContent;
|
|
|
1050
1092
|
}),
|
|
1051
1093
|
{
|
|
1052
1094
|
name: "rsc:bootstrap-script-content",
|
|
1053
|
-
|
|
1054
|
-
|
|
1055
|
-
|
|
1056
|
-
|
|
1057
|
-
|
|
1058
|
-
|
|
1059
|
-
const
|
|
1060
|
-
const
|
|
1061
|
-
|
|
1062
|
-
|
|
1063
|
-
|
|
1064
|
-
|
|
1065
|
-
|
|
1066
|
-
|
|
1067
|
-
|
|
1068
|
-
|
|
1069
|
-
code
|
|
1070
|
-
|
|
1071
|
-
|
|
1095
|
+
transform: {
|
|
1096
|
+
filter: { code: "loadBootstrapScriptContent" },
|
|
1097
|
+
async handler(code) {
|
|
1098
|
+
if (!code.includes("loadBootstrapScriptContent") || !/import\s*\.\s*meta\s*\.\s*viteRsc\s*\.\s*loadBootstrapScriptContent/.test(code)) return;
|
|
1099
|
+
assert(!rscPluginOptions.customClientEntry, `[vite-rsc] 'import.meta.viteRsc.loadBootstrapScriptContent' cannot be used with 'customClientEntry' option`);
|
|
1100
|
+
assert(this.environment.name !== "client");
|
|
1101
|
+
const output = new MagicString(code);
|
|
1102
|
+
for (const match of stripLiteral(code).matchAll(/import\s*\.\s*meta\s*\.\s*viteRsc\s*\.\s*loadBootstrapScriptContent\(([\s\S]*?)\)/dg)) {
|
|
1103
|
+
const [argStart, argEnd] = match.indices[1];
|
|
1104
|
+
const argCode = code.slice(argStart, argEnd).trim();
|
|
1105
|
+
assert(evalValue(argCode), `[vite-rsc] expected 'loadBootstrapScriptContent("index")' but got ${argCode}`);
|
|
1106
|
+
let replacement = `Promise.resolve(__vite_rsc_assets_manifest.bootstrapScriptContent)`;
|
|
1107
|
+
const [start, end] = match.indices[0];
|
|
1108
|
+
output.overwrite(start, end, replacement);
|
|
1109
|
+
}
|
|
1110
|
+
if (output.hasChanged()) {
|
|
1111
|
+
if (!code.includes("__vite_rsc_assets_manifest")) output.prepend(`import __vite_rsc_assets_manifest from "virtual:vite-rsc/assets-manifest";`);
|
|
1112
|
+
return {
|
|
1113
|
+
code: output.toString(),
|
|
1114
|
+
map: output.generateMap({ hires: "boundary" })
|
|
1115
|
+
};
|
|
1116
|
+
}
|
|
1072
1117
|
}
|
|
1073
1118
|
}
|
|
1074
1119
|
},
|
|
@@ -1124,9 +1169,12 @@ import.meta.hot.on("rsc:update", () => {
|
|
|
1124
1169
|
function globalAsyncLocalStoragePlugin() {
|
|
1125
1170
|
return [{
|
|
1126
1171
|
name: "rsc:inject-async-local-storage",
|
|
1127
|
-
transform: {
|
|
1128
|
-
|
|
1129
|
-
|
|
1172
|
+
transform: {
|
|
1173
|
+
filter: { code: "typeof AsyncLocalStorage" },
|
|
1174
|
+
handler(code) {
|
|
1175
|
+
if ((this.environment.name === "ssr" || this.environment.name === "rsc") && code.includes("typeof AsyncLocalStorage") && code.includes("new AsyncLocalStorage()") && !code.includes("__viteRscAsyncHooks")) return (this.environment.mode === "build" && !isRolldownVite ? `const __viteRscAsyncHooks = require("node:async_hooks");` : `import * as __viteRscAsyncHooks from "node:async_hooks";`) + `globalThis.AsyncLocalStorage = __viteRscAsyncHooks.AsyncLocalStorage;` + code;
|
|
1176
|
+
}
|
|
1177
|
+
}
|
|
1130
1178
|
}];
|
|
1131
1179
|
}
|
|
1132
1180
|
function vitePluginUseClient(useClientPluginOptions, manager) {
|
|
@@ -1143,7 +1191,7 @@ function vitePluginUseClient(useClientPluginOptions, manager) {
|
|
|
1143
1191
|
return [
|
|
1144
1192
|
{
|
|
1145
1193
|
name: "rsc:use-client",
|
|
1146
|
-
async
|
|
1194
|
+
transform: { async handler(code, id) {
|
|
1147
1195
|
if (this.environment.name !== serverEnvironmentName) return;
|
|
1148
1196
|
if (!code.includes("use client")) {
|
|
1149
1197
|
delete manager.clientReferenceMetaMap[id];
|
|
@@ -1206,87 +1254,96 @@ function vitePluginUseClient(useClientPluginOptions, manager) {
|
|
|
1206
1254
|
code: output.toString(),
|
|
1207
1255
|
map: { mappings: "" }
|
|
1208
1256
|
};
|
|
1209
|
-
}
|
|
1257
|
+
} }
|
|
1210
1258
|
},
|
|
1211
1259
|
{
|
|
1212
1260
|
name: "rsc:use-client/build-references",
|
|
1213
|
-
resolveId
|
|
1214
|
-
|
|
1261
|
+
resolveId: {
|
|
1262
|
+
filter: { id: prefixRegex("virtual:vite-rsc/client-references") },
|
|
1263
|
+
handler(source) {
|
|
1264
|
+
if (source.startsWith("virtual:vite-rsc/client-references")) return "\0" + source;
|
|
1265
|
+
}
|
|
1215
1266
|
},
|
|
1216
|
-
load
|
|
1217
|
-
|
|
1218
|
-
|
|
1219
|
-
|
|
1220
|
-
|
|
1221
|
-
|
|
1222
|
-
if (manager.isScanBuild) {
|
|
1223
|
-
let code = ``;
|
|
1224
|
-
for (const meta of Object.values(manager.clientReferenceMetaMap)) code += `import ${JSON.stringify(withResolvedIdProxy(meta.importId))};\n`;
|
|
1225
|
-
return {
|
|
1226
|
-
code,
|
|
1267
|
+
load: {
|
|
1268
|
+
filter: { id: prefixRegex("\0virtual:vite-rsc/client-references") },
|
|
1269
|
+
handler(id) {
|
|
1270
|
+
if (id === "\0virtual:vite-rsc/client-references") {
|
|
1271
|
+
if (this.environment.mode === "dev") return {
|
|
1272
|
+
code: `export default {}`,
|
|
1227
1273
|
map: null
|
|
1228
1274
|
};
|
|
1229
|
-
|
|
1230
|
-
|
|
1231
|
-
|
|
1232
|
-
|
|
1233
|
-
|
|
1234
|
-
|
|
1235
|
-
|
|
1236
|
-
|
|
1237
|
-
|
|
1238
|
-
|
|
1239
|
-
|
|
1240
|
-
|
|
1241
|
-
|
|
1242
|
-
|
|
1243
|
-
|
|
1244
|
-
|
|
1245
|
-
|
|
1246
|
-
|
|
1275
|
+
if (manager.isScanBuild) {
|
|
1276
|
+
let code = ``;
|
|
1277
|
+
for (const meta of Object.values(manager.clientReferenceMetaMap)) code += `import ${JSON.stringify(withResolvedIdProxy(meta.importId))};\n`;
|
|
1278
|
+
return {
|
|
1279
|
+
code,
|
|
1280
|
+
map: null
|
|
1281
|
+
};
|
|
1282
|
+
}
|
|
1283
|
+
let code = "";
|
|
1284
|
+
manager.clientReferenceGroups = {};
|
|
1285
|
+
for (const meta of Object.values(manager.clientReferenceMetaMap)) {
|
|
1286
|
+
if (!meta.serverChunk) continue;
|
|
1287
|
+
let name = useClientPluginOptions.clientChunks?.({
|
|
1288
|
+
id: meta.importId,
|
|
1289
|
+
normalizedId: manager.toRelativeId(meta.importId),
|
|
1290
|
+
serverChunk: meta.serverChunk
|
|
1291
|
+
}) ?? meta.serverChunk;
|
|
1292
|
+
name = cleanUrl(name.replaceAll("..", "__"));
|
|
1293
|
+
(manager.clientReferenceGroups[name] ??= []).push(meta);
|
|
1294
|
+
meta.groupChunkId = `\0virtual:vite-rsc/client-references/group/${name}`;
|
|
1295
|
+
}
|
|
1296
|
+
debug("client-reference-groups", manager.clientReferenceGroups);
|
|
1297
|
+
for (const [name, metas] of Object.entries(manager.clientReferenceGroups)) {
|
|
1298
|
+
const groupVirtual = `virtual:vite-rsc/client-references/group/${name}`;
|
|
1299
|
+
for (const meta of metas) code += `\
|
|
1247
1300
|
${JSON.stringify(meta.referenceKey)}: async () => {
|
|
1248
1301
|
const m = await import(${JSON.stringify(groupVirtual)});
|
|
1249
1302
|
return m.export_${meta.referenceKey};
|
|
1250
1303
|
},
|
|
1251
1304
|
`;
|
|
1305
|
+
}
|
|
1306
|
+
code = `export default {${code}};\n`;
|
|
1307
|
+
return {
|
|
1308
|
+
code,
|
|
1309
|
+
map: null
|
|
1310
|
+
};
|
|
1252
1311
|
}
|
|
1253
|
-
|
|
1254
|
-
|
|
1255
|
-
|
|
1256
|
-
|
|
1257
|
-
|
|
1258
|
-
|
|
1259
|
-
|
|
1260
|
-
|
|
1261
|
-
const metas = manager.clientReferenceGroups[name];
|
|
1262
|
-
assert(metas, `unknown client reference group: ${name}`);
|
|
1263
|
-
let code = ``;
|
|
1264
|
-
for (const meta of metas) {
|
|
1265
|
-
const exports = meta.renderedExports.map((name) => `${name}: import_${meta.referenceKey}.${name},\n`).sort().join("");
|
|
1266
|
-
code += `
|
|
1312
|
+
if (id.startsWith("\0virtual:vite-rsc/client-references/group/")) {
|
|
1313
|
+
const name = id.slice(42);
|
|
1314
|
+
const metas = manager.clientReferenceGroups[name];
|
|
1315
|
+
assert(metas, `unknown client reference group: ${name}`);
|
|
1316
|
+
let code = ``;
|
|
1317
|
+
for (const meta of metas) {
|
|
1318
|
+
const exports = meta.renderedExports.map((name) => `${name}: import_${meta.referenceKey}.${name},\n`).sort().join("");
|
|
1319
|
+
code += `
|
|
1267
1320
|
import * as import_${meta.referenceKey} from ${JSON.stringify(withResolvedIdProxy(meta.importId))};
|
|
1268
1321
|
export const export_${meta.referenceKey} = {${exports}};
|
|
1269
1322
|
`;
|
|
1323
|
+
}
|
|
1324
|
+
return {
|
|
1325
|
+
code,
|
|
1326
|
+
map: null
|
|
1327
|
+
};
|
|
1270
1328
|
}
|
|
1271
|
-
return {
|
|
1272
|
-
code,
|
|
1273
|
-
map: null
|
|
1274
|
-
};
|
|
1275
1329
|
}
|
|
1276
1330
|
}
|
|
1277
1331
|
},
|
|
1278
1332
|
{
|
|
1279
1333
|
name: "rsc:virtual-client-in-server-package",
|
|
1280
|
-
|
|
1281
|
-
|
|
1282
|
-
|
|
1283
|
-
|
|
1284
|
-
|
|
1285
|
-
|
|
1334
|
+
load: {
|
|
1335
|
+
filter: { id: prefixRegex("\0virtual:vite-rsc/client-in-server-package-proxy/") },
|
|
1336
|
+
async handler(id) {
|
|
1337
|
+
if (id.startsWith("\0virtual:vite-rsc/client-in-server-package-proxy/")) {
|
|
1338
|
+
assert.equal(this.environment.mode, "dev");
|
|
1339
|
+
assert(this.environment.name !== serverEnvironmentName);
|
|
1340
|
+
id = decodeURIComponent(id.slice(49));
|
|
1341
|
+
return `
|
|
1286
1342
|
export * from ${JSON.stringify(id)};
|
|
1287
1343
|
import * as __all__ from ${JSON.stringify(id)};
|
|
1288
1344
|
export default __all__.default;
|
|
1289
1345
|
`;
|
|
1346
|
+
}
|
|
1290
1347
|
}
|
|
1291
1348
|
}
|
|
1292
1349
|
},
|
|
@@ -1304,11 +1361,14 @@ function vitePluginUseClient(useClientPluginOptions, manager) {
|
|
|
1304
1361
|
}
|
|
1305
1362
|
}
|
|
1306
1363
|
},
|
|
1307
|
-
|
|
1308
|
-
|
|
1309
|
-
|
|
1310
|
-
|
|
1311
|
-
|
|
1364
|
+
load: {
|
|
1365
|
+
filter: { id: prefixRegex("\0virtual:vite-rsc/client-package-proxy/") },
|
|
1366
|
+
async handler(id) {
|
|
1367
|
+
if (id.startsWith("\0virtual:vite-rsc/client-package-proxy/")) {
|
|
1368
|
+
assert(this.environment.mode === "dev");
|
|
1369
|
+
const source = id.slice(39);
|
|
1370
|
+
return `export {${Object.values(manager.clientReferenceMetaMap).find((v) => v.packageSource === source).exportNames.join(",")}} from ${JSON.stringify(source)};\n`;
|
|
1371
|
+
}
|
|
1312
1372
|
}
|
|
1313
1373
|
},
|
|
1314
1374
|
generateBundle(_options, bundle) {
|
|
@@ -1392,16 +1452,22 @@ function vitePluginDefineEncryptionKey(useServerPluginOptions) {
|
|
|
1392
1452
|
async configEnvironment(name, _config, env) {
|
|
1393
1453
|
if (name === serverEnvironmentName && !env.isPreview) defineEncryptionKey = useServerPluginOptions.defineEncryptionKey ?? JSON.stringify(toBase64(await generateEncryptionKey()));
|
|
1394
1454
|
},
|
|
1395
|
-
resolveId
|
|
1396
|
-
|
|
1397
|
-
|
|
1398
|
-
|
|
1399
|
-
|
|
1455
|
+
resolveId: {
|
|
1456
|
+
filter: { id: exactRegex("virtual:vite-rsc/encryption-key") },
|
|
1457
|
+
handler(source) {
|
|
1458
|
+
if (source === "virtual:vite-rsc/encryption-key") return {
|
|
1459
|
+
id: "\0" + source,
|
|
1460
|
+
moduleSideEffects: false
|
|
1461
|
+
};
|
|
1462
|
+
}
|
|
1400
1463
|
},
|
|
1401
|
-
load
|
|
1402
|
-
|
|
1403
|
-
|
|
1404
|
-
|
|
1464
|
+
load: {
|
|
1465
|
+
filter: { id: exactRegex("\0virtual:vite-rsc/encryption-key") },
|
|
1466
|
+
handler(id) {
|
|
1467
|
+
if (id === "\0virtual:vite-rsc/encryption-key") {
|
|
1468
|
+
if (this.environment.mode === "build") return `export default () => ${KEY_PLACEHOLDER}`;
|
|
1469
|
+
return `export default () => (${defineEncryptionKey})`;
|
|
1470
|
+
}
|
|
1405
1471
|
}
|
|
1406
1472
|
},
|
|
1407
1473
|
renderChunk(code, chunk) {
|
|
@@ -1425,7 +1491,7 @@ function vitePluginUseServer(useServerPluginOptions, manager) {
|
|
|
1425
1491
|
const debug = createDebug("vite-rsc:use-server");
|
|
1426
1492
|
return [{
|
|
1427
1493
|
name: "rsc:use-server",
|
|
1428
|
-
async
|
|
1494
|
+
transform: { async handler(code, id) {
|
|
1429
1495
|
if (!code.includes("use server")) {
|
|
1430
1496
|
delete manager.serverReferenceMetaMap[id];
|
|
1431
1497
|
return;
|
|
@@ -1498,7 +1564,7 @@ function vitePluginUseServer(useServerPluginOptions, manager) {
|
|
|
1498
1564
|
map: output.generateMap({ hires: "boundary" })
|
|
1499
1565
|
};
|
|
1500
1566
|
}
|
|
1501
|
-
}
|
|
1567
|
+
} }
|
|
1502
1568
|
}, createVirtualPlugin("vite-rsc/server-references", function() {
|
|
1503
1569
|
if (this.environment.mode === "dev") return {
|
|
1504
1570
|
code: `export {}`,
|
|
@@ -1651,7 +1717,7 @@ function vitePluginRscCss(rscCssOptions = {}, manager) {
|
|
|
1651
1717
|
return [
|
|
1652
1718
|
{
|
|
1653
1719
|
name: "rsc:rsc-css-export-transform",
|
|
1654
|
-
async
|
|
1720
|
+
transform: { async handler(code, id) {
|
|
1655
1721
|
if (this.environment.name !== "rsc") return;
|
|
1656
1722
|
const filter = getRscCssTransformFilter({
|
|
1657
1723
|
id,
|
|
@@ -1667,13 +1733,14 @@ function vitePluginRscCss(rscCssOptions = {}, manager) {
|
|
|
1667
1733
|
code: result.output.toString(),
|
|
1668
1734
|
map: result.output.generateMap({ hires: "boundary" })
|
|
1669
1735
|
};
|
|
1670
|
-
}
|
|
1736
|
+
} }
|
|
1671
1737
|
},
|
|
1672
1738
|
{
|
|
1673
1739
|
name: "rsc:rsc-css-self-accept",
|
|
1674
1740
|
apply: "serve",
|
|
1675
1741
|
transform: {
|
|
1676
1742
|
order: "post",
|
|
1743
|
+
filter: { id: /\.(css|less|sass|scss|styl|stylus|pcss|postcss|sss)(\?|$)/ },
|
|
1677
1744
|
handler(_code, id, _options) {
|
|
1678
1745
|
if (this.environment.name === "client" && this.environment.mode === "dev" && isCSSRequest(id) && directRequestRE.test(id)) {
|
|
1679
1746
|
const mod = this.environment.moduleGraph.getModuleById(id);
|
|
@@ -1684,19 +1751,25 @@ function vitePluginRscCss(rscCssOptions = {}, manager) {
|
|
|
1684
1751
|
},
|
|
1685
1752
|
{
|
|
1686
1753
|
name: "rsc:css-virtual",
|
|
1687
|
-
resolveId
|
|
1688
|
-
|
|
1754
|
+
resolveId: {
|
|
1755
|
+
filter: { id: prefixRegex("virtual:vite-rsc/css?") },
|
|
1756
|
+
handler(source) {
|
|
1757
|
+
if (source.startsWith("virtual:vite-rsc/css?")) return "\0" + source;
|
|
1758
|
+
}
|
|
1689
1759
|
},
|
|
1690
|
-
|
|
1691
|
-
|
|
1692
|
-
|
|
1693
|
-
|
|
1694
|
-
|
|
1695
|
-
|
|
1696
|
-
|
|
1697
|
-
|
|
1698
|
-
|
|
1699
|
-
|
|
1760
|
+
load: {
|
|
1761
|
+
filter: { id: prefixRegex("\0virtual:vite-rsc/css?") },
|
|
1762
|
+
async handler(id) {
|
|
1763
|
+
const parsed = parseCssVirtual(id);
|
|
1764
|
+
if (parsed?.type === "ssr") {
|
|
1765
|
+
id = parsed.id;
|
|
1766
|
+
const { server } = manager;
|
|
1767
|
+
const mod = await server.environments.ssr.moduleGraph.getModuleByUrl(id);
|
|
1768
|
+
if (!mod?.id || !mod?.file) return `export default []`;
|
|
1769
|
+
const result = collectCss(server.environments.ssr, mod.id);
|
|
1770
|
+
for (const file of [mod.file, ...result.visitedFiles]) this.addWatchFile(file);
|
|
1771
|
+
return `export default ${serializeValueWithRuntime(result.hrefs.map((href) => assetsURL(href.slice(1), manager)))}`;
|
|
1772
|
+
}
|
|
1700
1773
|
}
|
|
1701
1774
|
}
|
|
1702
1775
|
},
|
|
@@ -1715,73 +1788,79 @@ function vitePluginRscCss(rscCssOptions = {}, manager) {
|
|
|
1715
1788
|
return original.apply(this, args);
|
|
1716
1789
|
};
|
|
1717
1790
|
},
|
|
1718
|
-
|
|
1719
|
-
|
|
1720
|
-
|
|
1721
|
-
|
|
1722
|
-
|
|
1723
|
-
|
|
1724
|
-
|
|
1725
|
-
const
|
|
1726
|
-
|
|
1727
|
-
|
|
1728
|
-
|
|
1729
|
-
|
|
1730
|
-
|
|
1731
|
-
|
|
1732
|
-
|
|
1733
|
-
|
|
1734
|
-
|
|
1735
|
-
|
|
1791
|
+
transform: {
|
|
1792
|
+
filter: { code: "import.meta.viteRsc.loadCss" },
|
|
1793
|
+
async handler(code, id) {
|
|
1794
|
+
if (!code.includes("import.meta.viteRsc.loadCss")) return;
|
|
1795
|
+
assert(this.environment.name === "rsc");
|
|
1796
|
+
const output = new MagicString(code);
|
|
1797
|
+
let importAdded = false;
|
|
1798
|
+
for (const match of stripLiteral(code).matchAll(/import\.meta\.viteRsc\.loadCss\(([\s\S]*?)\)/dg)) {
|
|
1799
|
+
const [start, end] = match.indices[0];
|
|
1800
|
+
const [argStart, argEnd] = match.indices[1];
|
|
1801
|
+
const argCode = code.slice(argStart, argEnd).trim();
|
|
1802
|
+
let importer = id;
|
|
1803
|
+
if (argCode) {
|
|
1804
|
+
const argValue = evalValue(argCode);
|
|
1805
|
+
const resolved = await this.resolve(argValue, id);
|
|
1806
|
+
if (resolved) importer = resolved.id;
|
|
1807
|
+
else {
|
|
1808
|
+
this.warn(`[vite-rsc] failed to transform 'import.meta.viteRsc.loadCss(${argCode})'`);
|
|
1809
|
+
output.update(start, end, `null`);
|
|
1810
|
+
continue;
|
|
1811
|
+
}
|
|
1736
1812
|
}
|
|
1737
|
-
|
|
1738
|
-
|
|
1739
|
-
|
|
1740
|
-
|
|
1741
|
-
|
|
1742
|
-
|
|
1743
|
-
if (this.environment.mode === "dev") replacement = `__vite_rsc_react__.createElement(async () => {
|
|
1813
|
+
const importId = toCssVirtual({
|
|
1814
|
+
id: importer,
|
|
1815
|
+
type: "rsc"
|
|
1816
|
+
});
|
|
1817
|
+
let replacement;
|
|
1818
|
+
if (this.environment.mode === "dev") replacement = `__vite_rsc_react__.createElement(async () => {
|
|
1744
1819
|
const __m = await import(${JSON.stringify(importId)});
|
|
1745
1820
|
return __vite_rsc_react__.createElement(__m.Resources);
|
|
1746
1821
|
})`;
|
|
1747
|
-
|
|
1748
|
-
|
|
1749
|
-
|
|
1750
|
-
|
|
1751
|
-
|
|
1822
|
+
else {
|
|
1823
|
+
const hash = hashString(importId);
|
|
1824
|
+
if (!importAdded && !code.includes(`__vite_rsc_importer_resources_${hash}`)) {
|
|
1825
|
+
importAdded = true;
|
|
1826
|
+
output.prepend(`import * as __vite_rsc_importer_resources_${hash} from ${JSON.stringify(importId)};`);
|
|
1827
|
+
}
|
|
1828
|
+
replacement = `__vite_rsc_react__.createElement(__vite_rsc_importer_resources_${hash}.Resources)`;
|
|
1752
1829
|
}
|
|
1753
|
-
|
|
1830
|
+
output.update(start, end, replacement);
|
|
1831
|
+
}
|
|
1832
|
+
if (output.hasChanged()) {
|
|
1833
|
+
if (!code.includes("__vite_rsc_react__")) output.prepend(`import __vite_rsc_react__ from "react";`);
|
|
1834
|
+
return {
|
|
1835
|
+
code: output.toString(),
|
|
1836
|
+
map: output.generateMap({ hires: "boundary" })
|
|
1837
|
+
};
|
|
1754
1838
|
}
|
|
1755
|
-
output.update(start, end, replacement);
|
|
1756
|
-
}
|
|
1757
|
-
if (output.hasChanged()) {
|
|
1758
|
-
if (!code.includes("__vite_rsc_react__")) output.prepend(`import __vite_rsc_react__ from "react";`);
|
|
1759
|
-
return {
|
|
1760
|
-
code: output.toString(),
|
|
1761
|
-
map: output.generateMap({ hires: "boundary" })
|
|
1762
|
-
};
|
|
1763
1839
|
}
|
|
1764
1840
|
},
|
|
1765
|
-
load
|
|
1766
|
-
|
|
1767
|
-
|
|
1768
|
-
|
|
1769
|
-
|
|
1770
|
-
|
|
1771
|
-
|
|
1772
|
-
const
|
|
1773
|
-
|
|
1774
|
-
|
|
1775
|
-
|
|
1776
|
-
|
|
1777
|
-
|
|
1778
|
-
|
|
1779
|
-
|
|
1780
|
-
|
|
1781
|
-
|
|
1841
|
+
load: {
|
|
1842
|
+
filter: { id: prefixRegex("\0virtual:vite-rsc/css?") },
|
|
1843
|
+
handler(id) {
|
|
1844
|
+
const { server } = manager;
|
|
1845
|
+
const parsed = parseCssVirtual(id);
|
|
1846
|
+
if (parsed?.type === "rsc") {
|
|
1847
|
+
assert(this.environment.name === "rsc");
|
|
1848
|
+
const importer = parsed.id;
|
|
1849
|
+
if (this.environment.mode === "dev") {
|
|
1850
|
+
const result = collectCss(server.environments.rsc, importer);
|
|
1851
|
+
for (const file of [importer, ...result.visitedFiles]) this.addWatchFile(file);
|
|
1852
|
+
return generateResourcesCode(serializeValueWithRuntime(assetsURLOfDeps({
|
|
1853
|
+
css: result.hrefs.map((href) => href.slice(1)),
|
|
1854
|
+
js: []
|
|
1855
|
+
}, manager)), manager, { cssLinkPrecedence: rscCssOptions.cssLinkPrecedence });
|
|
1856
|
+
} else {
|
|
1857
|
+
const key = manager.toRelativeId(importer);
|
|
1858
|
+
manager.serverResourcesMetaMap[importer] = { key };
|
|
1859
|
+
return `
|
|
1782
1860
|
import __vite_rsc_assets_manifest__ from "virtual:vite-rsc/assets-manifest";
|
|
1783
1861
|
${generateResourcesCode(`__vite_rsc_assets_manifest__.serverResources[${JSON.stringify(key)}]`, manager, { cssLinkPrecedence: rscCssOptions.cssLinkPrecedence })}
|
|
1784
1862
|
`;
|
|
1863
|
+
}
|
|
1785
1864
|
}
|
|
1786
1865
|
}
|
|
1787
1866
|
}
|
package/dist/plugin.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import "./cjs-
|
|
2
|
-
import { i as vitePluginRscMinimal, n as transformRscCssExport, r as vitePluginRsc, t as getPluginApi } from "./plugin-
|
|
1
|
+
import "./cjs-D2v1gYgq.js";
|
|
2
|
+
import { i as vitePluginRscMinimal, n as transformRscCssExport, r as vitePluginRsc, t as getPluginApi } from "./plugin-Cp12dr0Z.js";
|
|
3
3
|
import "./transforms-B2EJTNXG.js";
|
|
4
4
|
import "./rpc-EIuXyQpO.js";
|
|
5
5
|
|
package/dist/plugins/cjs.js
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@vitejs/plugin-rsc",
|
|
3
|
-
"version": "0.5.
|
|
3
|
+
"version": "0.5.18",
|
|
4
4
|
"description": "React Server Components (RSC) support for Vite.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"react",
|
|
@@ -39,6 +39,7 @@
|
|
|
39
39
|
"prepack": "tsdown"
|
|
40
40
|
},
|
|
41
41
|
"dependencies": {
|
|
42
|
+
"@rolldown/pluginutils": "1.0.0-rc.2",
|
|
42
43
|
"es-module-lexer": "^2.0.0",
|
|
43
44
|
"estree-walker": "^3.0.3",
|
|
44
45
|
"magic-string": "^0.30.21",
|
|
@@ -50,14 +51,15 @@
|
|
|
50
51
|
},
|
|
51
52
|
"devDependencies": {
|
|
52
53
|
"@hiogawa/utils": "^1.7.0",
|
|
53
|
-
"@playwright/test": "^1.58.
|
|
54
|
+
"@playwright/test": "^1.58.1",
|
|
54
55
|
"@tsconfig/strictest": "^2.0.8",
|
|
55
56
|
"@types/estree": "^1.0.8",
|
|
56
57
|
"@types/node": "^24.10.9",
|
|
57
|
-
"@types/react": "^19.2.
|
|
58
|
+
"@types/react": "^19.2.10",
|
|
58
59
|
"@types/react-dom": "^19.2.3",
|
|
59
60
|
"@vitejs/plugin-react": "workspace:*",
|
|
60
61
|
"@vitejs/test-dep-cjs-and-esm": "./test-dep/cjs-and-esm",
|
|
62
|
+
"@vitejs/test-dep-cjs-falsy-primitive": "./test-dep/cjs-falsy-primitive",
|
|
61
63
|
"picocolors": "^1.1.1",
|
|
62
64
|
"react": "^19.2.4",
|
|
63
65
|
"react-dom": "^19.2.4",
|
package/dist/plugin-9NQn9bDA.js
DELETED
|
@@ -1,24 +0,0 @@
|
|
|
1
|
-
//#region src/core/plugin.ts
|
|
2
|
-
function vitePluginRscCore() {
|
|
3
|
-
return [{
|
|
4
|
-
name: "rsc:patch-react-server-dom-webpack",
|
|
5
|
-
transform(originalCode, _id, _options) {
|
|
6
|
-
let code = originalCode;
|
|
7
|
-
if (code.includes("__webpack_require__.u")) code = code.replaceAll("__webpack_require__.u", "({}).u");
|
|
8
|
-
if (code.includes("__webpack_require__")) code = code.replaceAll("__webpack_require__", "__vite_rsc_require__");
|
|
9
|
-
if (code !== originalCode) return {
|
|
10
|
-
code,
|
|
11
|
-
map: null
|
|
12
|
-
};
|
|
13
|
-
}
|
|
14
|
-
}, {
|
|
15
|
-
name: "rsc:workaround-linked-dep",
|
|
16
|
-
apply: () => !import.meta.url.includes("/node_modules/"),
|
|
17
|
-
configEnvironment() {
|
|
18
|
-
return { build: { commonjsOptions: { include: [/\/node_modules\//, /\/vendor\/react-server-dom\//] } } };
|
|
19
|
-
}
|
|
20
|
-
}];
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
//#endregion
|
|
24
|
-
export { vitePluginRscCore as t };
|