@module-federation/vite 1.20.4 → 1.20.5

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 +47 -64
  2. package/package.json +1 -1
package/lib/index.js CHANGED
@@ -934,24 +934,7 @@ function normalizeSharedKey(key) {
934
934
  }
935
935
  function normalizeShared(shared) {
936
936
  explicitSharedKeys = /* @__PURE__ */ new Set();
937
- if (!shared) {
938
- const result = {};
939
- const packageJsonPath = path$1.join(getPackageDetectionCwd(), "package.json");
940
- try {
941
- const packageJson = JSON.parse(fs$2.readFileSync(packageJsonPath, "utf-8"));
942
- if (packageJson.name === "@module-federation/vite") return result;
943
- Object.keys(packageJson.dependencies || {}).filter((key) => shouldAutoShareDependency(key)).forEach((key) => {
944
- result[key] = normalizeShareItem(key, {
945
- name: key,
946
- import: void 0,
947
- singleton: true
948
- });
949
- });
950
- } catch {
951
- return result;
952
- }
953
- return result;
954
- }
937
+ if (!shared) return {};
955
938
  const result = {};
956
939
  const sourceEntries = [];
957
940
  if (Array.isArray(shared)) shared.forEach((key) => {
@@ -984,20 +967,6 @@ function normalizeShared(shared) {
984
967
  function isModuleFederationRuntimePackage(key) {
985
968
  return key === "@module-federation/runtime" || key === "@module-federation/runtime-core";
986
969
  }
987
- function shouldAutoShareDependency(key) {
988
- const pkg = getInstalledPackageJson(key)?.packageJson;
989
- if (!pkg) return false;
990
- if (!pkg.browser && !pkg.exports && typeof pkg.module !== "string") return false;
991
- if (key === "@module-federation/vite" || isModuleFederationRuntimePackage(key)) return false;
992
- const entry = [
993
- pkg.module,
994
- pkg.main,
995
- typeof pkg.exports === "string" ? pkg.exports : void 0
996
- ].find((value) => typeof value === "string")?.replace(/\\/g, "/");
997
- if (key === "nuxt" || key === "nuxt-nightly") return false;
998
- if (key.includes("nuxt") && entry?.endsWith("/dist/module.mjs")) return false;
999
- return true;
1000
- }
1001
970
  function normalizeLibrary(library) {
1002
971
  if (!library) return void 0;
1003
972
  return library;
@@ -5955,16 +5924,16 @@ const VUE_HMR_RUNTIME_GUARD_SCRIPT = `
5955
5924
  * the remote's bindings, so the render throws and Vue falls back to a full
5956
5925
  * reload required warning.
5957
5926
  *
5958
- * Fix: rewrite the remote's emitted `__hmrId` literal to be prefixed with the
5959
- * federation `name`, so the remote's instances live under a distinct key. The
5960
- * accept callback emitted by plugin-vue reads `_sfc_main.__hmrId` /
5961
- * `updated.__hmrId` at runtime, so rewriting the literal once is enough.
5927
+ * Fix: rewrite the remote's emitted HMR id literals to be prefixed with the
5928
+ * federation `name`, so the remote's instances live under a distinct key.
5929
+ * Main SFC modules assign `.__hmrId`; template submodules pass the same id
5930
+ * directly to `__VUE_HMR_RUNTIME__.rerender()`.
5962
5931
  */
5963
- const SFC_HMR_ID_LITERAL_RE = /(\.__hmrId\s*=\s*["'`])([^"'`]+)(["'`])/g;
5964
- function rewriteSfcHmrId(code, federationName) {
5932
+ const VUE_HMR_ID_LITERAL_RE = /((?:\.__hmrId\s*=|__VUE_HMR_RUNTIME__\.rerender\()\s*["'`])([^"'`]+)(["'`])/g;
5933
+ function rewriteVueHmrIds(code, federationName) {
5965
5934
  let matched = false;
5966
5935
  return {
5967
- code: code.replace(SFC_HMR_ID_LITERAL_RE, (_match, prefix, id, suffix) => {
5936
+ code: code.replace(VUE_HMR_ID_LITERAL_RE, (_match, prefix, id, suffix) => {
5968
5937
  matched = true;
5969
5938
  if (id.startsWith(`${federationName}-`)) return `${prefix}${id}${suffix}`;
5970
5939
  return `${prefix}${federationName}-${id}${suffix}`;
@@ -5976,7 +5945,7 @@ let pluginVueRegressionWarned = false;
5976
5945
  function warnPluginVueRegression() {
5977
5946
  if (pluginVueRegressionWarned) return;
5978
5947
  pluginVueRegressionWarned = true;
5979
- mfWarn("Detected a Vue SFC module that calls `__VUE_HMR_RUNTIME__.createRecord(` but no `.__hmrId = \"...\"` literal could be rewritten. @vitejs/plugin-vue may have changed its output format — without the rewrite, host and remote SFCs that share a path will collide on the shared HMR runtime. Please report this to @module-federation/vite.");
5948
+ mfWarn("Detected a Vue SFC module with HMR calls but no HMR id literal could be rewritten. @vitejs/plugin-vue may have changed its output format — without the rewrite, host and remote SFCs that share a path will collide on the shared HMR runtime. Please report this to @module-federation/vite.");
5980
5949
  }
5981
5950
  const vueAdapter = {
5982
5951
  name: "vue",
@@ -5989,8 +5958,8 @@ const vueAdapter = {
5989
5958
  }];
5990
5959
  } },
5991
5960
  remote: { transform(code, _id, ctx) {
5992
- if (!code.includes("__VUE_HMR_RUNTIME__.createRecord(")) return;
5993
- const { code: rewritten, matched } = rewriteSfcHmrId(code, ctx.options.name);
5961
+ if (!code.includes("__VUE_HMR_RUNTIME__.createRecord(") && !code.includes("__VUE_HMR_RUNTIME__.rerender(")) return;
5962
+ const { code: rewritten, matched } = rewriteVueHmrIds(code, ctx.options.name);
5994
5963
  if (!matched) {
5995
5964
  warnPluginVueRegression();
5996
5965
  return;
@@ -8962,8 +8931,8 @@ function readBoundedRunnerBody(req, res) {
8962
8931
  function pluginSSRRemoteEntry(options) {
8963
8932
  const remoteEntrySSRId = getRemoteEntrySSRId(options);
8964
8933
  const virtualExposesSSRId = getVirtualExposesSSRId(options);
8965
- let isRolldown = false;
8966
- let ssrOutputFilename = "";
8934
+ let cachedSsrRemoteEntrySource;
8935
+ const ssrOutputFilename = getSsrRemoteEntryFileName(options.filename);
8967
8936
  let ssrOutputFiles = /* @__PURE__ */ new Set();
8968
8937
  let ssrOutputDir = "";
8969
8938
  let clientOutputDir = "";
@@ -8980,6 +8949,16 @@ function pluginSSRRemoteEntry(options) {
8980
8949
  let viteConfig;
8981
8950
  let isNuxtProject = false;
8982
8951
  let reactIslandExposes = /* @__PURE__ */ new Set();
8952
+ const isSsrRemoteEntryBuild = (environment) => {
8953
+ const environmentName = environment?.name;
8954
+ const hasSsrEnvironment = Boolean(viteConfig?.environments?.ssr);
8955
+ const isLegacySsrBuild = Boolean(environment?.config?.build?.ssr ?? viteConfig?.build?.ssr);
8956
+ return Object.keys(options.exposes).length > 0 && !(hasSsrEnvironment && environmentName !== "ssr" || !hasSsrEnvironment && !isLegacySsrBuild);
8957
+ };
8958
+ const getSsrRemoteEntrySource = () => {
8959
+ if (cachedSsrRemoteEntrySource === void 0) cachedSsrRemoteEntrySource = generateRemoteEntrySSR(options);
8960
+ return cachedSsrRemoteEntrySource;
8961
+ };
8983
8962
  const findNuxtExposesChunk = (bundle) => {
8984
8963
  const exposeKeys = Object.keys(options.exposes);
8985
8964
  if (exposeKeys.length === 0) return;
@@ -9106,13 +9085,9 @@ function pluginSSRRemoteEntry(options) {
9106
9085
  },
9107
9086
  buildStart() {
9108
9087
  if (isServe) return;
9109
- isRolldown = getIsRolldown(this);
9110
- ssrOutputFilename = getSsrRemoteEntryFileName(options.filename);
9111
- const environmentName = this.environment?.name;
9112
- const hasSsrEnvironment = Boolean(viteConfig?.environments?.ssr);
9113
- const isLegacySsrBuild = Boolean(this.environment?.config?.build?.ssr ?? viteConfig?.build?.ssr);
9114
- if (hasSsrEnvironment && environmentName !== "ssr" || !hasSsrEnvironment && !isLegacySsrBuild) return;
9115
- if (Object.keys(options.exposes).length === 0) return;
9088
+ const isRolldown = getIsRolldown(this);
9089
+ const environment = this.environment;
9090
+ if (!isSsrRemoteEntryBuild(environment)) return;
9116
9091
  if (isRolldown) this.emitFile({
9117
9092
  type: "chunk",
9118
9093
  id: remoteEntrySSRId,
@@ -9120,20 +9095,27 @@ function pluginSSRRemoteEntry(options) {
9120
9095
  fileName: ssrOutputFilename,
9121
9096
  preserveSignature: "strict"
9122
9097
  });
9123
- else this.emitFile({
9124
- type: "asset",
9125
- fileName: ssrOutputFilename,
9126
- source: generateRemoteEntrySSR(options)
9127
- });
9128
9098
  },
9129
- generateBundle(_options, bundle) {
9130
- const exposesChunk = findNuxtExposesChunk(bundle);
9131
- const ssrAsset = bundle[ssrOutputFilename];
9132
- if (exposesChunk && ssrAsset?.type === "asset" && typeof ssrAsset.source === "string") ssrAsset.source = ssrAsset.source.replace(/import\("virtual:mf-exposes-ssr:[^"]+"\)/g, `import("./${exposesChunk}")`);
9133
- if (this.environment?.name === "ssr") ssrOutputFiles = collectEntryOutputFiles(bundle, ssrOutputFilename);
9134
- if (!isRolldown) return;
9135
- const chunk = bundle[ssrOutputFilename];
9136
- if (!chunk || chunk.type !== "chunk") return;
9099
+ generateBundle: {
9100
+ order: "post",
9101
+ handler(_options, bundle) {
9102
+ const isRolldown = getIsRolldown(this);
9103
+ const environment = this.environment;
9104
+ const exposesChunk = findNuxtExposesChunk(bundle);
9105
+ if (!isRolldown && isSsrRemoteEntryBuild(environment)) {
9106
+ let source = getSsrRemoteEntrySource();
9107
+ if (exposesChunk) source = source.replace(/import\("virtual:mf-exposes-ssr:[^"]+"\)/g, `import("./${exposesChunk}")`);
9108
+ this.emitFile({
9109
+ type: "asset",
9110
+ fileName: ssrOutputFilename,
9111
+ source
9112
+ });
9113
+ }
9114
+ if (this.environment?.name === "ssr") ssrOutputFiles = collectEntryOutputFiles(bundle, ssrOutputFilename);
9115
+ if (!isRolldown) return;
9116
+ const chunk = bundle[ssrOutputFilename];
9117
+ if (!chunk || chunk.type !== "chunk") return;
9118
+ }
9137
9119
  },
9138
9120
  writeBundle(outputOptions) {
9139
9121
  const environmentName = this.environment?.name;
@@ -9976,6 +9958,7 @@ function federation(mfUserOptions) {
9976
9958
  name: "vite:module-federation-virtual-modules",
9977
9959
  enforce: "pre",
9978
9960
  resolveId(id) {
9961
+ if (id === "@module-federation/vite/ssrEntryLoader") return resolveImportPath(id);
9979
9962
  let virtualModule = VirtualModule.findById(id);
9980
9963
  if (!virtualModule) {
9981
9964
  materializeCachedLoadShareModule({
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@module-federation/vite",
3
- "version": "1.20.4",
3
+ "version": "1.20.5",
4
4
  "description": "Vite plugin for Module Federation",
5
5
  "type": "module",
6
6
  "main": "./lib/index.js",