@module-federation/vite 1.16.4 → 1.16.6

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.
@@ -1,9 +1,52 @@
1
- import { createRequire } from "node:module";
2
1
  import { existsSync, readFileSync, readdirSync } from "fs";
3
- import { createRequire as createRequire$1 } from "module";
4
- import path from "pathe";
5
- //#region \0rolldown/runtime.js
6
- var __require = /* @__PURE__ */ createRequire(import.meta.url);
2
+ import { createRequire } from "module";
3
+ import * as path$1 from "node:path";
4
+ import { fileURLToPath, pathToFileURL } from "url";
5
+ //#region src/utils/buildPaths.ts
6
+ /**
7
+ * Rebase an import path for a bootstrap file that moved from root into `dir`.
8
+ *
9
+ * When entryFileNames places entries in a subdirectory (e.g. `static/js/`),
10
+ * the bootstrap file moves there too. Paths that resolved from the HTML root
11
+ * must resolve from the new directory instead.
12
+ *
13
+ * Cases: `/static/js/hostInit.js` → `./hostInit.js` (strip dir prefix)
14
+ * `./src/main.tsx` → `../../src/main.tsx` (climb back up for each dir level)
15
+ * `https://cdn.example.com` → unchanged (absolute URL)
16
+ */
17
+ function rebaseImport(importSrc, dir) {
18
+ if (!dir) return importSrc;
19
+ if (isAbsoluteUrl(importSrc)) return importSrc;
20
+ const normalizedDir = dir.replace(/^\/+|\/+$/g, "");
21
+ if (!normalizedDir) return importSrc;
22
+ const stripDirPrefix = (src, prefix) => {
23
+ if (src === prefix) return "";
24
+ if (src.startsWith(prefix + "/")) return src.slice(prefix.length);
25
+ };
26
+ const absoluteRemainder = stripDirPrefix(importSrc, "/" + normalizedDir);
27
+ if (absoluteRemainder !== void 0) {
28
+ const remainder = absoluteRemainder.replace(/^\/+/, "");
29
+ return remainder ? "./" + remainder : "./";
30
+ }
31
+ const relativeRemainder = stripDirPrefix(importSrc, normalizedDir);
32
+ if (relativeRemainder !== void 0) {
33
+ const remainder = relativeRemainder.replace(/^\/+/, "");
34
+ return remainder ? "./" + remainder : "./";
35
+ }
36
+ const upLevels = normalizedDir.split("/").filter(Boolean).length;
37
+ const prefix = upLevels > 0 ? "../".repeat(upLevels) : "./";
38
+ if (importSrc.startsWith("./")) return prefix + importSrc.slice(2);
39
+ if (importSrc.startsWith("/")) return prefix + importSrc.slice(1);
40
+ return prefix + importSrc;
41
+ }
42
+ function normalizePathForImport(path) {
43
+ return path.replace(/\\/g, "/");
44
+ }
45
+ const EXTERNAL_URL_RE = /^(?:[a-z]+:|\/\/)/i;
46
+ function isAbsoluteUrl(src) {
47
+ if (/^[a-z]:[\\/]/i.test(src)) return false;
48
+ return EXTERNAL_URL_RE.test(src);
49
+ }
7
50
  //#endregion
8
51
  //#region src/utils/logger.ts
9
52
  const MODULE_FEDERATION_LOG_PREFIX = "[Module Federation]";
