@vitejs/plugin-rsc 0.5.18 → 0.5.20
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/browser.d.ts +2 -2
- package/dist/{chunk-Dj_d7TT4.js → chunk-f2BShn47.js} +1 -1
- package/dist/{cjs-D2v1gYgq.js → cjs-v2jRTNln.js} +1 -61
- package/dist/core/browser.d.ts +5 -1
- package/dist/core/browser.js +1 -1
- package/dist/core/plugin.js +25 -1
- package/dist/core/rsc.d.ts +1 -1
- package/dist/core/rsc.js +84 -1
- package/dist/core/ssr.d.ts +1 -1
- package/dist/core/ssr.js +1 -1
- package/dist/import-environment-B994HXEc.d.ts +11 -0
- package/dist/index.d.ts +3 -2
- package/dist/index.js +4 -4
- package/dist/{picocolors-BRyoHAlU.js → picocolors-B0A1T24z.js} +1 -1
- package/dist/plugin.d.ts +179 -3
- package/dist/plugin.js +1461 -4
- package/dist/plugins/cjs.js +62 -1
- package/dist/react/browser.d.ts +2 -2
- package/dist/react/rsc.js +1 -1
- package/dist/rsc.d.ts +3 -2
- package/dist/rsc.js +2 -2
- package/dist/ssr.d.ts +3 -2
- package/dist/ssr.js +1 -1
- package/dist/transforms/index.d.ts +1 -1
- package/dist/transforms/index.js +1 -1
- package/dist/utils/encryption-runtime.js +1 -2
- package/dist/utils/rpc.js +89 -1
- package/dist/validate-import-DJumtHRw.js +498 -0
- package/package.json +7 -7
- package/dist/browser-s-WcB8A7.d.ts +0 -6
- package/dist/plugin-BGmSmdwL.js +0 -27
- package/dist/plugin-Cp12dr0Z.js +0 -1944
- package/dist/plugin-K7i9F4Fd.d.ts +0 -187
- package/dist/rpc-EIuXyQpO.js +0 -91
- package/dist/rsc-Bhp6O2qz.js +0 -86
- /package/dist/{encryption-utils-DdqSKS_O.js → encryption-utils-Bk5eKdu6.js} +0 -0
- /package/dist/{index-now_lP2V.d.ts → index-BIbdRBfk.d.ts} +0 -0
- /package/dist/{index-CLmWsR1c.d.ts → server-action-B2zS9t-J.d.ts} +0 -0
- /package/dist/{transforms-B2EJTNXG.js → server-action-JkEy-6yW.js} +0 -0
- /package/dist/{shared-rtJPs0Yj.js → shared-Dhw3vs8e.js} +0 -0
- /package/dist/{shared-CGK4coF3.js → shared-d80_k_tn.js} +0 -0
|
@@ -0,0 +1,498 @@
|
|
|
1
|
+
import assert from "node:assert";
|
|
2
|
+
import fs from "node:fs";
|
|
3
|
+
import path from "node:path";
|
|
4
|
+
import { fileURLToPath } from "node:url";
|
|
5
|
+
import { exactRegex, prefixRegex } from "@rolldown/pluginutils";
|
|
6
|
+
import * as esModuleLexer from "es-module-lexer";
|
|
7
|
+
import MagicString from "magic-string";
|
|
8
|
+
import { stripLiteral } from "strip-literal";
|
|
9
|
+
import { isFileLoadingAllowed, normalizePath, parseAstAsync } from "vite";
|
|
10
|
+
import { walk } from "estree-walker";
|
|
11
|
+
import { stripVTControlCharacters } from "node:util";
|
|
12
|
+
import { createHash } from "node:crypto";
|
|
13
|
+
|
|
14
|
+
//#region src/plugins/vite-utils.ts
|
|
15
|
+
const VALID_ID_PREFIX = `/@id/`;
|
|
16
|
+
const NULL_BYTE_PLACEHOLDER = `__x00__`;
|
|
17
|
+
const FS_PREFIX = `/@fs/`;
|
|
18
|
+
function wrapId(id) {
|
|
19
|
+
return id.startsWith(VALID_ID_PREFIX) ? id : VALID_ID_PREFIX + id.replace("\0", NULL_BYTE_PLACEHOLDER);
|
|
20
|
+
}
|
|
21
|
+
function withTrailingSlash(path) {
|
|
22
|
+
if (path[path.length - 1] !== "/") return `${path}/`;
|
|
23
|
+
return path;
|
|
24
|
+
}
|
|
25
|
+
const postfixRE = /[?#].*$/;
|
|
26
|
+
function cleanUrl(url) {
|
|
27
|
+
return url.replace(postfixRE, "");
|
|
28
|
+
}
|
|
29
|
+
function splitFileAndPostfix(path) {
|
|
30
|
+
const file = cleanUrl(path);
|
|
31
|
+
return {
|
|
32
|
+
file,
|
|
33
|
+
postfix: path.slice(file.length)
|
|
34
|
+
};
|
|
35
|
+
}
|
|
36
|
+
const windowsSlashRE = /\\/g;
|
|
37
|
+
function slash(p) {
|
|
38
|
+
return p.replace(windowsSlashRE, "/");
|
|
39
|
+
}
|
|
40
|
+
const isWindows = typeof process !== "undefined" && process.platform === "win32";
|
|
41
|
+
function injectQuery(url, queryToInject) {
|
|
42
|
+
const { file, postfix } = splitFileAndPostfix(url);
|
|
43
|
+
return `${isWindows ? slash(file) : file}?${queryToInject}${postfix[0] === "?" ? `&${postfix.slice(1)}` : postfix}`;
|
|
44
|
+
}
|
|
45
|
+
function normalizeResolvedIdToUrl(environment, url, resolved) {
|
|
46
|
+
const root = environment.config.root;
|
|
47
|
+
const depsOptimizer = environment.depsOptimizer;
|
|
48
|
+
if (resolved.id.startsWith(withTrailingSlash(root))) url = resolved.id.slice(root.length);
|
|
49
|
+
else if (depsOptimizer?.isOptimizedDepFile(resolved.id) || resolved.id !== "/@react-refresh" && path.isAbsolute(resolved.id) && fs.existsSync(cleanUrl(resolved.id))) url = path.posix.join(FS_PREFIX, resolved.id);
|
|
50
|
+
else url = resolved.id;
|
|
51
|
+
if (url[0] !== "." && url[0] !== "/") url = wrapId(resolved.id);
|
|
52
|
+
return url;
|
|
53
|
+
}
|
|
54
|
+
function normalizeViteImportAnalysisUrl(environment, id) {
|
|
55
|
+
let url = normalizeResolvedIdToUrl(environment, id, { id });
|
|
56
|
+
if (environment.config.consumer === "client") {
|
|
57
|
+
const mod = environment.moduleGraph.getModuleById(id);
|
|
58
|
+
if (mod && mod.lastHMRTimestamp > 0) url = injectQuery(url, `t=${mod.lastHMRTimestamp}`);
|
|
59
|
+
}
|
|
60
|
+
return url;
|
|
61
|
+
}
|
|
62
|
+
function prepareError(err) {
|
|
63
|
+
return {
|
|
64
|
+
message: stripVTControlCharacters(err.message),
|
|
65
|
+
stack: stripVTControlCharacters(cleanStack(err.stack || "")),
|
|
66
|
+
id: err.id,
|
|
67
|
+
frame: stripVTControlCharacters(err.frame || ""),
|
|
68
|
+
plugin: err.plugin,
|
|
69
|
+
pluginCode: err.pluginCode?.toString(),
|
|
70
|
+
loc: err.loc
|
|
71
|
+
};
|
|
72
|
+
}
|
|
73
|
+
function cleanStack(stack) {
|
|
74
|
+
return stack.split(/\n/).filter((l) => /^\s*at/.test(l)).join("\n");
|
|
75
|
+
}
|
|
76
|
+
function evalValue(rawValue) {
|
|
77
|
+
return new Function(`
|
|
78
|
+
var console, exports, global, module, process, require
|
|
79
|
+
return (\n${rawValue}\n)
|
|
80
|
+
`)();
|
|
81
|
+
}
|
|
82
|
+
const directRequestRE = /(\?|&)direct=?(?:&|$)/;
|
|
83
|
+
|
|
84
|
+
//#endregion
|
|
85
|
+
//#region src/plugins/find-source-map-url.ts
|
|
86
|
+
function vitePluginFindSourceMapURL() {
|
|
87
|
+
return [{
|
|
88
|
+
name: "rsc:findSourceMapURL",
|
|
89
|
+
apply: "serve",
|
|
90
|
+
configureServer(server) {
|
|
91
|
+
server.middlewares.use(async (req, res, next) => {
|
|
92
|
+
const url = new URL(req.url, `http://localhost`);
|
|
93
|
+
if (url.pathname === "/__vite_rsc_findSourceMapURL") {
|
|
94
|
+
let filename = url.searchParams.get("filename");
|
|
95
|
+
let environmentName = url.searchParams.get("environmentName");
|
|
96
|
+
try {
|
|
97
|
+
const map = await findSourceMapURL(server, filename, environmentName);
|
|
98
|
+
res.setHeader("content-type", "application/json");
|
|
99
|
+
if (!map) res.statusCode = 404;
|
|
100
|
+
res.end(JSON.stringify(map ?? {}));
|
|
101
|
+
} catch (e) {
|
|
102
|
+
next(e);
|
|
103
|
+
}
|
|
104
|
+
return;
|
|
105
|
+
}
|
|
106
|
+
next();
|
|
107
|
+
});
|
|
108
|
+
}
|
|
109
|
+
}];
|
|
110
|
+
}
|
|
111
|
+
async function findSourceMapURL(server, filename, environmentName) {
|
|
112
|
+
if (filename.startsWith("file://")) {
|
|
113
|
+
filename = slash(fileURLToPath(filename));
|
|
114
|
+
if (isFileLoadingAllowed(server.config, filename) && fs.existsSync(filename)) {
|
|
115
|
+
const content = fs.readFileSync(filename, "utf-8");
|
|
116
|
+
return {
|
|
117
|
+
version: 3,
|
|
118
|
+
sources: [filename],
|
|
119
|
+
sourcesContent: [content],
|
|
120
|
+
mappings: "AAAA" + ";AACA".repeat(content.split("\n").length)
|
|
121
|
+
};
|
|
122
|
+
}
|
|
123
|
+
return;
|
|
124
|
+
}
|
|
125
|
+
let mod;
|
|
126
|
+
let map;
|
|
127
|
+
if (environmentName === "Server") {
|
|
128
|
+
mod = server.environments.rsc.moduleGraph.getModuleById(filename);
|
|
129
|
+
map = mod?.transformResult?.map;
|
|
130
|
+
if (map && map.mappings) map = {
|
|
131
|
+
...map,
|
|
132
|
+
mappings: ";;" + map.mappings
|
|
133
|
+
};
|
|
134
|
+
}
|
|
135
|
+
const base = server.config.base.slice(0, -1);
|
|
136
|
+
if (environmentName === "Client") try {
|
|
137
|
+
const url = new URL(filename).pathname.slice(base.length);
|
|
138
|
+
mod = server.environments.client.moduleGraph.urlToModuleMap.get(url);
|
|
139
|
+
map = mod?.transformResult?.map;
|
|
140
|
+
} catch (e) {}
|
|
141
|
+
if (mod && map) return {
|
|
142
|
+
...map,
|
|
143
|
+
sources: [base + mod.url]
|
|
144
|
+
};
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
//#endregion
|
|
148
|
+
//#region src/plugins/utils.ts
|
|
149
|
+
function sortObject(o) {
|
|
150
|
+
return Object.fromEntries(Object.entries(o).sort(([a], [b]) => a.localeCompare(b)));
|
|
151
|
+
}
|
|
152
|
+
function withRollupError(ctx, f) {
|
|
153
|
+
function processError(e) {
|
|
154
|
+
if (e && typeof e === "object" && typeof e.pos === "number") return ctx.error(e, e.pos);
|
|
155
|
+
throw e;
|
|
156
|
+
}
|
|
157
|
+
return function(...args) {
|
|
158
|
+
try {
|
|
159
|
+
const result = f.apply(this, args);
|
|
160
|
+
if (result instanceof Promise) return result.catch((e) => processError(e));
|
|
161
|
+
return result;
|
|
162
|
+
} catch (e) {
|
|
163
|
+
processError(e);
|
|
164
|
+
}
|
|
165
|
+
};
|
|
166
|
+
}
|
|
167
|
+
function createVirtualPlugin(name, load) {
|
|
168
|
+
const virtualId = "virtual:" + name;
|
|
169
|
+
const resolvedId = "\0" + virtualId;
|
|
170
|
+
return {
|
|
171
|
+
name: `rsc:virtual-${name}`,
|
|
172
|
+
resolveId: {
|
|
173
|
+
filter: { id: exactRegex(virtualId) },
|
|
174
|
+
handler(source) {
|
|
175
|
+
if (source === virtualId) return resolvedId;
|
|
176
|
+
}
|
|
177
|
+
},
|
|
178
|
+
load: {
|
|
179
|
+
filter: { id: exactRegex(resolvedId) },
|
|
180
|
+
handler(id, options) {
|
|
181
|
+
if (id === resolvedId) return load.apply(this, [id, options]);
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
};
|
|
185
|
+
}
|
|
186
|
+
function normalizeRelativePath(s) {
|
|
187
|
+
s = normalizePath(s);
|
|
188
|
+
return s[0] === "." ? s : "./" + s;
|
|
189
|
+
}
|
|
190
|
+
function getEntrySource(config, name) {
|
|
191
|
+
const input = config.build.rollupOptions.input;
|
|
192
|
+
if (!name) return getFallbackRollupEntry(input).source;
|
|
193
|
+
if (typeof input === "object" && !Array.isArray(input) && name in input && typeof input[name] === "string") return input[name];
|
|
194
|
+
throw new Error(`[vite-rsc:getEntrySource] expected 'build.rollupOptions.input' to be an object with a '${name}' property that is a string, but got ${JSON.stringify(input)}`);
|
|
195
|
+
}
|
|
196
|
+
function getFallbackRollupEntry(input = {}) {
|
|
197
|
+
const inputEntries = Object.entries(normalizeRollupOpitonsInput(input));
|
|
198
|
+
if (inputEntries.length === 1) {
|
|
199
|
+
const [name, source] = inputEntries[0];
|
|
200
|
+
return {
|
|
201
|
+
name,
|
|
202
|
+
source
|
|
203
|
+
};
|
|
204
|
+
}
|
|
205
|
+
throw new Error(`[vite-rsc] cannot determine fallback entry name from multiple entries, please specify the entry name explicitly`);
|
|
206
|
+
}
|
|
207
|
+
function normalizeRollupOpitonsInput(input = {}) {
|
|
208
|
+
if (typeof input === "string") input = [input];
|
|
209
|
+
if (Array.isArray(input)) return Object.fromEntries(input.map((file) => [path.basename(file).slice(0, -path.extname(file).length), file]));
|
|
210
|
+
return input;
|
|
211
|
+
}
|
|
212
|
+
function hashString(v) {
|
|
213
|
+
return createHash("sha256").update(v).digest().toString("hex").slice(0, 12);
|
|
214
|
+
}
|
|
215
|
+
function getFetchHandlerExport(exports) {
|
|
216
|
+
if ("default" in exports) {
|
|
217
|
+
const default_ = exports.default;
|
|
218
|
+
if (default_ && typeof default_ === "object" && "fetch" in default_ && typeof default_.fetch === "function") return default_.fetch;
|
|
219
|
+
if (typeof default_ === "function") return default_;
|
|
220
|
+
}
|
|
221
|
+
throw new Error("Invalid server handler entry");
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
//#endregion
|
|
225
|
+
//#region src/plugins/import-environment.ts
|
|
226
|
+
const ENV_IMPORTS_MANIFEST_NAME = "__vite_rsc_env_imports_manifest.js";
|
|
227
|
+
const ENV_IMPORTS_MANIFEST_PLACEHOLDER = "virtual:vite-rsc/env-imports-manifest";
|
|
228
|
+
const ENV_IMPORTS_ENTRY_FALLBACK = "virtual:vite-rsc/env-imports-entry-fallback";
|
|
229
|
+
function ensureEnvironmentImportsEntryFallback({ environments }) {
|
|
230
|
+
for (const [name, config] of Object.entries(environments)) {
|
|
231
|
+
if (name === "client") continue;
|
|
232
|
+
const input = normalizeRollupOpitonsInput(config.build?.rollupOptions?.input);
|
|
233
|
+
if (Object.keys(input).length === 0) {
|
|
234
|
+
config.build = config.build || {};
|
|
235
|
+
config.build.rollupOptions = config.build.rollupOptions || {};
|
|
236
|
+
config.build.rollupOptions.input = { __vite_rsc_env_imports_entry_fallback: ENV_IMPORTS_ENTRY_FALLBACK };
|
|
237
|
+
}
|
|
238
|
+
}
|
|
239
|
+
}
|
|
240
|
+
function vitePluginImportEnvironment(manager) {
|
|
241
|
+
return [{
|
|
242
|
+
name: "rsc:import-environment",
|
|
243
|
+
resolveId: {
|
|
244
|
+
filter: { id: exactRegex(ENV_IMPORTS_MANIFEST_PLACEHOLDER) },
|
|
245
|
+
handler(source) {
|
|
246
|
+
if (source === ENV_IMPORTS_MANIFEST_PLACEHOLDER) return {
|
|
247
|
+
id: ENV_IMPORTS_MANIFEST_PLACEHOLDER,
|
|
248
|
+
external: true
|
|
249
|
+
};
|
|
250
|
+
}
|
|
251
|
+
},
|
|
252
|
+
buildStart() {
|
|
253
|
+
if (this.environment.mode !== "build") return;
|
|
254
|
+
const emitted = /* @__PURE__ */ new Set();
|
|
255
|
+
for (const byTargetEnv of Object.values(manager.environmentImportMetaMap)) {
|
|
256
|
+
const imports = byTargetEnv[this.environment.name];
|
|
257
|
+
if (!imports) continue;
|
|
258
|
+
for (const meta of Object.values(imports)) if (!emitted.has(meta.resolvedId)) {
|
|
259
|
+
emitted.add(meta.resolvedId);
|
|
260
|
+
this.emitFile({
|
|
261
|
+
type: "chunk",
|
|
262
|
+
id: meta.resolvedId
|
|
263
|
+
});
|
|
264
|
+
}
|
|
265
|
+
}
|
|
266
|
+
},
|
|
267
|
+
transform: {
|
|
268
|
+
filter: { code: "import.meta.viteRsc.import" },
|
|
269
|
+
async handler(code, id) {
|
|
270
|
+
if (!code.includes("import.meta.viteRsc.import")) return;
|
|
271
|
+
const { server } = manager;
|
|
272
|
+
const s = new MagicString(code);
|
|
273
|
+
for (const match of stripLiteral(code).matchAll(/import\.meta\.viteRsc\.import\s*(<[\s\S]*?>)?\s*\(([\s\S]*?)\)/dg)) {
|
|
274
|
+
const [argStart, argEnd] = match.indices[2];
|
|
275
|
+
const [specifier, options] = evalValue(`[${code.slice(argStart, argEnd).trim()}]`);
|
|
276
|
+
const environmentName = options.environment;
|
|
277
|
+
let resolvedId;
|
|
278
|
+
if (this.environment.mode === "dev") {
|
|
279
|
+
const targetEnv = server.environments[environmentName];
|
|
280
|
+
assert(targetEnv, `[vite-rsc] unknown environment '${environmentName}'`);
|
|
281
|
+
const resolved = await targetEnv.pluginContainer.resolveId(specifier, id);
|
|
282
|
+
assert(resolved, `[vite-rsc] failed to resolve '${specifier}' in environment '${environmentName}'`);
|
|
283
|
+
resolvedId = resolved.id;
|
|
284
|
+
} else {
|
|
285
|
+
const targetEnvConfig = manager.config.environments[environmentName];
|
|
286
|
+
assert(targetEnvConfig, `[vite-rsc] unknown environment '${environmentName}'`);
|
|
287
|
+
const resolved = await this.resolve(specifier, id);
|
|
288
|
+
assert(resolved, `[vite-rsc] failed to resolve '${specifier}' in environment '${environmentName}'`);
|
|
289
|
+
resolvedId = resolved.id;
|
|
290
|
+
}
|
|
291
|
+
const sourceEnv = this.environment.name;
|
|
292
|
+
const targetEnv = environmentName;
|
|
293
|
+
manager.environmentImportMetaMap[sourceEnv] ??= {};
|
|
294
|
+
manager.environmentImportMetaMap[sourceEnv][targetEnv] ??= {};
|
|
295
|
+
manager.environmentImportMetaMap[sourceEnv][targetEnv][resolvedId] = {
|
|
296
|
+
resolvedId,
|
|
297
|
+
targetEnv,
|
|
298
|
+
sourceEnv,
|
|
299
|
+
specifier
|
|
300
|
+
};
|
|
301
|
+
let replacement;
|
|
302
|
+
if (this.environment.mode === "dev") replacement = `globalThis.__VITE_ENVIRONMENT_RUNNER_IMPORT__(${JSON.stringify(environmentName)}, ${JSON.stringify(resolvedId)})`;
|
|
303
|
+
else {
|
|
304
|
+
const relativeId = manager.toRelativeId(resolvedId);
|
|
305
|
+
replacement = `(await import(${JSON.stringify(ENV_IMPORTS_MANIFEST_PLACEHOLDER)})).default[${JSON.stringify(relativeId)}]()`;
|
|
306
|
+
}
|
|
307
|
+
const [start, end] = match.indices[0];
|
|
308
|
+
s.overwrite(start, end, replacement);
|
|
309
|
+
}
|
|
310
|
+
if (s.hasChanged()) return {
|
|
311
|
+
code: s.toString(),
|
|
312
|
+
map: s.generateMap({ hires: "boundary" })
|
|
313
|
+
};
|
|
314
|
+
}
|
|
315
|
+
},
|
|
316
|
+
renderChunk(code, chunk) {
|
|
317
|
+
if (code.includes(ENV_IMPORTS_MANIFEST_PLACEHOLDER)) {
|
|
318
|
+
const replacement = normalizeRelativePath(path.relative(path.join(chunk.fileName, ".."), ENV_IMPORTS_MANIFEST_NAME));
|
|
319
|
+
code = code.replaceAll(ENV_IMPORTS_MANIFEST_PLACEHOLDER, () => replacement);
|
|
320
|
+
return { code };
|
|
321
|
+
}
|
|
322
|
+
}
|
|
323
|
+
}, createVirtualPlugin(ENV_IMPORTS_ENTRY_FALLBACK.slice(8), () => {
|
|
324
|
+
return `export default "__vite_rsc_env_imports_entry_fallback";`;
|
|
325
|
+
})];
|
|
326
|
+
}
|
|
327
|
+
function writeEnvironmentImportsManifest(manager) {
|
|
328
|
+
if (Object.keys(manager.environmentImportMetaMap).length === 0) return;
|
|
329
|
+
for (const [sourceEnv, byTargetEnv] of Object.entries(manager.environmentImportMetaMap)) {
|
|
330
|
+
const sourceOutDir = manager.config.environments[sourceEnv].build.outDir;
|
|
331
|
+
const manifestPath = path.join(sourceOutDir, ENV_IMPORTS_MANIFEST_NAME);
|
|
332
|
+
let code = "export default {\n";
|
|
333
|
+
for (const [_targetEnv, imports] of Object.entries(byTargetEnv)) for (const [resolvedId, meta] of Object.entries(imports)) {
|
|
334
|
+
const bundle = manager.bundles[meta.targetEnv];
|
|
335
|
+
if (!bundle) throw new Error(`[vite-rsc] missing bundle for environment import: ${meta.targetEnv}`);
|
|
336
|
+
const chunk = Object.values(bundle).find((c) => c.type === "chunk" && c.facadeModuleId === resolvedId);
|
|
337
|
+
if (!chunk) throw new Error(`[vite-rsc] missing output for environment import: ${resolvedId}`);
|
|
338
|
+
const targetOutDir = manager.config.environments[meta.targetEnv].build.outDir;
|
|
339
|
+
const relativePath = normalizeRelativePath(path.relative(sourceOutDir, path.join(targetOutDir, chunk.fileName)));
|
|
340
|
+
const relativeId = manager.toRelativeId(resolvedId);
|
|
341
|
+
code += ` ${JSON.stringify(relativeId)}: () => import(${JSON.stringify(relativePath)}),\n`;
|
|
342
|
+
}
|
|
343
|
+
code += "}\n";
|
|
344
|
+
fs.writeFileSync(manifestPath, code);
|
|
345
|
+
}
|
|
346
|
+
}
|
|
347
|
+
|
|
348
|
+
//#endregion
|
|
349
|
+
//#region src/plugins/resolved-id-proxy.ts
|
|
350
|
+
const RESOLVED_ID_PROXY_PREFIX = "virtual:vite-rsc/resolved-id/";
|
|
351
|
+
function toResolvedIdProxy(resolvedId) {
|
|
352
|
+
return RESOLVED_ID_PROXY_PREFIX + encodeURIComponent(resolvedId);
|
|
353
|
+
}
|
|
354
|
+
function withResolvedIdProxy(resolvedId) {
|
|
355
|
+
return resolvedId.startsWith("\0") ? toResolvedIdProxy(resolvedId) : resolvedId;
|
|
356
|
+
}
|
|
357
|
+
function fromResolvedIdProxy(source) {
|
|
358
|
+
if (!source.startsWith(RESOLVED_ID_PROXY_PREFIX)) return;
|
|
359
|
+
const clean = source.split("?")[0];
|
|
360
|
+
return decodeURIComponent(clean.slice(29));
|
|
361
|
+
}
|
|
362
|
+
/**
|
|
363
|
+
* Vite plugin that resolves proxy import specifiers to the original resolved IDs.
|
|
364
|
+
*/
|
|
365
|
+
function vitePluginResolvedIdProxy() {
|
|
366
|
+
return {
|
|
367
|
+
name: "rsc:resolved-id-proxy",
|
|
368
|
+
resolveId: {
|
|
369
|
+
filter: { id: prefixRegex(RESOLVED_ID_PROXY_PREFIX) },
|
|
370
|
+
handler(source) {
|
|
371
|
+
const originalId = fromResolvedIdProxy(source);
|
|
372
|
+
if (originalId !== void 0) return originalId;
|
|
373
|
+
}
|
|
374
|
+
}
|
|
375
|
+
};
|
|
376
|
+
}
|
|
377
|
+
|
|
378
|
+
//#endregion
|
|
379
|
+
//#region src/plugins/scan.ts
|
|
380
|
+
function scanBuildStripPlugin({ manager }) {
|
|
381
|
+
return {
|
|
382
|
+
name: "rsc:scan-strip",
|
|
383
|
+
apply: "build",
|
|
384
|
+
enforce: "post",
|
|
385
|
+
transform: {
|
|
386
|
+
filter: { id: { exclude: exactRegex("\0rolldown/runtime.js") } },
|
|
387
|
+
async handler(code, _id, _options) {
|
|
388
|
+
if (!manager.isScanBuild) return;
|
|
389
|
+
return {
|
|
390
|
+
code: await transformScanBuildStrip(code),
|
|
391
|
+
map: { mappings: "" }
|
|
392
|
+
};
|
|
393
|
+
}
|
|
394
|
+
}
|
|
395
|
+
};
|
|
396
|
+
}
|
|
397
|
+
const importGlobRE = /\bimport\.meta\.glob(?:<\w+>)?\s*\(/g;
|
|
398
|
+
async function transformScanBuildStrip(code) {
|
|
399
|
+
const [imports] = esModuleLexer.parse(code);
|
|
400
|
+
let output = imports.map((e) => e.n && `import ${JSON.stringify(e.n)};\n`).filter(Boolean).join("");
|
|
401
|
+
if (importGlobRE.test(code)) {
|
|
402
|
+
walk(await parseAstAsync(code), { enter(node) {
|
|
403
|
+
if (node.type === "CallExpression" && node.callee.type === "MemberExpression" && node.callee.object.type === "MetaProperty" && node.callee.object.meta.type === "Identifier" && node.callee.object.meta.name === "import" && node.callee.object.property.type === "Identifier" && node.callee.object.property.name === "meta" && node.callee.property.type === "Identifier" && node.callee.property.name === "glob") {
|
|
404
|
+
const importMetaGlob = code.slice(node.start, node.end);
|
|
405
|
+
output += `console.log(${importMetaGlob});\n`;
|
|
406
|
+
}
|
|
407
|
+
} });
|
|
408
|
+
output += "";
|
|
409
|
+
}
|
|
410
|
+
return output;
|
|
411
|
+
}
|
|
412
|
+
|
|
413
|
+
//#endregion
|
|
414
|
+
//#region src/plugins/validate-import.ts
|
|
415
|
+
function validateImportPlugin() {
|
|
416
|
+
return {
|
|
417
|
+
name: "rsc:validate-imports",
|
|
418
|
+
resolveId: {
|
|
419
|
+
order: "pre",
|
|
420
|
+
filter: { id: /^(client-only|server-only)$/ },
|
|
421
|
+
async handler(source, _importer, options) {
|
|
422
|
+
if ("scan" in options && options.scan) return;
|
|
423
|
+
if (source === "client-only" || source === "server-only") {
|
|
424
|
+
if (source === "client-only" && this.environment.name === "rsc" || source === "server-only" && this.environment.name !== "rsc") return {
|
|
425
|
+
id: `\0virtual:vite-rsc/validate-imports/invalid/${source}`,
|
|
426
|
+
moduleSideEffects: true
|
|
427
|
+
};
|
|
428
|
+
return {
|
|
429
|
+
id: `\0virtual:vite-rsc/validate-imports/valid/${source}`,
|
|
430
|
+
moduleSideEffects: false
|
|
431
|
+
};
|
|
432
|
+
}
|
|
433
|
+
}
|
|
434
|
+
},
|
|
435
|
+
load: {
|
|
436
|
+
filter: { id: prefixRegex("\0virtual:vite-rsc/validate-imports/") },
|
|
437
|
+
handler(id) {
|
|
438
|
+
if (id.startsWith("\0virtual:vite-rsc/validate-imports/invalid/")) return `throw new Error("invalid import of '${id.slice(id.lastIndexOf("/") + 1)}'")`;
|
|
439
|
+
if (id.startsWith("\0virtual:vite-rsc/validate-imports/")) return `export {}`;
|
|
440
|
+
}
|
|
441
|
+
},
|
|
442
|
+
transform: {
|
|
443
|
+
order: "post",
|
|
444
|
+
filter: { id: prefixRegex("\0virtual:vite-rsc/validate-imports/invalid/") },
|
|
445
|
+
async handler(_code, id) {
|
|
446
|
+
if (this.environment.mode === "dev") {
|
|
447
|
+
if (id.startsWith(`\0virtual:vite-rsc/validate-imports/invalid/`)) validateImportChain(getImportChainDev(this.environment, id), this.environment.name, this.environment.config.root);
|
|
448
|
+
}
|
|
449
|
+
}
|
|
450
|
+
},
|
|
451
|
+
buildEnd() {
|
|
452
|
+
if (this.environment.mode === "build") {
|
|
453
|
+
validateImportChain(getImportChainBuild(this, "\0virtual:vite-rsc/validate-imports/invalid/server-only"), this.environment.name, this.environment.config.root);
|
|
454
|
+
validateImportChain(getImportChainBuild(this, "\0virtual:vite-rsc/validate-imports/invalid/client-only"), this.environment.name, this.environment.config.root);
|
|
455
|
+
}
|
|
456
|
+
}
|
|
457
|
+
};
|
|
458
|
+
}
|
|
459
|
+
function getImportChainDev(environment, id) {
|
|
460
|
+
const chain = [];
|
|
461
|
+
const recurse = (id) => {
|
|
462
|
+
if (chain.includes(id)) return;
|
|
463
|
+
const info = environment.moduleGraph.getModuleById(id);
|
|
464
|
+
if (!info) return;
|
|
465
|
+
chain.push(id);
|
|
466
|
+
const next = [...info.importers][0];
|
|
467
|
+
if (next && next.id) recurse(next.id);
|
|
468
|
+
};
|
|
469
|
+
recurse(id);
|
|
470
|
+
return chain;
|
|
471
|
+
}
|
|
472
|
+
function getImportChainBuild(ctx, id) {
|
|
473
|
+
const chain = [];
|
|
474
|
+
const recurse = (id) => {
|
|
475
|
+
if (chain.includes(id)) return;
|
|
476
|
+
const info = ctx.getModuleInfo(id);
|
|
477
|
+
if (!info) return;
|
|
478
|
+
chain.push(id);
|
|
479
|
+
const next = info.importers[0];
|
|
480
|
+
if (next) recurse(next);
|
|
481
|
+
};
|
|
482
|
+
recurse(id);
|
|
483
|
+
return chain;
|
|
484
|
+
}
|
|
485
|
+
function validateImportChain(chain, environmentName, root) {
|
|
486
|
+
if (chain.length === 0) return;
|
|
487
|
+
const id = chain[0];
|
|
488
|
+
const source = id.slice(id.lastIndexOf("/") + 1);
|
|
489
|
+
let result = `'${source}' cannot be imported in ${source === "server-only" ? "client" : "server"} build ('${environmentName}' environment):\n`;
|
|
490
|
+
result += chain.slice(1, 6).map((id, i) => " ".repeat(i + 1) + `imported by ${path.relative(root, id).replaceAll("\0", "")}\n`).join("");
|
|
491
|
+
if (chain.length > 6) result += " ".repeat(7) + "...\n";
|
|
492
|
+
const error = new Error(result);
|
|
493
|
+
if (chain[1]) Object.assign(error, { id: chain[1] });
|
|
494
|
+
throw error;
|
|
495
|
+
}
|
|
496
|
+
|
|
497
|
+
//#endregion
|
|
498
|
+
export { cleanUrl as _, ensureEnvironmentImportsEntryFallback as a, normalizeViteImportAnalysisUrl as b, createVirtualPlugin as c, getFetchHandlerExport as d, hashString as f, vitePluginFindSourceMapURL as g, withRollupError as h, withResolvedIdProxy as i, getEntrySource as l, sortObject as m, scanBuildStripPlugin as n, vitePluginImportEnvironment as o, normalizeRelativePath as p, vitePluginResolvedIdProxy as r, writeEnvironmentImportsManifest as s, validateImportPlugin as t, getFallbackRollupEntry as u, directRequestRE as v, prepareError as x, evalValue as y };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@vitejs/plugin-rsc",
|
|
3
|
-
"version": "0.5.
|
|
3
|
+
"version": "0.5.20",
|
|
4
4
|
"description": "React Server Components (RSC) support for Vite.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"react",
|
|
@@ -39,23 +39,23 @@
|
|
|
39
39
|
"prepack": "tsdown"
|
|
40
40
|
},
|
|
41
41
|
"dependencies": {
|
|
42
|
-
"@rolldown/pluginutils": "1.0.0-rc.
|
|
42
|
+
"@rolldown/pluginutils": "1.0.0-rc.4",
|
|
43
43
|
"es-module-lexer": "^2.0.0",
|
|
44
44
|
"estree-walker": "^3.0.3",
|
|
45
45
|
"magic-string": "^0.30.21",
|
|
46
46
|
"periscopic": "^4.0.2",
|
|
47
|
-
"srvx": "^0.
|
|
47
|
+
"srvx": "^0.11.4",
|
|
48
48
|
"strip-literal": "^3.1.0",
|
|
49
49
|
"turbo-stream": "^3.1.0",
|
|
50
50
|
"vitefu": "^1.1.1"
|
|
51
51
|
},
|
|
52
52
|
"devDependencies": {
|
|
53
53
|
"@hiogawa/utils": "^1.7.0",
|
|
54
|
-
"@playwright/test": "^1.58.
|
|
54
|
+
"@playwright/test": "^1.58.2",
|
|
55
55
|
"@tsconfig/strictest": "^2.0.8",
|
|
56
56
|
"@types/estree": "^1.0.8",
|
|
57
|
-
"@types/node": "^24.10.
|
|
58
|
-
"@types/react": "^19.2.
|
|
57
|
+
"@types/node": "^24.10.13",
|
|
58
|
+
"@types/react": "^19.2.14",
|
|
59
59
|
"@types/react-dom": "^19.2.3",
|
|
60
60
|
"@vitejs/plugin-react": "workspace:*",
|
|
61
61
|
"@vitejs/test-dep-cjs-and-esm": "./test-dep/cjs-and-esm",
|
|
@@ -65,7 +65,7 @@
|
|
|
65
65
|
"react-dom": "^19.2.4",
|
|
66
66
|
"react-server-dom-webpack": "^19.2.4",
|
|
67
67
|
"tinyexec": "^1.0.2",
|
|
68
|
-
"tsdown": "^0.20.
|
|
68
|
+
"tsdown": "^0.20.3"
|
|
69
69
|
},
|
|
70
70
|
"peerDependencies": {
|
|
71
71
|
"react": "*",
|
package/dist/plugin-BGmSmdwL.js
DELETED
|
@@ -1,27 +0,0 @@
|
|
|
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 };
|