@module-federation/vite 1.14.0 → 1.14.1

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/lib/index.cjs CHANGED
@@ -640,6 +640,13 @@ function pluginDevRemoteHmr(options) {
640
640
  connections.length = 0;
641
641
  };
642
642
  for (const [remoteName, remote] of Object.entries(options.remotes)) connectRemote(remoteName, remote);
643
+ const triggerHostReload = (file) => {
644
+ if (shouldIgnoreFile(file, options)) return;
645
+ server.ws.send({ type: "full-reload" });
646
+ };
647
+ server.watcher.on("change", triggerHostReload);
648
+ server.watcher.on("add", triggerHostReload);
649
+ server.watcher.on("unlink", triggerHostReload);
643
650
  server.httpServer?.once("close", teardown);
644
651
  }
645
652
  }
@@ -898,11 +905,20 @@ function normalizeRemotes(remotes) {
898
905
  }
899
906
  function normalizeRemoteItem(key, remote) {
900
907
  if (typeof remote === "string") {
901
- const [entryGlobalName] = remote.split("@");
908
+ const separatorIndex = remote.startsWith("@") ? remote.indexOf("@", 1) : remote.indexOf("@");
909
+ let entryGlobalName;
910
+ let entry;
911
+ if (separatorIndex > 0) {
912
+ entryGlobalName = remote.slice(0, separatorIndex);
913
+ entry = remote.slice(separatorIndex + 1);
914
+ } else {
915
+ entryGlobalName = remote;
916
+ entry = remote;
917
+ }
902
918
  return {
903
919
  type: "var",
904
920
  name: key,
905
- entry: remote.replace(entryGlobalName + "@", ""),
921
+ entry,
906
922
  entryGlobalName,
907
923
  shareScope: "default"
908
924
  };
@@ -928,8 +944,13 @@ function searchPackageVersion(sharedName) {
928
944
  while (pathe.parse(potentialPackageJsonDir).base !== "node_modules" && potentialPackageJsonDir !== rootDir) {
929
945
  const potentialPackageJsonPath = pathe.join(potentialPackageJsonDir, "package.json");
930
946
  if (fs.existsSync(potentialPackageJsonPath)) {
931
- const potentialPackageJson = JSON.parse(fs.readFileSync(potentialPackageJsonPath, "utf-8"));
932
- if (typeof potentialPackageJson == "object" && potentialPackageJson !== null && typeof potentialPackageJson.version === "string" && potentialPackageJson.name === sharedName) return potentialPackageJson.version;
947
+ const potentialPackageJsonContent = fs.readFileSync(potentialPackageJsonPath, "utf-8");
948
+ try {
949
+ const potentialPackageJson = JSON.parse(potentialPackageJsonContent);
950
+ if (typeof potentialPackageJson == "object" && potentialPackageJson !== null && typeof potentialPackageJson.version === "string" && potentialPackageJson.name === sharedName) return potentialPackageJson.version;
951
+ } catch (error) {
952
+ if (!(error instanceof SyntaxError)) throw error;
953
+ }
933
954
  }
934
955
  potentialPackageJsonDir = pathe.dirname(potentialPackageJsonDir);
935
956
  }
@@ -1453,13 +1474,23 @@ function getInstalledPackageJsonPath(pkg) {
1453
1474
  while (currentDir !== rootDir) {
1454
1475
  const packageJsonPath = pathe.default.join(currentDir, "package.json");
1455
1476
  if ((0, fs.existsSync)(packageJsonPath)) {
1456
- if (JSON.parse((0, fs.readFileSync)(packageJsonPath, "utf-8")).name === packageName) return packageJsonPath;
1477
+ const packageJsonContent = (0, fs.readFileSync)(packageJsonPath, "utf-8");
1478
+ try {
1479
+ if (JSON.parse(packageJsonContent).name === packageName) return packageJsonPath;
1480
+ } catch (error) {
1481
+ if (!(error instanceof SyntaxError)) throw error;
1482
+ }
1457
1483
  }
1458
1484
  currentDir = pathe.default.dirname(currentDir);
1459
1485
  }
1460
1486
  const rootPackageJsonPath = pathe.default.join(rootDir, "package.json");
1461
1487
  if ((0, fs.existsSync)(rootPackageJsonPath)) {
1462
- if (JSON.parse((0, fs.readFileSync)(rootPackageJsonPath, "utf-8")).name === packageName) return rootPackageJsonPath;
1488
+ const rootPackageJsonContent = (0, fs.readFileSync)(rootPackageJsonPath, "utf-8");
1489
+ try {
1490
+ if (JSON.parse(rootPackageJsonContent).name === packageName) return rootPackageJsonPath;
1491
+ } catch (error) {
1492
+ if (!(error instanceof SyntaxError)) throw error;
1493
+ }
1463
1494
  }
1464
1495
  } catch {
1465
1496
  const packageName = removePathFromNpmPackage(pkg);
@@ -3111,6 +3142,16 @@ var aliasToArrayPlugin_default = {
3111
3142
  }
3112
3143
  };
3113
3144
  //#endregion
3145
+ //#region src/utils/isTestEnv.ts
3146
+ /**
3147
+ * Detects whether the current process is running in a test environment
3148
+ * Set `MFE_VITE_NO_TEST_ENV_CHECK=true` to load federation plugins during tests.
3149
+ */
3150
+ function isTestEnv() {
3151
+ if (process.env.MFE_VITE_NO_TEST_ENV_CHECK === "true") return false;
3152
+ return process.env.NODE_ENV === "test" || process.env.VITEST != null || process.env.JEST_WORKER_ID != null;
3153
+ }
3154
+ //#endregion
3114
3155
  //#region src/utils/controlChunkSanitizer.ts
3115
3156
  const FEDERATION_CONTROL_CHUNK_HINTS = [
3116
3157
  "hostInit",
@@ -3256,6 +3297,7 @@ function createEarlyVirtualModulesPlugin(options) {
3256
3297
  };
3257
3298
  }
3258
3299
  function federation(mfUserOptions) {
3300
+ if (isTestEnv()) return [];
3259
3301
  const options = normalizeModuleFederationOptions(mfUserOptions);
3260
3302
  const isVinext = hasPackageDependency("vinext");
3261
3303
  const { name, remotes, shared, filename, hostInitInjectLocation } = options;
@@ -3651,12 +3693,12 @@ function federation(mfUserOptions) {
3651
3693
  const prefixToRoot = chunkDir === "." ? "" : `${pathe.default.relative(chunkDir, ".")}/`;
3652
3694
  const replacementExpr = prefixToRoot ? `${escapeUnsafeJsSourceChars(JSON.stringify(prefixToRoot))}+$1` : "$1";
3653
3695
  const replacement = `=function($1){return new URL(${replacementExpr},import.meta.url).href}`;
3654
- const replaced = chunk.code.replace(/=\(?(\w+)(?:,\w+)?\)?\s*=>\s*["'`][^"'`]*["'`]\s*\+\s*\1/, replacement);
3696
+ const replaced = chunk.code.replace(/=\(?(\w+)(?:,\w+)?\)?\s*=>\s*["'][./][^"']*["']\s*\+\s*\1/, replacement);
3655
3697
  if (replaced !== chunk.code) {
3656
3698
  chunk.code = replaced;
3657
3699
  continue;
3658
3700
  }
3659
- chunk.code = chunk.code.replace(/=function\((\w+)(?:,\w+)?\)\{return\s*["'`][^"'`]*["'`]\s*\+\s*\1\s*\}/, replacement);
3701
+ chunk.code = chunk.code.replace(/=function\((\w+)(?:,\w+)?\)\{return\s*["'][./][^"']*["']\s*\+\s*\1\s*\}/, replacement);
3660
3702
  chunk.code = chunk.code.replace(/=function\((\w+)(?:,\w+)?\)\{return new URL\("\.\.\/"\+\1,import\.meta\.url\)\.href\}/, replacement);
3661
3703
  chunk.code = chunk.code.replace(/new URL\("\.\.\/"\+(\w+),import\.meta\.url\)\.href/g, `new URL(${replacementExpr},import.meta.url).href`);
3662
3704
  }
package/lib/index.mjs CHANGED
@@ -618,6 +618,13 @@ function pluginDevRemoteHmr(options) {
618
618
  connections.length = 0;
619
619
  };
620
620
  for (const [remoteName, remote] of Object.entries(options.remotes)) connectRemote(remoteName, remote);
621
+ const triggerHostReload = (file) => {
622
+ if (shouldIgnoreFile(file, options)) return;
623
+ server.ws.send({ type: "full-reload" });
624
+ };
625
+ server.watcher.on("change", triggerHostReload);
626
+ server.watcher.on("add", triggerHostReload);
627
+ server.watcher.on("unlink", triggerHostReload);
621
628
  server.httpServer?.once("close", teardown);
622
629
  }
623
630
  }
@@ -876,11 +883,20 @@ function normalizeRemotes(remotes) {
876
883
  }
877
884
  function normalizeRemoteItem(key, remote) {
878
885
  if (typeof remote === "string") {
879
- const [entryGlobalName] = remote.split("@");
886
+ const separatorIndex = remote.startsWith("@") ? remote.indexOf("@", 1) : remote.indexOf("@");
887
+ let entryGlobalName;
888
+ let entry;
889
+ if (separatorIndex > 0) {
890
+ entryGlobalName = remote.slice(0, separatorIndex);
891
+ entry = remote.slice(separatorIndex + 1);
892
+ } else {
893
+ entryGlobalName = remote;
894
+ entry = remote;
895
+ }
880
896
  return {
881
897
  type: "var",
882
898
  name: key,
883
- entry: remote.replace(entryGlobalName + "@", ""),
899
+ entry,
884
900
  entryGlobalName,
885
901
  shareScope: "default"
886
902
  };
@@ -906,8 +922,13 @@ function searchPackageVersion(sharedName) {
906
922
  while (path$1.parse(potentialPackageJsonDir).base !== "node_modules" && potentialPackageJsonDir !== rootDir) {
907
923
  const potentialPackageJsonPath = path$1.join(potentialPackageJsonDir, "package.json");
908
924
  if (fs.existsSync(potentialPackageJsonPath)) {
909
- const potentialPackageJson = JSON.parse(fs.readFileSync(potentialPackageJsonPath, "utf-8"));
910
- if (typeof potentialPackageJson == "object" && potentialPackageJson !== null && typeof potentialPackageJson.version === "string" && potentialPackageJson.name === sharedName) return potentialPackageJson.version;
925
+ const potentialPackageJsonContent = fs.readFileSync(potentialPackageJsonPath, "utf-8");
926
+ try {
927
+ const potentialPackageJson = JSON.parse(potentialPackageJsonContent);
928
+ if (typeof potentialPackageJson == "object" && potentialPackageJson !== null && typeof potentialPackageJson.version === "string" && potentialPackageJson.name === sharedName) return potentialPackageJson.version;
929
+ } catch (error) {
930
+ if (!(error instanceof SyntaxError)) throw error;
931
+ }
911
932
  }
912
933
  potentialPackageJsonDir = path$1.dirname(potentialPackageJsonDir);
913
934
  }
@@ -1430,13 +1451,23 @@ function getInstalledPackageJsonPath(pkg) {
1430
1451
  while (currentDir !== rootDir) {
1431
1452
  const packageJsonPath = path.join(currentDir, "package.json");
1432
1453
  if (existsSync(packageJsonPath)) {
1433
- if (JSON.parse(readFileSync(packageJsonPath, "utf-8")).name === packageName) return packageJsonPath;
1454
+ const packageJsonContent = readFileSync(packageJsonPath, "utf-8");
1455
+ try {
1456
+ if (JSON.parse(packageJsonContent).name === packageName) return packageJsonPath;
1457
+ } catch (error) {
1458
+ if (!(error instanceof SyntaxError)) throw error;
1459
+ }
1434
1460
  }
1435
1461
  currentDir = path.dirname(currentDir);
1436
1462
  }
1437
1463
  const rootPackageJsonPath = path.join(rootDir, "package.json");
1438
1464
  if (existsSync(rootPackageJsonPath)) {
1439
- if (JSON.parse(readFileSync(rootPackageJsonPath, "utf-8")).name === packageName) return rootPackageJsonPath;
1465
+ const rootPackageJsonContent = readFileSync(rootPackageJsonPath, "utf-8");
1466
+ try {
1467
+ if (JSON.parse(rootPackageJsonContent).name === packageName) return rootPackageJsonPath;
1468
+ } catch (error) {
1469
+ if (!(error instanceof SyntaxError)) throw error;
1470
+ }
1440
1471
  }
1441
1472
  } catch {
1442
1473
  const packageName = removePathFromNpmPackage(pkg);
@@ -3088,6 +3119,16 @@ var aliasToArrayPlugin_default = {
3088
3119
  }
3089
3120
  };
3090
3121
  //#endregion
3122
+ //#region src/utils/isTestEnv.ts
3123
+ /**
3124
+ * Detects whether the current process is running in a test environment
3125
+ * Set `MFE_VITE_NO_TEST_ENV_CHECK=true` to load federation plugins during tests.
3126
+ */
3127
+ function isTestEnv() {
3128
+ if (process.env.MFE_VITE_NO_TEST_ENV_CHECK === "true") return false;
3129
+ return process.env.NODE_ENV === "test" || process.env.VITEST != null || process.env.JEST_WORKER_ID != null;
3130
+ }
3131
+ //#endregion
3091
3132
  //#region src/utils/controlChunkSanitizer.ts
3092
3133
  const FEDERATION_CONTROL_CHUNK_HINTS = [
3093
3134
  "hostInit",
@@ -3233,6 +3274,7 @@ function createEarlyVirtualModulesPlugin(options) {
3233
3274
  };
3234
3275
  }
3235
3276
  function federation(mfUserOptions) {
3277
+ if (isTestEnv()) return [];
3236
3278
  const options = normalizeModuleFederationOptions(mfUserOptions);
3237
3279
  const isVinext = hasPackageDependency("vinext");
3238
3280
  const { name, remotes, shared, filename, hostInitInjectLocation } = options;
@@ -3628,12 +3670,12 @@ function federation(mfUserOptions) {
3628
3670
  const prefixToRoot = chunkDir === "." ? "" : `${path.relative(chunkDir, ".")}/`;
3629
3671
  const replacementExpr = prefixToRoot ? `${escapeUnsafeJsSourceChars(JSON.stringify(prefixToRoot))}+$1` : "$1";
3630
3672
  const replacement = `=function($1){return new URL(${replacementExpr},import.meta.url).href}`;
3631
- const replaced = chunk.code.replace(/=\(?(\w+)(?:,\w+)?\)?\s*=>\s*["'`][^"'`]*["'`]\s*\+\s*\1/, replacement);
3673
+ const replaced = chunk.code.replace(/=\(?(\w+)(?:,\w+)?\)?\s*=>\s*["'][./][^"']*["']\s*\+\s*\1/, replacement);
3632
3674
  if (replaced !== chunk.code) {
3633
3675
  chunk.code = replaced;
3634
3676
  continue;
3635
3677
  }
3636
- chunk.code = chunk.code.replace(/=function\((\w+)(?:,\w+)?\)\{return\s*["'`][^"'`]*["'`]\s*\+\s*\1\s*\}/, replacement);
3678
+ chunk.code = chunk.code.replace(/=function\((\w+)(?:,\w+)?\)\{return\s*["'][./][^"']*["']\s*\+\s*\1\s*\}/, replacement);
3637
3679
  chunk.code = chunk.code.replace(/=function\((\w+)(?:,\w+)?\)\{return new URL\("\.\.\/"\+\1,import\.meta\.url\)\.href\}/, replacement);
3638
3680
  chunk.code = chunk.code.replace(/new URL\("\.\.\/"\+(\w+),import\.meta\.url\)\.href/g, `new URL(${replacementExpr},import.meta.url).href`);
3639
3681
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@module-federation/vite",
3
- "version": "1.14.0",
3
+ "version": "1.14.1",
4
4
  "description": "Vite plugin for Module Federation",
5
5
  "type": "module",
6
6
  "main": "./lib/index.cjs",