@vitejs/plugin-rsc 0.4.17 → 0.4.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/index.js CHANGED
@@ -1,6 +1,6 @@
1
1
  import "./dist-DEF94lDJ.js";
2
2
  import "./plugin-CZbI4rhS.js";
3
- import { transformHoistInlineDirective, vitePluginRsc } from "./plugin-BxEQycAE.js";
3
+ import { transformHoistInlineDirective, vitePluginRsc } from "./plugin-TNrMxDOB.js";
4
4
  import "./encryption-utils-BDwwcMVT.js";
5
5
  import "./rpc-tGuLT8PD.js";
6
6
  import "./vite-utils-Vzd7cqfv.js";
@@ -301,6 +301,116 @@ function transformServerActionServer(input, ast, options) {
301
301
  });
302
302
  }
303
303
 
304
+ //#endregion
305
+ //#region src/plugins/utils.ts
306
+ function evalValue(rawValue) {
307
+ const fn = new Function(`
308
+ var console, exports, global, module, process, require
309
+ return (\n${rawValue}\n)
310
+ `);
311
+ return fn();
312
+ }
313
+ function parseIdQuery(id) {
314
+ if (!id.includes("?")) return {
315
+ filename: id,
316
+ query: {}
317
+ };
318
+ const [filename, rawQuery] = id.split(`?`, 2);
319
+ const query = Object.fromEntries(new URLSearchParams(rawQuery));
320
+ return {
321
+ filename,
322
+ query
323
+ };
324
+ }
325
+
326
+ //#endregion
327
+ //#region src/transforms/cjs.ts
328
+ function transformCjsToEsm(code, ast) {
329
+ const output = new MagicString(code);
330
+ const analyzed = analyze(ast);
331
+ let parentNodes = [];
332
+ let hoistIndex = 0;
333
+ walk(ast, {
334
+ enter(node) {
335
+ parentNodes.push(node);
336
+ if (node.type === "CallExpression" && node.callee.type === "Identifier" && node.callee.name === "require" && node.arguments.length === 1) {
337
+ let isTopLevel = true;
338
+ for (const parent of parentNodes) {
339
+ if (parent.type === "FunctionExpression" || parent.type === "FunctionDeclaration" || parent.type === "ArrowFunctionExpression") isTopLevel = false;
340
+ const scope = analyzed.map.get(parent);
341
+ if (scope && scope.declarations.has("require")) return;
342
+ }
343
+ if (isTopLevel) {
344
+ output.update(node.start, node.callee.end, "(await import");
345
+ output.appendRight(node.end, ")");
346
+ } else {
347
+ const hoisted = `__cjs_to_esm_hoist_${hoistIndex}`;
348
+ const importee = code.slice(node.arguments[0].start, node.arguments[0].end);
349
+ output.prepend(`const ${hoisted} = await import(${importee});\n`);
350
+ output.update(node.start, node.end, hoisted);
351
+ hoistIndex++;
352
+ }
353
+ }
354
+ },
355
+ leave() {
356
+ parentNodes.pop();
357
+ }
358
+ });
359
+ output.prepend(`const exports = {}; const module = { exports };\n`);
360
+ return { output };
361
+ }
362
+
363
+ //#endregion
364
+ //#region src/plugins/cjs.ts
365
+ function cjsModuleRunnerPlugin() {
366
+ const warnedPackages = new Set(["use-sync-external-store"]);
367
+ return [{
368
+ name: "cjs-module-runner-transform",
369
+ apply: "serve",
370
+ applyToEnvironment: (env) => env.config.dev.moduleRunnerTransform,
371
+ async transform(code, id) {
372
+ if (id.includes("/node_modules/") && !id.startsWith(this.environment.config.cacheDir) && /\b(require|exports)\b/.test(code)) {
373
+ id = parseIdQuery(id).filename;
374
+ if (!/\.[cm]?js$/.test(id)) return;
375
+ if (id.endsWith(".mjs")) return;
376
+ if (id.endsWith(".js")) {
377
+ const pkgJsonPath = await findClosestPkgJsonPath(path.dirname(id));
378
+ if (pkgJsonPath) {
379
+ const pkgJson = JSON.parse(fs.readFileSync(pkgJsonPath, "utf-8"));
380
+ if (pkgJson.type === "module") return;
381
+ }
382
+ }
383
+ const [, , , hasModuleSyntax] = esModuleLexer.parse(code);
384
+ if (hasModuleSyntax) return;
385
+ const packageKey = extractPackageKey(id);
386
+ if (!warnedPackages.has(packageKey)) {
387
+ this.warn(`Found non-optimized CJS dependency in '${this.environment.name}' environment. It is recommended to add the dependency to 'environments.${this.environment.name}.optimizeDeps.include'.`);
388
+ warnedPackages.add(packageKey);
389
+ }
390
+ const ast = await parseAstAsync(code);
391
+ const result = transformCjsToEsm(code, ast);
392
+ const output = result.output;
393
+ output.append(`__vite_ssr_exportAll__(module.exports)`);
394
+ return {
395
+ code: output.toString(),
396
+ map: output.generateMap({ hires: "boundary" })
397
+ };
398
+ }
399
+ }
400
+ }];
401
+ }
402
+ function extractPackageKey(id) {
403
+ const yarnMatch = id.match(/\/.yarn\/cache\/([^/]+)/);
404
+ if (yarnMatch) return yarnMatch[1];
405
+ if (id.includes("/node_modules")) {
406
+ id = id.split("/node_modules/").at(-1);
407
+ let [x, y] = id.split("/");
408
+ if (x.startsWith("@")) return `${x}/${y}`;
409
+ return x;
410
+ }
411
+ return id;
412
+ }
413
+
304
414
  //#endregion
