@module-federation/vite 1.20.8 → 1.20.9

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.
Files changed (2) hide show
  1. package/lib/index.js +55 -20
  2. package/package.json +5 -5
package/lib/index.js CHANGED
@@ -5378,7 +5378,7 @@ function generateRemotes(id, command, enableSsrInit = false, consumer = "unified
5378
5378
  }`;
5379
5379
  const realRemoteInit = `__mfRemotePending = __mfStartRemoteLoad().then(__mfAssignRemoteModule);`;
5380
5380
  const deferredClientInit = `exportModule = __mfCreateDeferredRemoteProxy();`;
5381
- const eagerLoadClientRemote = shouldEagerLoadClientRemoteInDev(command, enableSsrInit);
5381
+ const eagerLoadClientRemote = id === remoteRegistration?.alias || shouldEagerLoadClientRemoteInDev(command, enableSsrInit);
5382
5382
  const eagerClientInit = eagerLoadClientRemote ? getEagerDeferredClientInit() : deferredClientInit;
5383
5383
  const loadedFirstClientInit = eagerLoadClientRemote ? getEagerDeferredClientInit() : deferredClientInit;
5384
5384
  const environmentSplitInit = (clientInit, serverInit) => consumer === "client" ? clientInit : consumer === "server" ? serverInit : `if (${SERVER_ENV_GUARD}) {
@@ -5515,20 +5515,25 @@ function resolveDevHashEntryFileName$1(fileName) {
5515
5515
  function getBuildInput(config) {
5516
5516
  return config.build?.rollupOptions?.input ?? config.build?.rolldownOptions?.input;
5517
5517
  }
5518
- function patchHashEntryFileName(output, entryName, fileName) {
5519
- const originalEntryFileNames = output.entryFileNames;
5520
- output.entryFileNames = (chunkInfo, ...args) => {
5521
- if (chunkInfo?.name === entryName) return fileName;
5522
- if (typeof originalEntryFileNames === "function") return originalEntryFileNames(chunkInfo, ...args);
5523
- return originalEntryFileNames || "assets/[name]-[hash].js";
5524
- };
5518
+ function patchHashEntryFileName(output, entryName, fileName, defaultFileNames) {
5519
+ for (const option of ["entryFileNames", "chunkFileNames"]) {
5520
+ const originalFileNames = output[option];
5521
+ output[option] = (chunkInfo, ...args) => {
5522
+ if (chunkInfo?.name === entryName) return fileName;
5523
+ if (typeof originalFileNames === "function") return originalFileNames(chunkInfo, ...args);
5524
+ return originalFileNames || defaultFileNames;
5525
+ };
5526
+ }
5525
5527
  }
5526
5528
  function patchHashEntryFileNames(config, entryName, fileName) {
5527
5529
  if (!fileName?.includes?.("[hash")) return;
5530
+ fileName = fileName.replace(/(\[hash(?::\d+)?\])$/, "$1.js");
5528
5531
  config.build ??= {};
5529
5532
  config.build.rollupOptions ??= {};
5530
5533
  config.build.rolldownOptions ??= {};
5531
- const patchOutput = (output) => patchHashEntryFileName(output, entryName, fileName);
5534
+ const assetsDir = config.build.assetsDir ?? "assets";
5535
+ const defaultFileNames = `${assetsDir ? `${assetsDir}/` : ""}[name]-[hash].js`;
5536
+ const patchOutput = (output) => patchHashEntryFileName(output, entryName, fileName, defaultFileNames);
5532
5537
  const patchBundlerOutput = (bundlerOptions) => {
5533
5538
  const output = bundlerOptions.output;
5534
5539
  if (Array.isArray(output)) {
@@ -5539,6 +5544,7 @@ function patchHashEntryFileNames(config, entryName, fileName) {
5539
5544
  };
5540
5545
  patchBundlerOutput(config.build.rollupOptions);
5541
5546
  patchBundlerOutput(config.build.rolldownOptions);
5547
+ Object.values(config.environments ?? {}).forEach((environment) => patchHashEntryFileNames(environment, entryName, fileName));
5542
5548
  }
5543
5549
  const addEntry = ({ entryName, entryPath, fileName, inject = "entry", forceClientInjected, skipTransformFor = [], federationOptions }) => {
5544
5550
  const DEV_HTML_PROXY_PREFIX = "virtual:mf-html-entry-proxy?";
@@ -6099,6 +6105,7 @@ const REACT_REFRESH_PROXY_MODULE = [
6099
6105
  `const __rt = await import(__target);`,
6100
6106
  `export const injectIntoGlobalHook = __rt.injectIntoGlobalHook;`,
6101
6107
  `export const register = __rt.register;`,
6108
+ `export const getRefreshReg = __rt.getRefreshReg;`,
6102
6109
  `export const createSignatureFunctionForTransform = __rt.createSignatureFunctionForTransform;`,
6103
6110
  `export const registerExportsForReactRefresh = __rt.registerExportsForReactRefresh;`,
6104
6111
  `export const validateRefreshBoundaryAndEnqueueUpdate = __rt.validateRefreshBoundaryAndEnqueueUpdate;`,
@@ -7742,12 +7749,18 @@ function pluginProxyRemoteEntry_default({ options, remoteEntryId, virtualExposes
7742
7749
  return Object.keys(options.remotes).some((name) => source === name || source.startsWith(name + "/"));
7743
7750
  }
7744
7751
  function collectImportSources(code) {
7745
- const sources = /* @__PURE__ */ new Set();
7752
+ const sources = /* @__PURE__ */ new Map();
7746
7753
  for (const match of code.matchAll(/(?:^|[;\n\r])\s*import\s+(?:[\s\S]*?\s+from\s+)?["']([^"']+)["']|import\(\s*["']([^"']+)["']\s*\)/g)) {
7747
7754
  const source = match[1] || match[2];
7748
- if (source) sources.add(source);
7755
+ if (source) {
7756
+ const dynamic = !match[1];
7757
+ sources.set(source, (sources.get(source) ?? true) && dynamic);
7758
+ }
7749
7759
  }
7750
- return Array.from(sources).sort();
7760
+ return Array.from(sources, ([source, dynamic]) => ({
7761
+ source,
7762
+ dynamic
7763
+ })).sort((a, b) => a.source.localeCompare(b.source));
7751
7764
  }
7752
7765
  function shouldScanResolvedImport(id) {
7753
7766
  if (!id || id.includes("\0")) return false;
@@ -7764,9 +7777,9 @@ function pluginProxyRemoteEntry_default({ options, remoteEntryId, virtualExposes
7764
7777
  return [];
7765
7778
  }
7766
7779
  const dependencies = /* @__PURE__ */ new Set();
7767
- for (const source of collectImportSources(code)) {
7780
+ for (const { source, dynamic } of collectImportSources(code)) {
7768
7781
  if (isRemoteImport(source)) {
7769
- dependencies.add(source);
7782
+ if (!dynamic) dependencies.add(source);
7770
7783
  continue;
7771
7784
  }
7772
7785
  const resolved = await ctx.resolve(source, id);
@@ -8430,8 +8443,10 @@ function applyRewrites(code, imports, id) {
8430
8443
  importParts.push(`__mf_remote_pending as ${pendingId}`);
8431
8444
  let rewrite = `import { ${importParts.join(", ")} } from ${src};`;
8432
8445
  if (imp.named.length > 0) {
8433
- const destructParts = imp.named.map((s) => `${s.imported}: ${s.local}`);
8434
- rewrite += `\nconst { ${destructParts.join(", ")} } = ${nsId};`;
8446
+ const declarations = imp.named.map((s) => `let ${s.local};`).join("\n");
8447
+ const initializers = imp.named.map((s) => `if (${JSON.stringify(s.imported)} in ${nsId}) {\n${s.local} = ${nsId}[${JSON.stringify(s.imported)}];\n}`).join("\n");
8448
+ const assignments = imp.named.map((s) => `${s.local} = ${nsId}[${JSON.stringify(s.imported)}];`).join("\n");
8449
+ rewrite += `\n${declarations}\n${initializers}\n${pendingId}.then(() => {\n${assignments}\n});`;
8435
8450
  }
8436
8451
  ms.overwrite(imp.start, imp.end, rewrite);
8437
8452
  }
@@ -8451,10 +8466,11 @@ function applyRewrites(code, imports, id) {
8451
8466
  };
8452
8467
  });
8453
8468
  const importLine = `import { __moduleExports as ${nsId}, __mf_remote_pending as ${pendingId} } from ${src};`;
8454
- const varLines = vars.map((v) => `let ${v.tmp} = ${nsId}[${JSON.stringify(v.local)}];`).join("\n");
8469
+ const varLines = vars.map((v) => `let ${v.tmp};`).join("\n");
8470
+ const initializers = vars.map((v) => `if (${JSON.stringify(v.local)} in ${nsId}) {\n${v.tmp} = ${nsId}[${JSON.stringify(v.local)}];\n}`).join("\n");
8455
8471
  const syncLine = `${pendingId}.then(() => {\n${vars.map((v) => `${v.tmp} = ${nsId}[${JSON.stringify(v.local)}];`).join("\n")}\n});`;
8456
8472
  const exportLine = `export { ${vars.map((v) => `${v.tmp} as ${v.exported}`).join(", ")} };`;
8457
- ms.overwrite(imp.start, imp.end, `${importLine}\n${varLines}\n${syncLine}\n${exportLine}`);
8473
+ ms.overwrite(imp.start, imp.end, `${importLine}\n${varLines}\n${initializers}\n${syncLine}\n${exportLine}`);
8458
8474
  changed = true;
8459
8475
  break;
8460
8476
  }
@@ -9422,6 +9438,14 @@ function includeLinkedSharedEntries(optimizeDeps, shared, projectRoot, exposes,
9422
9438
  ]);
9423
9439
  for (const [packageName, share] of Object.entries(shared ?? {})) {
9424
9440
  if (share?.shareConfig?.import === false) continue;
9441
+ const configuredImport = share?.shareConfig?.import;
9442
+ if (typeof configuredImport === "string") {
9443
+ const entry = path$1.isAbsolute(configuredImport) ? configuredImport : path$1.resolve(projectRoot, configuredImport);
9444
+ if (existsSync(entry) && !entry.replaceAll("\\", "/").includes("/node_modules/")) {
9445
+ additions.add(entry);
9446
+ continue;
9447
+ }
9448
+ }
9425
9449
  const installed = getInstalledPackageJson(packageName, { cwd: projectRoot });
9426
9450
  if (!installed || installed.dir.replaceAll("\\", "/").includes("/node_modules/")) continue;
9427
9451
  const entry = getInstalledPackageEntry(packageName, { cwd: projectRoot });
@@ -9604,9 +9628,20 @@ function createEarlyVirtualModulesPlugin(options) {
9604
9628
  if (args.kind === "entry-point") return;
9605
9629
  if (!args.importer || args.namespace === "mf-shared") return;
9606
9630
  if (isSharedResolverInternalImporter(args.importer)) return;
9607
- if (!findSharedKey(args.path, shared) || isAssetLikeImport(args.path)) return;
9631
+ const key = findSharedKey(args.path, shared);
9632
+ if (!key || isAssetLikeImport(args.path)) return;
9608
9633
  if (getPackageNameFromNodeModulePath(args.importer) === getPackageName(args.path) && !isReactDomSelfReference(args.path, args.importer)) return;
9609
9634
  addUsedShares(args.path, options);
9635
+ if (args.kind === "import-statement" || args.kind === "dynamic-import") {
9636
+ const shareItem = shared[key];
9637
+ const loadSharePath = getLoadShareModulePath(args.path, isRolldown, options);
9638
+ writeLoadShareModule(args.path, shareItem, _command, isRolldown, options);
9639
+ if (shareItem.shareConfig?.import !== false) writePreBuildLibPath(args.path, shareItem, options);
9640
+ return {
9641
+ path: loadSharePath,
9642
+ external: true
9643
+ };
9644
+ }
9610
9645
  return {
9611
9646
  path: args.path,
9612
9647
  namespace: "mf-shared"
@@ -9663,7 +9698,7 @@ export default __mfShared.default ?? __mfShared;`
9663
9698
  optimizeDeps.exclude ??= [];
9664
9699
  const shouldBypassOptimizeDep = isLitShare(key) || !canResolveSharedSubpath(key, root);
9665
9700
  if (optimizeDeps.include.includes(key)) optimizeDeps.exclude = optimizeDeps.exclude.filter((dep) => dep !== key);
9666
- else if (shouldBypassOptimizeDep) optimizeDeps.exclude.push(key);
9701
+ else if (shouldBypassOptimizeDep || optimizeDeps.exclude.includes(key)) optimizeDeps.exclude.push(key);
9667
9702
  else optimizeDeps.include.push(key);
9668
9703
  for (const subpath of getCommonSharedSubpaths(key)) {
9669
9704
  const canResolveSubpath = canResolveSharedSubpath(subpath, root);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@module-federation/vite",
3
- "version": "1.20.8",
3
+ "version": "1.20.9",
4
4
  "description": "Vite plugin for Module Federation",
5
5
  "type": "module",
6
6
  "main": "./lib/index.js",
@@ -77,9 +77,9 @@
77
77
  "vite": "^5.0.0 || ^6.0.0 || ^7.0.0 || ^8.0.0"
78
78
  },
79
79
  "dependencies": {
80
- "@module-federation/dts-plugin": "2.8.2",
81
- "@module-federation/runtime": "2.8.2",
82
- "@module-federation/sdk": "2.8.2"
80
+ "@module-federation/dts-plugin": "2.9.0",
81
+ "@module-federation/runtime": "2.9.0",
82
+ "@module-federation/sdk": "2.9.0"
83
83
  },
84
84
  "devDependencies": {
85
85
  "@playwright/test": "1.62.0",
@@ -93,4 +93,4 @@
93
93
  "vite": "8.2.0",
94
94
  "vitest": "4.1.10"
95
95
  }
96
- }
96
+ }