@ivogt/rsc-router 0.0.0-experimental.10 → 0.0.0-experimental.12

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.
@@ -675,7 +675,7 @@ import { resolve } from "node:path";
675
675
  // package.json
676
676
  var package_default = {
677
677
  name: "@ivogt/rsc-router",
678
- version: "0.0.0-experimental.10",
678
+ version: "0.0.0-experimental.12",
679
679
  type: "module",
680
680
  description: "Type-safe RSC router with partial rendering support",
681
681
  author: "Ivo Todorov",
@@ -1070,14 +1070,10 @@ async function rscRouter(options) {
1070
1070
  }
1071
1071
  }
1072
1072
  },
1073
- // Pre-bundle rsc-html-stream and react-server-dom vendor to prevent discovery during first request
1074
- // The vendor file is CJS and needs to be transformed to ESM by Vite's optimizer
1073
+ // Pre-bundle rsc-html-stream to prevent discovery during first request
1075
1074
  // Exclude rsc-router modules to ensure same Context instance
1076
1075
  optimizeDeps: {
1077
- include: [
1078
- "rsc-html-stream/client",
1079
- "@vitejs/plugin-rsc/vendor/react-server-dom/client.browser"
1080
- ],
1076
+ include: ["rsc-html-stream/client"],
1081
1077
  exclude: excludeDeps,
1082
1078
  esbuildOptions: sharedEsbuildOptions
1083
1079
  }
@@ -1167,9 +1163,7 @@ async function rscRouter(options) {
1167
1163
  }
1168
1164
  },
1169
1165
  // Always exclude rsc-router modules, conditionally add virtual entry
