@vitejs/plugin-rsc 0.5.17 → 0.5.19
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-Xwi_Uxiv.js} +383 -301
- 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-Xwi_Uxiv.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,12 +394,15 @@ function scanBuildStripPlugin({ manager }) {
|
|
|
377
394
|
name: "rsc:scan-strip",
|
|
378
395
|
apply: "build",
|
|
379
396
|
enforce: "post",
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
397
|
+
transform: {
|
|
398
|
+
filter: { id: { exclude: exactRegex("\0rolldown/runtime.js") } },
|
|
399
|
+
async handler(code, _id, _options) {
|
|
400
|
+
if (!manager.isScanBuild) return;
|
|
401
|
+
return {
|
|
402
|
+
code: await transformScanBuildStrip(code),
|
|
403
|
+
map: { mappings: "" }
|
|
404
|
+
};
|
|
405
|
+
}
|
|
386
406
|
}
|
|
387
407
|
};
|
|
388
408
|
}
|
|
@@ -409,6 +429,7 @@ function validateImportPlugin() {
|
|
|
409
429
|
name: "rsc:validate-imports",
|
|
410
430
|
resolveId: {
|
|
411
431
|
order: "pre",
|
|
432
|
+
filter: { id: /^(client-only|server-only)$/ },
|
|
412
433
|
async handler(source, _importer, options) {
|
|
413
434
|
if ("scan" in options && options.scan) return;
|
|
414
435
|
if (source === "client-only" || source === "server-only") {
|
|
@@ -423,12 +444,16 @@ function validateImportPlugin() {
|
|
|
423
444
|
}
|
|
424
445
|
}
|
|
425
446
|
},
|
|
426
|
-
load
|
|
427
|
-
|
|
428
|
-
|
|
447
|
+
load: {
|
|
448
|
+
filter: { id: prefixRegex("\0virtual:vite-rsc/validate-imports/") },
|
|
449
|
+
handler(id) {
|
|
450
|
+
if (id.startsWith("\0virtual:vite-rsc/validate-imports/invalid/")) return `throw new Error("invalid import of '${id.slice(id.lastIndexOf("/") + 1)}'")`;
|
|
451
|
+
if (id.startsWith("\0virtual:vite-rsc/validate-imports/")) return `export {}`;
|
|
452
|
+
}
|
|
429
453
|
},
|
|
430
454
|
transform: {
|
|
431
455
|
order: "post",
|
|
456
|
+
filter: { id: prefixRegex("\0virtual:vite-rsc/validate-imports/invalid/") },
|
|
432
457
|
async handler(_code, id) {
|
|
433
458
|
if (this.environment.mode === "dev") {
|
|
434
459
|
if (id.startsWith(`\0virtual:vite-rsc/validate-imports/invalid/`)) validateImportChain(getImportChainDev(this.environment, id), this.environment.name, this.environment.config.root);
|
|
@@ -550,6 +575,7 @@ function vitePluginRscMinimal(rscPluginOptions = {}, manager = new RscPluginMana
|
|
|
550
575
|
name: "rsc:vite-client-raw-import",
|
|
551
576
|
transform: {
|
|
552
577
|
order: "post",
|
|
578
|
+
filter: { code: "__vite_rsc_raw_import__" },
|
|
553
579
|
handler(code) {
|
|
554
580
|
if (code.includes("__vite_rsc_raw_import__")) return code.replace("__vite_rsc_raw_import__", "import");
|
|
555
581
|
}
|
|
@@ -562,19 +588,22 @@ function vitePluginRscMinimal(rscPluginOptions = {}, manager = new RscPluginMana
|
|
|
562
588
|
{
|
|
563
589
|
name: "rsc:reference-validation",
|
|
564
590
|
apply: "serve",
|
|
565
|
-
load: {
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
|
|
591
|
+
load: {
|
|
592
|
+
filter: { id: prefixRegex("\0virtual:vite-rsc/reference-validation?") },
|
|
593
|
+
handler(id, _options) {
|
|
594
|
+
if (id.startsWith("\0virtual:vite-rsc/reference-validation?")) {
|
|
595
|
+
const parsed = parseReferenceValidationVirtual(id);
|
|
596
|
+
assert(parsed);
|
|
597
|
+
if (parsed.type === "client") {
|
|
598
|
+
if (Object.values(manager.clientReferenceMetaMap).find((meta) => meta.referenceKey === parsed.id)) return `export {}`;
|
|
599
|
+
}
|
|
600
|
+
if (parsed.type === "server") {
|
|
601
|
+
if (Object.values(manager.serverReferenceMetaMap).find((meta) => meta.referenceKey === parsed.id)) return `export {}`;
|
|
602
|
+
}
|
|
603
|
+
this.error(`[vite-rsc] invalid ${parsed.type} reference '${parsed.id}'`);
|
|
574
604
|
}
|
|
575
|
-
this.error(`[vite-rsc] invalid ${parsed.type} reference '${parsed.id}'`);
|
|
576
605
|
}
|
|
577
|
-
}
|
|
606
|
+
}
|
|
578
607
|
},
|
|
579
608
|
scanBuildStripPlugin({ manager }),
|
|
580
609
|
vitePluginResolvedIdProxy()
|
|
@@ -841,6 +870,7 @@ function vitePluginRsc(rscPluginOptions = {}) {
|
|
|
841
870
|
name: "rsc:react-server-dom-webpack-alias",
|
|
842
871
|
resolveId: {
|
|
843
872
|
order: "pre",
|
|
873
|
+
filter: { id: prefixRegex(`${PKG_NAME}/vendor/react-server-dom/`) },
|
|
844
874
|
async handler(source, importer, options) {
|
|
845
875
|
if (hasReactServerDomWebpack && source.startsWith(`${PKG_NAME}/vendor/react-server-dom/`)) {
|
|
846
876
|
const newSource = source.replace(`${PKG_NAME}/vendor/react-server-dom`, "react-server-dom-webpack");
|
|
@@ -854,40 +884,43 @@ function vitePluginRsc(rscPluginOptions = {}) {
|
|
|
854
884
|
},
|
|
855
885
|
{
|
|
856
886
|
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
|
-
|
|
887
|
+
transform: {
|
|
888
|
+
filter: { code: "import.meta.viteRsc.loadModule" },
|
|
889
|
+
async handler(code) {
|
|
890
|
+
if (!code.includes("import.meta.viteRsc.loadModule")) return;
|
|
891
|
+
const { server } = manager;
|
|
892
|
+
const s = new MagicString(code);
|
|
893
|
+
for (const match of stripLiteral(code).matchAll(/import\.meta\.viteRsc\.loadModule\(([\s\S]*?)\)/dg)) {
|
|
894
|
+
const [argStart, argEnd] = match.indices[1];
|
|
895
|
+
const [environmentName, entryName] = evalValue(`[${code.slice(argStart, argEnd).trim()}]`);
|
|
896
|
+
let replacement;
|
|
897
|
+
if (this.environment.mode === "dev" && rscPluginOptions.loadModuleDevProxy) replacement = `import("virtual:vite-rsc/rpc-client").then((module) => module.createRpcClient(${JSON.stringify({
|
|
898
|
+
environmentName,
|
|
899
|
+
entryName
|
|
900
|
+
})}))`;
|
|
901
|
+
else if (this.environment.mode === "dev") {
|
|
902
|
+
const environment = server.environments[environmentName];
|
|
903
|
+
const source = getEntrySource(environment.config, entryName);
|
|
904
|
+
const resolved = await environment.pluginContainer.resolveId(source);
|
|
905
|
+
assert(resolved, `[vite-rsc] failed to resolve entry '${source}'`);
|
|
906
|
+
replacement = `globalThis.__VITE_ENVIRONMENT_RUNNER_IMPORT__(${JSON.stringify(environmentName)}, ${JSON.stringify(resolved.id)})`;
|
|
907
|
+
} else {
|
|
908
|
+
const environment = manager.config.environments[environmentName];
|
|
909
|
+
const targetName = entryName || getFallbackRollupEntry(environment.build.rollupOptions.input).name;
|
|
910
|
+
replacement = JSON.stringify(`__vite_rsc_load_module_start__:` + JSON.stringify({
|
|
911
|
+
fromEnv: this.environment.name,
|
|
912
|
+
toEnv: environmentName,
|
|
913
|
+
targetFileName: `${targetName}.js`
|
|
914
|
+
}) + `:__vite_rsc_load_module_end__`);
|
|
915
|
+
}
|
|
916
|
+
const [start, end] = match.indices[0];
|
|
917
|
+
s.overwrite(start, end, replacement);
|
|
883
918
|
}
|
|
884
|
-
|
|
885
|
-
|
|
919
|
+
if (s.hasChanged()) return {
|
|
920
|
+
code: s.toString(),
|
|
921
|
+
map: s.generateMap({ hires: "boundary" })
|
|
922
|
+
};
|
|
886
923
|
}
|
|
887
|
-
if (s.hasChanged()) return {
|
|
888
|
-
code: s.toString(),
|
|
889
|
-
map: s.generateMap({ hires: "boundary" })
|
|
890
|
-
};
|
|
891
924
|
},
|
|
892
925
|
renderChunk(code, chunk) {
|
|
893
926
|
if (!code.includes("__vite_rsc_load_module")) return;
|
|
@@ -941,15 +974,20 @@ function vitePluginRsc(rscPluginOptions = {}) {
|
|
|
941
974
|
},
|
|
942
975
|
{
|
|
943
976
|
name: "rsc:virtual:vite-rsc/rpc-client",
|
|
944
|
-
resolveId
|
|
945
|
-
|
|
977
|
+
resolveId: {
|
|
978
|
+
filter: { id: exactRegex("virtual:vite-rsc/rpc-client") },
|
|
979
|
+
handler(source) {
|
|
980
|
+
if (source === "virtual:vite-rsc/rpc-client") return `\0${source}`;
|
|
981
|
+
}
|
|
946
982
|
},
|
|
947
|
-
load
|
|
948
|
-
|
|
949
|
-
|
|
950
|
-
|
|
951
|
-
|
|
952
|
-
|
|
983
|
+
load: {
|
|
984
|
+
filter: { id: exactRegex("\0virtual:vite-rsc/rpc-client") },
|
|
985
|
+
handler(id) {
|
|
986
|
+
if (id === "\0virtual:vite-rsc/rpc-client") {
|
|
987
|
+
const { server } = manager;
|
|
988
|
+
const origin = server.resolvedUrls?.local[0];
|
|
989
|
+
assert(origin, "[vite-rsc] no server for loadModuleDevProxy");
|
|
990
|
+
return `\
|
|
953
991
|
import * as __vite_rsc_rpc from "@vitejs/plugin-rsc/utils/rpc";
|
|
954
992
|
export function createRpcClient(params) {
|
|
955
993
|
const endpoint =
|
|
@@ -959,30 +997,37 @@ export function createRpcClient(params) {
|
|
|
959
997
|
return __vite_rsc_rpc.createRpcClient({ endpoint });
|
|
960
998
|
}
|
|
961
999
|
`;
|
|
1000
|
+
}
|
|
962
1001
|
}
|
|
963
1002
|
}
|
|
964
1003
|
},
|
|
965
1004
|
{
|
|
966
1005
|
name: "rsc:virtual:vite-rsc/assets-manifest",
|
|
967
|
-
resolveId
|
|
968
|
-
|
|
969
|
-
|
|
970
|
-
|
|
971
|
-
|
|
972
|
-
|
|
973
|
-
|
|
1006
|
+
resolveId: {
|
|
1007
|
+
filter: { id: exactRegex("virtual:vite-rsc/assets-manifest") },
|
|
1008
|
+
handler(source) {
|
|
1009
|
+
if (source === "virtual:vite-rsc/assets-manifest") {
|
|
1010
|
+
if (this.environment.mode === "build") return {
|
|
1011
|
+
id: source,
|
|
1012
|
+
external: true
|
|
1013
|
+
};
|
|
1014
|
+
return `\0` + source;
|
|
1015
|
+
}
|
|
974
1016
|
}
|
|
975
1017
|
},
|
|
976
|
-
load
|
|
977
|
-
|
|
978
|
-
|
|
979
|
-
|
|
980
|
-
|
|
981
|
-
|
|
982
|
-
|
|
983
|
-
|
|
984
|
-
|
|
985
|
-
|
|
1018
|
+
load: {
|
|
1019
|
+
filter: { id: exactRegex("\0virtual:vite-rsc/assets-manifest") },
|
|
1020
|
+
handler(id) {
|
|
1021
|
+
if (id === "\0virtual:vite-rsc/assets-manifest") {
|
|
1022
|
+
assert(this.environment.name !== "client");
|
|
1023
|
+
assert(this.environment.mode === "dev");
|
|
1024
|
+
const manifest = {
|
|
1025
|
+
bootstrapScriptContent: `import(${serializeValueWithRuntime(assetsURL("@id/__x00__" + VIRTUAL_ENTRIES.browser, manager))})`,
|
|
1026
|
+
clientReferenceDeps: {},
|
|
1027
|
+
cssLinkPrecedence: rscPluginOptions.cssLinkPrecedence
|
|
1028
|
+
};
|
|
1029
|
+
return `export default ${JSON.stringify(manifest, null, 2)}`;
|
|
1030
|
+
}
|
|
986
1031
|
}
|
|
987
1032
|
},
|
|
988
1033
|
generateBundle(_options, bundle) {
|
|
@@ -1050,25 +1095,28 @@ export default assetsManifest.bootstrapScriptContent;
|
|
|
1050
1095
|
}),
|
|
1051
1096
|
{
|
|
1052
1097
|
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
|
-
|
|
1098
|
+
transform: {
|
|
1099
|
+
filter: { code: "loadBootstrapScriptContent" },
|
|
1100
|
+
async handler(code) {
|
|
1101
|
+
if (!code.includes("loadBootstrapScriptContent") || !/import\s*\.\s*meta\s*\.\s*viteRsc\s*\.\s*loadBootstrapScriptContent/.test(code)) return;
|
|
1102
|
+
assert(!rscPluginOptions.customClientEntry, `[vite-rsc] 'import.meta.viteRsc.loadBootstrapScriptContent' cannot be used with 'customClientEntry' option`);
|
|
1103
|
+
assert(this.environment.name !== "client");
|
|
1104
|
+
const output = new MagicString(code);
|
|
1105
|
+
for (const match of stripLiteral(code).matchAll(/import\s*\.\s*meta\s*\.\s*viteRsc\s*\.\s*loadBootstrapScriptContent\(([\s\S]*?)\)/dg)) {
|
|
1106
|
+
const [argStart, argEnd] = match.indices[1];
|
|
1107
|
+
const argCode = code.slice(argStart, argEnd).trim();
|
|
1108
|
+
assert(evalValue(argCode), `[vite-rsc] expected 'loadBootstrapScriptContent("index")' but got ${argCode}`);
|
|
1109
|
+
let replacement = `Promise.resolve(__vite_rsc_assets_manifest.bootstrapScriptContent)`;
|
|
1110
|
+
const [start, end] = match.indices[0];
|
|
1111
|
+
output.overwrite(start, end, replacement);
|
|
1112
|
+
}
|
|
1113
|
+
if (output.hasChanged()) {
|
|
1114
|
+
if (!code.includes("__vite_rsc_assets_manifest")) output.prepend(`import __vite_rsc_assets_manifest from "virtual:vite-rsc/assets-manifest";`);
|
|
1115
|
+
return {
|
|
1116
|
+
code: output.toString(),
|
|
1117
|
+
map: output.generateMap({ hires: "boundary" })
|
|
1118
|
+
};
|
|
1119
|
+
}
|
|
1072
1120
|
}
|
|
1073
1121
|
}
|
|
1074
1122
|
},
|
|
@@ -1124,9 +1172,12 @@ import.meta.hot.on("rsc:update", () => {
|
|
|
1124
1172
|
function globalAsyncLocalStoragePlugin() {
|
|
1125
1173
|
return [{
|
|
1126
1174
|
name: "rsc:inject-async-local-storage",
|
|
1127
|
-
transform: {
|
|
1128
|
-
|
|
1129
|
-
|
|
1175
|
+
transform: {
|
|
1176
|
+
filter: { code: "typeof AsyncLocalStorage" },
|
|
1177
|
+
handler(code) {
|
|
1178
|
+
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;
|
|
1179
|
+
}
|
|
1180
|
+
}
|
|
1130
1181
|
}];
|
|
1131
1182
|
}
|
|
1132
1183
|
function vitePluginUseClient(useClientPluginOptions, manager) {
|
|
@@ -1143,7 +1194,7 @@ function vitePluginUseClient(useClientPluginOptions, manager) {
|
|
|
1143
1194
|
return [
|
|
1144
1195
|
{
|
|
1145
1196
|
name: "rsc:use-client",
|
|
1146
|
-
async
|
|
1197
|
+
transform: { async handler(code, id) {
|
|
1147
1198
|
if (this.environment.name !== serverEnvironmentName) return;
|
|
1148
1199
|
if (!code.includes("use client")) {
|
|
1149
1200
|
delete manager.clientReferenceMetaMap[id];
|
|
@@ -1206,87 +1257,96 @@ function vitePluginUseClient(useClientPluginOptions, manager) {
|
|
|
1206
1257
|
code: output.toString(),
|
|
1207
1258
|
map: { mappings: "" }
|
|
1208
1259
|
};
|
|
1209
|
-
}
|
|
1260
|
+
} }
|
|
1210
1261
|
},
|
|
1211
1262
|
{
|
|
1212
1263
|
name: "rsc:use-client/build-references",
|
|
1213
|
-
resolveId
|
|
1214
|
-
|
|
1264
|
+
resolveId: {
|
|
1265
|
+
filter: { id: prefixRegex("virtual:vite-rsc/client-references") },
|
|
1266
|
+
handler(source) {
|
|
1267
|
+
if (source.startsWith("virtual:vite-rsc/client-references")) return "\0" + source;
|
|
1268
|
+
}
|
|
1215
1269
|
},
|
|
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,
|
|
1270
|
+
load: {
|
|
1271
|
+
filter: { id: prefixRegex("\0virtual:vite-rsc/client-references") },
|
|
1272
|
+
handler(id) {
|
|
1273
|
+
if (id === "\0virtual:vite-rsc/client-references") {
|
|
1274
|
+
if (this.environment.mode === "dev") return {
|
|
1275
|
+
code: `export default {}`,
|
|
1227
1276
|
map: null
|
|
1228
1277
|
};
|
|
1229
|
-
|
|
1230
|
-
|
|
1231
|
-
|
|
1232
|
-
|
|
1233
|
-
|
|
1234
|
-
|
|
1235
|
-
|
|
1236
|
-
|
|
1237
|
-
|
|
1238
|
-
|
|
1239
|
-
|
|
1240
|
-
|
|
1241
|
-
|
|
1242
|
-
|
|
1243
|
-
|
|
1244
|
-
|
|
1245
|
-
|
|
1246
|
-
|
|
1278
|
+
if (manager.isScanBuild) {
|
|
1279
|
+
let code = ``;
|
|
1280
|
+
for (const meta of Object.values(manager.clientReferenceMetaMap)) code += `import ${JSON.stringify(withResolvedIdProxy(meta.importId))};\n`;
|
|
1281
|
+
return {
|
|
1282
|
+
code,
|
|
1283
|
+
map: null
|
|
1284
|
+
};
|
|
1285
|
+
}
|
|
1286
|
+
let code = "";
|
|
1287
|
+
manager.clientReferenceGroups = {};
|
|
1288
|
+
for (const meta of Object.values(manager.clientReferenceMetaMap)) {
|
|
1289
|
+
if (!meta.serverChunk) continue;
|
|
1290
|
+
let name = useClientPluginOptions.clientChunks?.({
|
|
1291
|
+
id: meta.importId,
|
|
1292
|
+
normalizedId: manager.toRelativeId(meta.importId),
|
|
1293
|
+
serverChunk: meta.serverChunk
|
|
1294
|
+
}) ?? meta.serverChunk;
|
|
1295
|
+
name = cleanUrl(name.replaceAll("..", "__"));
|
|
1296
|
+
(manager.clientReferenceGroups[name] ??= []).push(meta);
|
|
1297
|
+
meta.groupChunkId = `\0virtual:vite-rsc/client-references/group/${name}`;
|
|
1298
|
+
}
|
|
1299
|
+
debug("client-reference-groups", manager.clientReferenceGroups);
|
|
1300
|
+
for (const [name, metas] of Object.entries(manager.clientReferenceGroups)) {
|
|
1301
|
+
const groupVirtual = `virtual:vite-rsc/client-references/group/${name}`;
|
|
1302
|
+
for (const meta of metas) code += `\
|
|
1247
1303
|
${JSON.stringify(meta.referenceKey)}: async () => {
|
|
1248
1304
|
const m = await import(${JSON.stringify(groupVirtual)});
|
|
1249
1305
|
return m.export_${meta.referenceKey};
|
|
1250
1306
|
},
|
|
1251
1307
|
`;
|
|
1308
|
+
}
|
|
1309
|
+
code = `export default {${code}};\n`;
|
|
1310
|
+
return {
|
|
1311
|
+
code,
|
|
1312
|
+
map: null
|
|
1313
|
+
};
|
|
1252
1314
|
}
|
|
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 += `
|
|
1315
|
+
if (id.startsWith("\0virtual:vite-rsc/client-references/group/")) {
|
|
1316
|
+
const name = id.slice(42);
|
|
1317
|
+
const metas = manager.clientReferenceGroups[name];
|
|
1318
|
+
assert(metas, `unknown client reference group: ${name}`);
|
|
1319
|
+
let code = ``;
|
|
1320
|
+
for (const meta of metas) {
|
|
1321
|
+
const exports = meta.renderedExports.map((name) => `${name}: import_${meta.referenceKey}.${name},\n`).sort().join("");
|
|
1322
|
+
code += `
|
|
1267
1323
|
import * as import_${meta.referenceKey} from ${JSON.stringify(withResolvedIdProxy(meta.importId))};
|
|
1268
1324
|
export const export_${meta.referenceKey} = {${exports}};
|
|
1269
1325
|
`;
|
|
1326
|
+
}
|
|
1327
|
+
return {
|
|
1328
|
+
code,
|
|
1329
|
+
map: null
|
|
1330
|
+
};
|
|
1270
1331
|
}
|
|
1271
|
-
return {
|
|
1272
|
-
code,
|
|
1273
|
-
map: null
|
|
1274
|
-
};
|
|
1275
1332
|
}
|
|
1276
1333
|
}
|
|
1277
1334
|
},
|
|
1278
1335
|
{
|
|
1279
1336
|
name: "rsc:virtual-client-in-server-package",
|
|
1280
|
-
|
|
1281
|
-
|
|
1282
|
-
|
|
1283
|
-
|
|
1284
|
-
|
|
1285
|
-
|
|
1337
|
+
load: {
|
|
1338
|
+
filter: { id: prefixRegex("\0virtual:vite-rsc/client-in-server-package-proxy/") },
|
|
1339
|
+
async handler(id) {
|
|
1340
|
+
if (id.startsWith("\0virtual:vite-rsc/client-in-server-package-proxy/")) {
|
|
1341
|
+
assert.equal(this.environment.mode, "dev");
|
|
1342
|
+
assert(this.environment.name !== serverEnvironmentName);
|
|
1343
|
+
id = decodeURIComponent(id.slice(49));
|
|
1344
|
+
return `
|
|
1286
1345
|
export * from ${JSON.stringify(id)};
|
|
1287
1346
|
import * as __all__ from ${JSON.stringify(id)};
|
|
1288
1347
|
export default __all__.default;
|
|
1289
1348
|
`;
|
|
1349
|
+
}
|
|
1290
1350
|
}
|
|
1291
1351
|
}
|
|
1292
1352
|
},
|
|
@@ -1304,11 +1364,14 @@ function vitePluginUseClient(useClientPluginOptions, manager) {
|
|
|
1304
1364
|
}
|
|
1305
1365
|
}
|
|
1306
1366
|
},
|
|
1307
|
-
|
|
1308
|
-
|
|
1309
|
-
|
|
1310
|
-
|
|
1311
|
-
|
|
1367
|
+
load: {
|
|
1368
|
+
filter: { id: prefixRegex("\0virtual:vite-rsc/client-package-proxy/") },
|
|
1369
|
+
async handler(id) {
|
|
1370
|
+
if (id.startsWith("\0virtual:vite-rsc/client-package-proxy/")) {
|
|
1371
|
+
assert(this.environment.mode === "dev");
|
|
1372
|
+
const source = id.slice(39);
|
|
1373
|
+
return `export {${Object.values(manager.clientReferenceMetaMap).find((v) => v.packageSource === source).exportNames.join(",")}} from ${JSON.stringify(source)};\n`;
|
|
1374
|
+
}
|
|
1312
1375
|
}
|
|
1313
1376
|
},
|
|
1314
1377
|
generateBundle(_options, bundle) {
|
|
@@ -1392,16 +1455,22 @@ function vitePluginDefineEncryptionKey(useServerPluginOptions) {
|
|
|
1392
1455
|
async configEnvironment(name, _config, env) {
|
|
1393
1456
|
if (name === serverEnvironmentName && !env.isPreview) defineEncryptionKey = useServerPluginOptions.defineEncryptionKey ?? JSON.stringify(toBase64(await generateEncryptionKey()));
|
|
1394
1457
|
},
|
|
1395
|
-
resolveId
|
|
1396
|
-
|
|
1397
|
-
|
|
1398
|
-
|
|
1399
|
-
|
|
1458
|
+
resolveId: {
|
|
1459
|
+
filter: { id: exactRegex("virtual:vite-rsc/encryption-key") },
|
|
1460
|
+
handler(source) {
|
|
1461
|
+
if (source === "virtual:vite-rsc/encryption-key") return {
|
|
1462
|
+
id: "\0" + source,
|
|
1463
|
+
moduleSideEffects: false
|
|
1464
|
+
};
|
|
1465
|
+
}
|
|
1400
1466
|
},
|
|
1401
|
-
load
|
|
1402
|
-
|
|
1403
|
-
|
|
1404
|
-
|
|
1467
|
+
load: {
|
|
1468
|
+
filter: { id: exactRegex("\0virtual:vite-rsc/encryption-key") },
|
|
1469
|
+
handler(id) {
|
|
1470
|
+
if (id === "\0virtual:vite-rsc/encryption-key") {
|
|
1471
|
+
if (this.environment.mode === "build") return `export default () => ${KEY_PLACEHOLDER}`;
|
|
1472
|
+
return `export default () => (${defineEncryptionKey})`;
|
|
1473
|
+
}
|
|
1405
1474
|
}
|
|
1406
1475
|
},
|
|
1407
1476
|
renderChunk(code, chunk) {
|
|
@@ -1425,7 +1494,7 @@ function vitePluginUseServer(useServerPluginOptions, manager) {
|
|
|
1425
1494
|
const debug = createDebug("vite-rsc:use-server");
|
|
1426
1495
|
return [{
|
|
1427
1496
|
name: "rsc:use-server",
|
|
1428
|
-
async
|
|
1497
|
+
transform: { async handler(code, id) {
|
|
1429
1498
|
if (!code.includes("use server")) {
|
|
1430
1499
|
delete manager.serverReferenceMetaMap[id];
|
|
1431
1500
|
return;
|
|
@@ -1498,7 +1567,7 @@ function vitePluginUseServer(useServerPluginOptions, manager) {
|
|
|
1498
1567
|
map: output.generateMap({ hires: "boundary" })
|
|
1499
1568
|
};
|
|
1500
1569
|
}
|
|
1501
|
-
}
|
|
1570
|
+
} }
|
|
1502
1571
|
}, createVirtualPlugin("vite-rsc/server-references", function() {
|
|
1503
1572
|
if (this.environment.mode === "dev") return {
|
|
1504
1573
|
code: `export {}`,
|
|
@@ -1651,7 +1720,7 @@ function vitePluginRscCss(rscCssOptions = {}, manager) {
|
|
|
1651
1720
|
return [
|
|
1652
1721
|
{
|
|
1653
1722
|
name: "rsc:rsc-css-export-transform",
|
|
1654
|
-
async
|
|
1723
|
+
transform: { async handler(code, id) {
|
|
1655
1724
|
if (this.environment.name !== "rsc") return;
|
|
1656
1725
|
const filter = getRscCssTransformFilter({
|
|
1657
1726
|
id,
|
|
@@ -1667,13 +1736,14 @@ function vitePluginRscCss(rscCssOptions = {}, manager) {
|
|
|
1667
1736
|
code: result.output.toString(),
|
|
1668
1737
|
map: result.output.generateMap({ hires: "boundary" })
|
|
1669
1738
|
};
|
|
1670
|
-
}
|
|
1739
|
+
} }
|
|
1671
1740
|
},
|
|
1672
1741
|
{
|
|
1673
1742
|
name: "rsc:rsc-css-self-accept",
|
|
1674
1743
|
apply: "serve",
|
|
1675
1744
|
transform: {
|
|
1676
1745
|
order: "post",
|
|
1746
|
+
filter: { id: /\.(css|less|sass|scss|styl|stylus|pcss|postcss|sss)(\?|$)/ },
|
|
1677
1747
|
handler(_code, id, _options) {
|
|
1678
1748
|
if (this.environment.name === "client" && this.environment.mode === "dev" && isCSSRequest(id) && directRequestRE.test(id)) {
|
|
1679
1749
|
const mod = this.environment.moduleGraph.getModuleById(id);
|
|
@@ -1684,19 +1754,25 @@ function vitePluginRscCss(rscCssOptions = {}, manager) {
|
|
|
1684
1754
|
},
|
|
1685
1755
|
{
|
|
1686
1756
|
name: "rsc:css-virtual",
|
|
1687
|
-
resolveId
|
|
1688
|
-
|
|
1757
|
+
resolveId: {
|
|
1758
|
+
filter: { id: prefixRegex("virtual:vite-rsc/css?") },
|
|
1759
|
+
handler(source) {
|
|
1760
|
+
if (source.startsWith("virtual:vite-rsc/css?")) return "\0" + source;
|
|
1761
|
+
}
|
|
1689
1762
|
},
|
|
1690
|
-
|
|
1691
|
-
|
|
1692
|
-
|
|
1693
|
-
|
|
1694
|
-
|
|
1695
|
-
|
|
1696
|
-
|
|
1697
|
-
|
|
1698
|
-
|
|
1699
|
-
|
|
1763
|
+
load: {
|
|
1764
|
+
filter: { id: prefixRegex("\0virtual:vite-rsc/css?") },
|
|
1765
|
+
async handler(id) {
|
|
1766
|
+
const parsed = parseCssVirtual(id);
|
|
1767
|
+
if (parsed?.type === "ssr") {
|
|
1768
|
+
id = parsed.id;
|
|
1769
|
+
const { server } = manager;
|
|
1770
|
+
const mod = await server.environments.ssr.moduleGraph.getModuleByUrl(id);
|
|
1771
|
+
if (!mod?.id || !mod?.file) return `export default []`;
|
|
1772
|
+
const result = collectCss(server.environments.ssr, mod.id);
|
|
1773
|
+
for (const file of [mod.file, ...result.visitedFiles]) this.addWatchFile(file);
|
|
1774
|
+
return `export default ${serializeValueWithRuntime(result.hrefs.map((href) => assetsURL(href.slice(1), manager)))}`;
|
|
1775
|
+
}
|
|
1700
1776
|
}
|
|
1701
1777
|
}
|
|
1702
1778
|
},
|
|
@@ -1715,73 +1791,79 @@ function vitePluginRscCss(rscCssOptions = {}, manager) {
|
|
|
1715
1791
|
return original.apply(this, args);
|
|
1716
1792
|
};
|
|
1717
1793
|
},
|
|
1718
|
-
|
|
1719
|
-
|
|
1720
|
-
|
|
1721
|
-
|
|
1722
|
-
|
|
1723
|
-
|
|
1724
|
-
|
|
1725
|
-
const
|
|
1726
|
-
|
|
1727
|
-
|
|
1728
|
-
|
|
1729
|
-
|
|
1730
|
-
|
|
1731
|
-
|
|
1732
|
-
|
|
1733
|
-
|
|
1734
|
-
|
|
1735
|
-
|
|
1794
|
+
transform: {
|
|
1795
|
+
filter: { code: "import.meta.viteRsc.loadCss" },
|
|
1796
|
+
async handler(code, id) {
|
|
1797
|
+
if (!code.includes("import.meta.viteRsc.loadCss")) return;
|
|
1798
|
+
assert(this.environment.name === "rsc");
|
|
1799
|
+
const output = new MagicString(code);
|
|
1800
|
+
let importAdded = false;
|
|
1801
|
+
for (const match of stripLiteral(code).matchAll(/import\.meta\.viteRsc\.loadCss\(([\s\S]*?)\)/dg)) {
|
|
1802
|
+
const [start, end] = match.indices[0];
|
|
1803
|
+
const [argStart, argEnd] = match.indices[1];
|
|
1804
|
+
const argCode = code.slice(argStart, argEnd).trim();
|
|
1805
|
+
let importer = id;
|
|
1806
|
+
if (argCode) {
|
|
1807
|
+
const argValue = evalValue(argCode);
|
|
1808
|
+
const resolved = await this.resolve(argValue, id);
|
|
1809
|
+
if (resolved) importer = resolved.id;
|
|
1810
|
+
else {
|
|
1811
|
+
this.warn(`[vite-rsc] failed to transform 'import.meta.viteRsc.loadCss(${argCode})'`);
|
|
1812
|
+
output.update(start, end, `null`);
|
|
1813
|
+
continue;
|
|
1814
|
+
}
|
|
1736
1815
|
}
|
|
1737
|
-
|
|
1738
|
-
|
|
1739
|
-
|
|
1740
|
-
|
|
1741
|
-
|
|
1742
|
-
|
|
1743
|
-
if (this.environment.mode === "dev") replacement = `__vite_rsc_react__.createElement(async () => {
|
|
1816
|
+
const importId = toCssVirtual({
|
|
1817
|
+
id: importer,
|
|
1818
|
+
type: "rsc"
|
|
1819
|
+
});
|
|
1820
|
+
let replacement;
|
|
1821
|
+
if (this.environment.mode === "dev") replacement = `__vite_rsc_react__.createElement(async () => {
|
|
1744
1822
|
const __m = await import(${JSON.stringify(importId)});
|
|
1745
1823
|
return __vite_rsc_react__.createElement(__m.Resources);
|
|
1746
1824
|
})`;
|
|
1747
|
-
|
|
1748
|
-
|
|
1749
|
-
|
|
1750
|
-
|
|
1751
|
-
|
|
1825
|
+
else {
|
|
1826
|
+
const hash = hashString(importId);
|
|
1827
|
+
if (!importAdded && !code.includes(`__vite_rsc_importer_resources_${hash}`)) {
|
|
1828
|
+
importAdded = true;
|
|
1829
|
+
output.prepend(`import * as __vite_rsc_importer_resources_${hash} from ${JSON.stringify(importId)};`);
|
|
1830
|
+
}
|
|
1831
|
+
replacement = `__vite_rsc_react__.createElement(__vite_rsc_importer_resources_${hash}.Resources)`;
|
|
1752
1832
|
}
|
|
1753
|
-
|
|
1833
|
+
output.update(start, end, replacement);
|
|
1834
|
+
}
|
|
1835
|
+
if (output.hasChanged()) {
|
|
1836
|
+
if (!code.includes("__vite_rsc_react__")) output.prepend(`import __vite_rsc_react__ from "react";`);
|
|
1837
|
+
return {
|
|
1838
|
+
code: output.toString(),
|
|
1839
|
+
map: output.generateMap({ hires: "boundary" })
|
|
1840
|
+
};
|
|
1754
1841
|
}
|
|
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
1842
|
}
|
|
1764
1843
|
},
|
|
1765
|
-
load
|
|
1766
|
-
|
|
1767
|
-
|
|
1768
|
-
|
|
1769
|
-
|
|
1770
|
-
|
|
1771
|
-
|
|
1772
|
-
const
|
|
1773
|
-
|
|
1774
|
-
|
|
1775
|
-
|
|
1776
|
-
|
|
1777
|
-
|
|
1778
|
-
|
|
1779
|
-
|
|
1780
|
-
|
|
1781
|
-
|
|
1844
|
+
load: {
|
|
1845
|
+
filter: { id: prefixRegex("\0virtual:vite-rsc/css?") },
|
|
1846
|
+
handler(id) {
|
|
1847
|
+
const { server } = manager;
|
|
1848
|
+
const parsed = parseCssVirtual(id);
|
|
1849
|
+
if (parsed?.type === "rsc") {
|
|
1850
|
+
assert(this.environment.name === "rsc");
|
|
1851
|
+
const importer = parsed.id;
|
|
1852
|
+
if (this.environment.mode === "dev") {
|
|
1853
|
+
const result = collectCss(server.environments.rsc, importer);
|
|
1854
|
+
for (const file of [importer, ...result.visitedFiles]) this.addWatchFile(file);
|
|
1855
|
+
return generateResourcesCode(serializeValueWithRuntime(assetsURLOfDeps({
|
|
1856
|
+
css: result.hrefs.map((href) => href.slice(1)),
|
|
1857
|
+
js: []
|
|
1858
|
+
}, manager)), manager, { cssLinkPrecedence: rscCssOptions.cssLinkPrecedence });
|
|
1859
|
+
} else {
|
|
1860
|
+
const key = manager.toRelativeId(importer);
|
|
1861
|
+
manager.serverResourcesMetaMap[importer] = { key };
|
|
1862
|
+
return `
|
|
1782
1863
|
import __vite_rsc_assets_manifest__ from "virtual:vite-rsc/assets-manifest";
|
|
1783
1864
|
${generateResourcesCode(`__vite_rsc_assets_manifest__.serverResources[${JSON.stringify(key)}]`, manager, { cssLinkPrecedence: rscCssOptions.cssLinkPrecedence })}
|
|
1784
1865
|
`;
|
|
1866
|
+
}
|
|
1785
1867
|
}
|
|
1786
1868
|
}
|
|
1787
1869
|
}
|
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-Xwi_Uxiv.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.19",
|
|
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 };
|