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

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.11",
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,57 @@ 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(
1254
+ /^\s*["']use strict["'];\s*["']production["']\s*!==\s*process\.env\.NODE_ENV\s*&&\s*\(function\s*\(\)\s*\{/,
1255
+ ""
1256
+ );
1257
+ transformed = transformed.replace(/\}\)\(\);?\s*$/, "");
1258
+ transformed = transformed.replace(
1259
+ /var\s+React\s*=\s*require\s*\(\s*["']react["']\s*\)\s*,[\s\n]+ReactDOM\s*=\s*require\s*\(\s*["']react-dom["']\s*\)\s*,/g,
1260
+ 'import React from "react";\nimport ReactDOM from "react-dom";\nvar '
1261
+ );
1262
+ transformed = transformed.replace(
1263
+ /exports\.(\w+)\s*=\s*function\s*\(/g,
1264
+ "export function $1("
1265
+ );
1266
+ transformed = transformed.replace(
1267
+ /exports\.(\w+)\s*=/g,
1268
+ "export const $1 ="
1269
+ );
1270
+ transformed = license + "\n" + transformed;
1271
+ return {
1272
+ code: transformed,
1273
+ map: null
1274
+ };
1275
+ }
1276
+ return null;
1277
+ }
1278
+ };
1279
+ }
1237
1280
  export {
1238
1281
  exposeActionId,
1239
1282
  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.11",
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
  }