1170
- // Include react-server-dom vendor to transform CJS to ESM
1171
1166
  optimizeDeps: {
1172
- include: ["@vitejs/plugin-rsc/vendor/react-server-dom/client.browser"],
1173
1167
  exclude: excludeDeps,
1174
1168
  esbuildOptions: sharedEsbuildOptions,
1175
1169
  ...useVirtualClient && {
@@ -1232,8 +1226,62 @@ async function rscRouter(options) {
1232
1226
  if (rscEntryPath) {
1233
1227
  plugins.push(createVersionInjectorPlugin(rscEntryPath));
1234
1228
  }
1229
+ plugins.push(createCjsToEsmPlugin());
1235
1230
  return plugins;
1236
1231
  }
1232
+ function createCjsToEsmPlugin() {
1233
+ return {
1234
+ name: "rsc-router:cjs-to-esm",
1235
+ enforce: "pre",
1236
+ transform(code, id) {
1237
+ const cleanId = id.split("?")[0];
1238
+ if (cleanId.includes("vendor/react-server-dom/client.browser.js") || cleanId.includes("vendor\\react-server-dom\\client.browser.js")) {
1239
+ const isProd = process.env.NODE_ENV === "production";
1240
+ const cjsFile = isProd ? "./cjs/react-server-dom-webpack-client.browser.production.js" : "./cjs/react-server-dom-webpack-client.browser.development.js";
1241
+ return {
1242
+ code: `export * from "${cjsFile}";`,
1243
+ map: null
1244
+ };
1245
+ }
1246
+ if ((cleanId.includes("vendor/react-server-dom/cjs/") || cleanId.includes("vendor\\react-server-dom\\cjs\\")) && cleanId.includes("client.browser")) {
1247
+ let transformed = code;
1248
+ const licenseMatch = transformed.match(/^\/\*\*[\s\S]*?\*\//);
1249
+ const license = licenseMatch ? licenseMatch[0] : "";
1250
+ if (license) {
1251
+ transformed = transformed.slice(license.length);
1252
+ }
1253
+ transformed = transformed.replace(/^\s*["']use strict["'];\s*/, "");
1254
+ transformed = transformed.replace(
1255
+ /^\s*["']production["']\s*!==\s*process\.env\.NODE_ENV\s*&&\s*\(function\s*\(\)\s*\{/,
1256
+ ""
1257
+ );
1258
+ transformed = transformed.replace(/\}\)\(\);?\s*$/, "");
1259
+ transformed = transformed.replace(
1260
+ /var\s+React\s*=\s*require\s*\(\s*["']react["']\s*\)\s*,[\s\n]+ReactDOM\s*=\s*require\s*\(\s*["']react-dom["']\s*\)\s*,/g,
1261
+ 'import React from "react";\nimport ReactDOM from "react-dom";\nvar '
1262
+ );
1263
+ transformed = transformed.replace(
1264
+ /var\s+ReactDOM\s*=\s*require\s*\(\s*["']react-dom["']\s*\)\s*,/g,
1265
+ 'import ReactDOM from "react-dom";\nvar '
1266
+ );
1267
+ transformed = transformed.replace(
1268
+ /exports\.(\w+)\s*=\s*function\s*\(/g,
1269
+ "export function $1("
1270
+ );
1271
+ transformed = transformed.replace(
1272
+ /exports\.(\w+)\s*=/g,
1273
+ "export const $1 ="
1274
+ );
1275
+ transformed = license + "\n" + transformed;
1276
+ return {
1277
+ code: transformed,
1278
+ map: null
1279
+ };
1280
+ }
1281
+ return null;
1282
+ }
1283
+ };
1284
+ }
1237
1285
  export {
1238
1286
  exposeActionId,
1239
1287
  exposeHandleId,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ivogt/rsc-router",
3
- "version": "0.0.0-experimental.10",
3
+ "version": "0.0.0-experimental.12",
4
4
  "type": "module",
5
5
  "description": "Type-safe RSC router with partial rendering support",
6
6
  "author": "Ivo Todorov",
package/src/vite/index.ts CHANGED
@@ -477,14 +477,10 @@ export async function rscRouter(
477
477
  },
478
478
  },
479
479
  },
480
- // Pre-bundle rsc-html-stream and react-server-dom vendor to prevent discovery during first request
481
- // The vendor file is CJS and needs to be transformed to ESM by Vite's optimizer
480
+ // Pre-bundle rsc-html-stream to prevent discovery during first request
482
481
  // Exclude rsc-router modules to ensure same Context instance
483
482
  optimizeDeps: {
484
- include: [
485
- "rsc-html-stream/client",
486
- "@vitejs/plugin-rsc/vendor/react-server-dom/client.browser",
487
- ],
483
+ include: ["rsc-html-stream/client"],
488
484
  exclude: excludeDeps,
489
485
  esbuildOptions: sharedEsbuildOptions,
490
486
  },
@@ -598,9 +594,7 @@ export async function rscRouter(
598
594
  },
599
595
  },
600
596
  // Always exclude rsc-router modules, conditionally add virtual entry
601
- // Include react-server-dom vendor to transform CJS to ESM
602
597
  optimizeDeps: {
603
- include: ["@vitejs/plugin-rsc/vendor/react-server-dom/client.browser"],
604
598
  exclude: excludeDeps,
605
599
  esbuildOptions: sharedEsbuildOptions,
606
600
  ...(useVirtualClient && {
@@ -684,9 +678,9 @@ export async function rscRouter(
684
678
  plugins.push(createVersionInjectorPlugin(rscEntryPath));
685
679
  }
686
680
 
687
- // CJS to ESM transformation is now handled by Vite's optimizeDeps.include
688
- // See client environment config where we include "@vitejs/plugin-rsc/vendor/react-server-dom/client.browser"
689
- // plugins.push(createCjsToEsmPlugin());
681
+ // Transform CJS vendor files to ESM for browser compatibility
682
+ // optimizeDeps.include doesn't work because the file is loaded after initial optimization
683
+ plugins.push(createCjsToEsmPlugin());
690
684
 
691
685
  return plugins;
692
686
  }
@@ -733,21 +727,30 @@ function createCjsToEsmPlugin(): Plugin {
733
727
  transformed = transformed.slice(license.length);
734
728
  }
735
729
 
736
- // Remove "use strict" and the conditional IIFE wrapper
730
+ // Remove "use strict" (both dev and prod have this)
731
+ transformed = transformed.replace(/^\s*["']use strict["'];\s*/, "");
732
+
733
+ // Remove the conditional IIFE wrapper (development only)
737
734
  transformed = transformed.replace(
738
- /^\s*["']use strict["'];\s*["']production["']\s*!==\s*process\.env\.NODE_ENV\s*&&\s*\(function\s*\(\)\s*\{/,
735
+ /^\s*["']production["']\s*!==\s*process\.env\.NODE_ENV\s*&&\s*\(function\s*\(\)\s*\{/,
739
736
  ""
740
737
  );
741
738
 
742
- // Remove the closing of the conditional IIFE at the end
739
+ // Remove the closing of the conditional IIFE at the end (development only)
743
740
  transformed = transformed.replace(/\}\)\(\);?\s*$/, "");
744
741
 
745
- // Replace require('react') and require('react-dom') with imports
742
+ // Replace require('react') and require('react-dom') with imports (development)
746
743
  transformed = transformed.replace(
747
744
  /var\s+React\s*=\s*require\s*\(\s*["']react["']\s*\)\s*,[\s\n]+ReactDOM\s*=\s*require\s*\(\s*["']react-dom["']\s*\)\s*,/g,
748
745
  'import React from "react";\nimport ReactDOM from "react-dom";\nvar '
749
746
  );
750
747
 
748
+ // Replace require('react-dom') only (production - doesn't import React)
749
+ transformed = transformed.replace(
750
+ /var\s+ReactDOM\s*=\s*require\s*\(\s*["']react-dom["']\s*\)\s*,/g,
751
+ 'import ReactDOM from "react-dom";\nvar '
752
+ );
753
+
751
754
  // Transform exports.xyz = function() to export function xyz()
752
755
  transformed = transformed.replace(
753
756
  /exports\.(\w+)\s*=\s*function\s*\(/g,