@@ -49,6 +92,17 @@ function setPackageDetectionCwd(cwd) {
49
92
  function getPackageDetectionCwd() {
50
93
  return packageDetectionCwd || process.cwd();
51
94
  }
95
+ function resolveImportPath(specifier) {
96
+ const resolved = import.meta.resolve(specifier);
97
+ if (!resolved.startsWith("file:")) return resolved;
98
+ const filePath = fileURLToPath(resolved);
99
+ if (!existsSync(filePath)) {
100
+ const error = /* @__PURE__ */ new Error(`Cannot find module '${specifier}'`);
101
+ error.code = "MODULE_NOT_FOUND";
102
+ throw error;
103
+ }
104
+ return filePath;
105
+ }
52
106
  const DEFAULT_EXPORT_CONDITIONS = [
53
107
  "browser",
54
108
  "import",
@@ -134,7 +188,7 @@ function getInstalledPackageJson(pkg, opts) {
134
188
  try {
135
189
  return {
136
190
  path: packageJsonPath,
137
- dir: path.dirname(packageJsonPath),
191
+ dir: path$1.dirname(packageJsonPath),
138
192
  packageJson: JSON.parse(readFileSync(packageJsonPath, "utf-8"))
139
193
  };
140
194
  } catch {
@@ -143,22 +197,22 @@ function getInstalledPackageJson(pkg, opts) {
143
197
  };
144
198
  const findPackageInPnpmStore = (startDir) => {
145
199
  let currentDir = startDir;
146
- const rootDir = path.parse(currentDir).root;
200
+ const rootDir = path$1.parse(currentDir).root;
147
201
  while (true) {
148
- const pnpmStoreDir = path.join(currentDir, "node_modules", ".pnpm");
202
+ const pnpmStoreDir = path$1.join(currentDir, "node_modules", ".pnpm");
149
203
  if (existsSync(pnpmStoreDir)) try {
150
204
  for (const entry of readdirSync(pnpmStoreDir, { withFileTypes: true })) {
151
205
  if (!entry.isDirectory()) continue;
152
- const candidate = tryReadPackageJson(path.join(pnpmStoreDir, entry.name, "node_modules", packageName, "package.json"));
206
+ const candidate = tryReadPackageJson(path$1.join(pnpmStoreDir, entry.name, "node_modules", packageName, "package.json"));
153
207
  if (candidate?.packageJson.name === packageName) return candidate;
154
208
  }
155
209
  } catch {}
156
210
  if (currentDir === rootDir) break;
157
- currentDir = path.dirname(currentDir);
211
+ currentDir = path$1.dirname(currentDir);
158
212
  }
159
213
  };
160
214
  try {
161
- const projectRequire = createRequire$1(new URL(`file://${path.join(cwd, "package.json")}`));
215
+ const projectRequire = createRequire(pathToFileURL(path$1.join(cwd, "package.json")));
162
216
  let resolvedPath;
163
217
  if (opts?.fromResolvedEntry) resolvedPath = opts.fromResolvedEntry;
164
218
  else try {
@@ -166,10 +220,10 @@ function getInstalledPackageJson(pkg, opts) {
166
220
  } catch {
167
221
  resolvedPath = projectRequire.resolve(packageName);
168
222
  }
169
- let currentDir = path.dirname(resolvedPath);
170
- const rootDir = path.parse(currentDir).root;
223
+ let currentDir = path$1.dirname(resolvedPath);
224
+ const rootDir = path$1.parse(currentDir).root;
171
225
  while (true) {
172
- const packageJsonPath = path.join(currentDir, "package.json");
226
+ const packageJsonPath = path$1.join(currentDir, "package.json");
173
227
  if (existsSync(packageJsonPath)) {
174
228
  const packageJsonContent = readFileSync(packageJsonPath, "utf-8");
175
229
  try {
@@ -184,16 +238,16 @@ function getInstalledPackageJson(pkg, opts) {
184
238
  }
185
239
  }
186
240
  if (currentDir === rootDir) break;
187
- currentDir = path.dirname(currentDir);
241
+ currentDir = path$1.dirname(currentDir);
188
242
  }
189
243
  } catch {
190
244
  let currentDir = cwd;
191
- const rootDir = path.parse(currentDir).root;
245
+ const rootDir = path$1.parse(currentDir).root;
192
246
  while (true) {
193
- const directCandidate = tryReadPackageJson(path.join(currentDir, "node_modules", packageName, "package.json"));
247
+ const directCandidate = tryReadPackageJson(path$1.join(currentDir, "node_modules", packageName, "package.json"));
194
248
  if (directCandidate?.packageJson.name === packageName) return directCandidate;
195
249
  if (currentDir === rootDir) break;
196
- currentDir = path.dirname(currentDir);
250
+ currentDir = path$1.dirname(currentDir);
197
251
  }
198
252
  return findPackageInPnpmStore(cwd);
199
253
  }
@@ -204,11 +258,11 @@ function getInstalledPackageEntry(pkg, opts) {
204
258
  const cwd = opts?.cwd || getPackageDetectionCwd();
205
259
  const packageName = opts?.packageName || getPackageName(pkg);
206
260
  if (pkg !== packageName && opts?.resolveSubpathWithRequire !== false) try {
207
- return createRequire$1(new URL(`file://${path.join(cwd, "package.json")}`)).resolve(pkg);
261
+ return createRequire(pathToFileURL(path$1.join(cwd, "package.json"))).resolve(pkg);
208
262
  } catch {}
209
263
  const packageJson = installed.packageJson;
210
264
  const explicitEntry = resolveExportsEntry(getPackageExportsTarget(pkg, packageName, packageJson.exports), opts?.conditions) || (typeof packageJson.module === "string" ? packageJson.module : void 0) || (typeof packageJson.main === "string" ? packageJson.main : void 0) || "index.js";
211
- return path.join(installed.dir, explicitEntry);
265
+ return path$1.join(installed.dir, explicitEntry);
212
266
  }
213
267
  /**
214
268
  * Detect whether the current runtime is Vite 8+ by checking for a Vite version flag
@@ -224,7 +278,7 @@ function isNuxtProjectRoot(root) {
224
278
  let dir = root;
225
279
  for (let i = 0; i < 8; i++) {
226
280
  if (hasPackageDependency("nuxt", dir) || hasPackageDependency("nuxt-nightly", dir)) return true;
227
- const parent = path.dirname(dir);
281
+ const parent = path$1.dirname(dir);
228
282
  if (parent === dir) break;
229
283
  dir = parent;
230
284
  }
@@ -235,7 +289,7 @@ function hasPackageDependency(dependencyName, cwd = packageDetectionCwd || proce
235
289
  const cached = dependencyPresenceCache.get(cacheKey);
236
290
  if (cached !== void 0) return cached;
237
291
  try {
238
- const packageJson = JSON.parse(readFileSync(path.join(cwd, "package.json"), "utf8"));
292
+ const packageJson = JSON.parse(readFileSync(path$1.join(cwd, "package.json"), "utf8"));
239
293
  const hasDependency = [
240
294
  packageJson.dependencies,
241
295
  packageJson.devDependencies,
@@ -250,4 +304,4 @@ function hasPackageDependency(dependencyName, cwd = packageDetectionCwd || proce
250
304
  }
251
305
  }
252
306
  //#endregion
253
- export { getPackageName as a, hasPackageDependency as c, packageNameEncode as d, setPackageDetectionCwd as f, __require as g, mfWarn as h, getPackageDetectionCwd as i, isNuxtProjectRoot as l, mfError as m, getInstalledPackageJson as n, getPackageNameFromNodeModulePath as o, createModuleFederationError as p, getIsRolldown as r, getSharedCacheKey as s, getInstalledPackageEntry as t, packageNameDecode as u };
307
+ export { normalizePathForImport as _, getPackageName as a, hasPackageDependency as c, packageNameEncode as d, resolveImportPath as f, mfWarn as g, mfError as h, getPackageDetectionCwd as i, isNuxtProjectRoot as l, createModuleFederationError as m, getInstalledPackageJson as n, getPackageNameFromNodeModulePath as o, setPackageDetectionCwd as p, getIsRolldown as r, getSharedCacheKey as s, getInstalledPackageEntry as t, packageNameDecode as u, rebaseImport as v };
@@ -1,6 +1,6 @@
1
- import { c as hasPackageDependency, g as __require, m as mfError, p as createModuleFederationError } from "./packageUtils-Bbc6r6__.mjs";
1
+ import { _ as normalizePathForImport, c as hasPackageDependency, f as resolveImportPath, h as mfError, m as createModuleFederationError } from "./packageUtils-CYnJFfPP.js";
2
2
  import fs from "fs";
3
- import * as path$1 from "pathe";
3
+ import * as path$1 from "node:path";
4
4
  import { normalizeOptions } from "@module-federation/sdk";
5
5
  import { consumeTypesAPI, generateTypesAPI, isTSProject, normalizeConsumeTypesOptions, normalizeDtsOptions, normalizeGenerateTypesOptions } from "@module-federation/dts-plugin";
6
6
  import { rpc } from "@module-federation/dts-plugin/core";
@@ -14,7 +14,7 @@ const DYNAMIC_HINTS_PLUGIN = "@module-federation/dts-plugin/dynamic-remote-type-
14
14
  const getIPv4 = () => process.env["FEDERATION_IPV4"] || "127.0.0.1";
15
15
  const DEV_TYPES_FOLDER = ".dev-server";
16
16
  const DEFAULT_PUBLIC_TYPES_FOLDER = "@mf-types";
17
- const forkDevWorkerPath = __require.resolve("@module-federation/dts-plugin/dist/fork-dev-worker.js");
17
+ const forkDevWorkerPath = resolveImportPath("@module-federation/dts-plugin/dist/fork-dev-worker.js");
18
18
  var DevWorker = class {
19
19
  worker = rpc.createRpcWorker(forkDevWorkerPath, {}, void 0, false);
20
20
  constructor(options) {
@@ -57,7 +57,7 @@ const buildDtsModuleFederationConfig = (options) => {
57
57
  };
58
58
  const resolveOutputDir = (config) => {
59
59
  const { outDir } = config.build;
60
- if (path$1.isAbsolute(outDir)) return path$1.relative(config.root, outDir);
60
+ if (path$1.isAbsolute(outDir)) return normalizePathForImport(path$1.relative(config.root, outDir));
61
61
  return outDir;
62
62
  };
63
63
  const ensureRuntimePlugin = (options, pluginId) => {
@@ -205,9 +205,9 @@ function transformSsrCode(code, base, sharedPkgMap) {
205
205
  return code;
206
206
  }
207
207
  /**
208
- * Fetch an HTTP ESM module, transform it, write it to a temp .mjs file and
208
+ * Fetch an HTTP ESM module, transform it, write it to a temp .js file and
209
209
  * return the file path. Recursively does the same for HTTP transitive imports
210
- * so that `import('file:///...temp.mjs')` can resolve them.
210
+ * so that `import('file:///...temp.js')` can resolve them.
211
211
  */
212
212
  async function fetchEsmToTempFile(url, tmpDir, visited, sharedPkgMap) {
213
213
  if (visited.has(url)) return visited.get(url);
@@ -229,7 +229,7 @@ async function fetchEsmToTempFile(url, tmpDir, visited, sharedPkgMap) {
229
229
  const { createHash } = await _crypto();
230
230
  const { join } = await _path();
231
231
  const { writeFileSync } = await _fs();
232
- const tmpFile = join(tmpDir, `${createHash("sha1").update(url).digest("hex").slice(0, 12)}.mjs`);
232
+ const tmpFile = join(tmpDir, `${createHash("sha1").update(url).digest("hex").slice(0, 12)}.js`);
233
233
  writeFileSync(tmpFile, code, "utf8");
234
234
  visited.set(url, tmpFile);
235
235
  return tmpFile;
package/package.json CHANGED
@@ -1,30 +1,25 @@
1
1
  {
2
2
  "name": "@module-federation/vite",
3
- "version": "1.16.4",
3
+ "version": "1.16.6",
4
4
  "description": "Vite plugin for Module Federation",
5
5
  "type": "module",
6
- "main": "./lib/index.cjs",
7
- "module": "./lib/index.mjs",
8
- "types": "./lib/index.d.mts",
6
+ "main": "./lib/index.js",
7
+ "module": "./lib/index.js",
8
+ "types": "./lib/index.d.ts",
9
9
  "exports": {
10
10
  ".": {
11
- "types": {
12
- "import": "./lib/index.d.mts",
13
- "require": "./lib/index.d.cts"
14
- },
15
- "import": "./lib/index.mjs",
16
- "require": "./lib/index.cjs"
11
+ "types": "./lib/index.d.ts",
12
+ "default": "./lib/index.js"
17
13
  },
18
14
  "./ssrEntryLoader": {
19
- "types": {
20
- "import": "./lib/utils/ssrEntryLoader.d.mts",
21
- "require": "./lib/utils/ssrEntryLoader.d.cts"
22
- },
23
- "import": "./lib/utils/ssrEntryLoader.mjs",
24
- "require": "./lib/utils/ssrEntryLoader.cjs"
15
+ "types": "./lib/utils/ssrEntryLoader.d.ts",
16
+ "default": "./lib/utils/ssrEntryLoader.js"
25
17
  },
26
18
  "./package.json": "./package.json"
27
19
  },
20
+ "engines": {
21
+ "node": "^20.19.0 || >=22.12.0"
22
+ },
28
23
  "files": [
29
24
  "lib/**/*"
30
25
  ],
@@ -48,9 +43,7 @@
48
43
  "multi-example": "pnpm clean && pnpm --filter 'multi-example-*' --parallel run start",
49
44
  "test": "vitest run --dir src",
50
45
  "test:integration": "vitest run integration",
51
- "e2e": "playwright test",
52
- "changeset": "changeset",
53
- "changeset:status": "changeset status"
46
+ "e2e": "playwright test"
54
47
  },
55
48
  "repository": {
56
49
  "type": "git",
@@ -79,16 +72,13 @@
79
72
  "dependencies": {
80
73
  "@module-federation/dts-plugin": "2.5.1",
81
74
  "@module-federation/runtime": "2.5.1",
82
- "@module-federation/sdk": "2.5.1",
83
- "es-module-lexer": "2.1.0",
84
- "estree-walker": "3.0.3",
85
- "pathe": "2.0.3"
75
+ "@module-federation/sdk": "2.5.1"
86
76
  },
87
77
  "devDependencies": {
88
- "@changesets/cli": "2.30.0",
89
78
  "@playwright/test": "1.58.2",
90
79
  "@types/node": "25.3.3",
91
80
  "husky": "9.1.7",
81
+ "mock-shared-dep": "workspace:*",
92
82
  "oxfmt": "0.36.0",
93
83
  "rollup": "4.47.1",
94
84
  "tsdown": "0.21.0",