305
415
  //#region src/plugin.ts
306
416
  let serverReferences = {};
@@ -820,31 +930,10 @@ globalThis.AsyncLocalStorage = __viteRscAyncHooks.AsyncLocalStorage;
820
930
  ...vitePluginFindSourceMapURL(),
821
931
  ...vitePluginRscCss({ rscCssTransform: rscPluginOptions.rscCssTransform }),
822
932
  ...rscPluginOptions.validateImports !== false ? [validateImportPlugin()] : [],
823
- ...vendorUseSyncExternalStorePlugin(),
824
933
  scanBuildStripPlugin(),
825
- detectNonOptimizedCjsPlugin()
934
+ ...cjsModuleRunnerPlugin()
826
935
  ];
827
936
  }
828
- function detectNonOptimizedCjsPlugin() {
829
- return {
830
- name: "rsc:detect-non-optimized-cjs",
831
- apply: "serve",
832
- async transform(code, id) {
833
- if (id.includes("/node_modules/") && !id.startsWith(this.environment.config.cacheDir) && /\b(require|exports)\b/.test(code)) {
834
- id = parseIdQuery(id).filename;
835
- let isEsm = id.endsWith(".mjs");
836
- if (id.endsWith(".js")) {
837
- const pkgJsonPath = await findClosestPkgJsonPath(path.dirname(id));
838
- if (pkgJsonPath) {
839
- const pkgJson = JSON.parse(fs.readFileSync(pkgJsonPath, "utf-8"));
840
- isEsm = pkgJson.type === "module";
841
- }
842
- }
843
- if (!isEsm) this.warn(`[vite-rsc] found non-optimized CJS dependency in '${this.environment.name}' environment. It is recommended to manually add the dependency to 'environments.${this.environment.name}.optimizeDeps.include'.`);
844
- }
845
- }
846
- };
847
- }
848
937
  function scanBuildStripPlugin() {
849
938
  return {
850
939
  name: "rsc:scan-strip",
@@ -1499,25 +1588,6 @@ function generateResourcesCode(depsCode) {
1499
1588
  export const Resources = (${ResourcesFn.toString()})(__vite_rsc_react__, ${depsCode});
1500
1589
  `;
1501
1590
  }
1502
- function evalValue(rawValue) {
1503
- const fn = new Function(`
1504
- var console, exports, global, module, process, require
1505
- return (\n${rawValue}\n)
1506
- `);
1507
- return fn();
1508
- }
1509
- function parseIdQuery(id) {
1510
- if (!id.includes("?")) return {
1511
- filename: id,
1512
- query: {}
1513
- };
1514
- const [filename, rawQuery] = id.split(`?`, 2);
1515
- const query = Object.fromEntries(new URLSearchParams(rawQuery));
1516
- return {
1517
- filename,
1518
- query
1519
- };
1520
- }
1521
1591
  async function transformRscCssExport(options) {
1522
1592
  if (hasDirective(options.ast.body, "use client")) return;
1523
1593
  const result = transformWrapExport(options.code, options.ast, {
@@ -1596,24 +1666,6 @@ function validateImportPlugin() {
1596
1666
  }
1597
1667
  };
1598
1668
  }
1599
- function vendorUseSyncExternalStorePlugin() {
1600
- const exports = [
1601
- "use-sync-external-store",
1602
- "use-sync-external-store/with-selector",
1603
- "use-sync-external-store/with-selector.js",
1604
- "use-sync-external-store/shim",
1605
- "use-sync-external-store/shim/index.js",
1606
- "use-sync-external-store/shim/with-selector",
1607
- "use-sync-external-store/shim/with-selector.js"
1608
- ];
1609
- return [{
1610
- name: "rsc:vendor-use-sync-external-store",
1611
- apply: "serve",
1612
- config() {
1613
- return { environments: { ssr: { optimizeDeps: { include: exports.map((e) => `${PKG_NAME} > ${e}`) } } } };
1614
- }
1615
- }];
1616
- }
1617
1669
  function sortObject(o) {
1618
1670
  return Object.fromEntries(Object.entries(o).sort(([a], [b]) => a.localeCompare(b)));
1619
1671
  }
package/dist/plugin.js CHANGED
@@ -1,6 +1,6 @@
1
1
  import "./dist-DEF94lDJ.js";
2
2
  import "./plugin-CZbI4rhS.js";
3
- import { __fix_cloudflare, findSourceMapURL, transformRscCssExport, vitePluginFindSourceMapURL, vitePluginRsc, vitePluginRscCss, vitePluginRscMinimal } from "./plugin-BxEQycAE.js";
3
+ import { __fix_cloudflare, findSourceMapURL, transformRscCssExport, vitePluginFindSourceMapURL, vitePluginRsc, vitePluginRscCss, vitePluginRscMinimal } from "./plugin-TNrMxDOB.js";
4
4
  import "./encryption-utils-BDwwcMVT.js";
5
5
  import "./rpc-tGuLT8PD.js";
6
6
  import "./vite-utils-Vzd7cqfv.js";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@vitejs/plugin-rsc",
3
- "version": "0.4.17",
3
+ "version": "0.4.18",
4
4
  "description": "React Server Components (RSC) support for Vite.",
5
5
  "keywords": [
6
6
  "vite",
@@ -44,7 +44,6 @@
44
44
  "magic-string": "^0.30.17",
45
45
  "periscopic": "^4.0.2",
46
46
  "turbo-stream": "^3.1.0",
47
- "use-sync-external-store": "^1.5.0",
48
47
  "vitefu": "^1.1.1"
49
48
  },
50
49
  "devDependencies": {
@@ -52,7 +51,7 @@
52
51
  "@playwright/test": "^1.54.2",
53
52
  "@tsconfig/strictest": "^2.0.5",
54
53
  "@types/estree": "^1.0.8",
55
- "@types/node": "^22.17.0",
54
+ "@types/node": "^22.17.1",
56
55
  "@types/react": "^19.1.9",
57
56
  "@types/react-dom": "^19.1.7",
58
57
  "@vitejs/plugin-react": "workspace:*",
@@ -61,7 +60,7 @@
61
60
  "react-server-dom-webpack": "^19.1.1",
62
61
  "rsc-html-stream": "^0.0.7",
63
62
  "tinyexec": "^1.0.1",
64
- "tsdown": "^0.13.2",
63
+ "tsdown": "^0.14.0",
65
64
  "vite-plugin-inspect": "^11.3.2"
66
65
  },
67
66
  "peerDependencies": {