@vercel/microfrontends 1.0.1-canary.3 → 1.0.1-canary.4

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 (38) hide show
  1. package/dist/bin/cli.cjs +113 -79
  2. package/dist/config.cjs +3 -0
  3. package/dist/config.cjs.map +1 -1
  4. package/dist/config.js +3 -0
  5. package/dist/config.js.map +1 -1
  6. package/dist/experimental/sveltekit.cjs +79 -72
  7. package/dist/experimental/sveltekit.cjs.map +1 -1
  8. package/dist/experimental/sveltekit.js +79 -72
  9. package/dist/experimental/sveltekit.js.map +1 -1
  10. package/dist/experimental/vite.cjs +82 -75
  11. package/dist/experimental/vite.cjs.map +1 -1
  12. package/dist/experimental/vite.js +82 -75
  13. package/dist/experimental/vite.js.map +1 -1
  14. package/dist/microfrontends/server.cjs +79 -45
  15. package/dist/microfrontends/server.cjs.map +1 -1
  16. package/dist/microfrontends/server.js +79 -45
  17. package/dist/microfrontends/server.js.map +1 -1
  18. package/dist/microfrontends.cjs +3 -0
  19. package/dist/microfrontends.cjs.map +1 -1
  20. package/dist/microfrontends.js +3 -0
  21. package/dist/microfrontends.js.map +1 -1
  22. package/dist/next/config.cjs +79 -72
  23. package/dist/next/config.cjs.map +1 -1
  24. package/dist/next/config.js +79 -72
  25. package/dist/next/config.js.map +1 -1
  26. package/dist/next/middleware.cjs +3 -0
  27. package/dist/next/middleware.cjs.map +1 -1
  28. package/dist/next/middleware.js +3 -0
  29. package/dist/next/middleware.js.map +1 -1
  30. package/dist/next/testing.cjs +3 -0
  31. package/dist/next/testing.cjs.map +1 -1
  32. package/dist/next/testing.js +3 -0
  33. package/dist/next/testing.js.map +1 -1
  34. package/dist/utils/mfe-port.cjs +83 -49
  35. package/dist/utils/mfe-port.cjs.map +1 -1
  36. package/dist/utils/mfe-port.js +83 -49
  37. package/dist/utils/mfe-port.js.map +1 -1
  38. package/package.json +2 -2
@@ -1,10 +1,10 @@
1
1
  // src/utils/mfe-port.ts
2
- import path5 from "node:path";
3
- import fs6 from "node:fs";
2
+ import path6 from "node:path";
3
+ import fs7 from "node:fs";
4
4
 
5
5
  // src/config/microfrontends/server/index.ts
6
- import fs5 from "node:fs";
7
- import { dirname as dirname3, join as join2 } from "node:path";
6
+ import fs6 from "node:fs";
7
+ import { dirname as dirname3 } from "node:path";
8
8
 
9
9
  // src/config/overrides/constants.ts
10
10
  var OVERRIDES_COOKIE_PREFIX = "vercel-micro-frontends-override";
@@ -187,21 +187,21 @@ var MicrofrontendConfigClient = class {
187
187
  isEqual(other) {
188
188
  return JSON.stringify(this.applications) === JSON.stringify(other.applications);
189
189
  }
190
- getApplicationNameForPath(path6) {
191
- if (!path6.startsWith("/")) {
190
+ getApplicationNameForPath(path7) {
191
+ if (!path7.startsWith("/")) {
192
192
  throw new Error(`Path must start with a /`);
193
193
  }
194
- if (this.pathCache[path6]) {
195
- return this.pathCache[path6];
194
+ if (this.pathCache[path7]) {
195
+ return this.pathCache[path7];
196
196
  }
197
- const pathname = new URL(path6, "https://example.com").pathname;
197
+ const pathname = new URL(path7, "https://example.com").pathname;
198
198
  for (const [name, application] of Object.entries(this.applications)) {
199
199
  if (application.routing) {
200
200
  for (const group of application.routing) {
201
201
  for (const childPath of group.paths) {
202
202
  const regexp = pathToRegexp(childPath);
203
203
  if (regexp.test(pathname)) {
204
- this.pathCache[path6] = name;
204
+ this.pathCache[path7] = name;
205
205
  return name;
206
206
  }
207
207
  }
@@ -214,7 +214,7 @@ var MicrofrontendConfigClient = class {
214
214
  if (!defaultApplication) {
215
215
  return null;
216
216
  }
217
- this.pathCache[path6] = defaultApplication[0];
217
+ this.pathCache[path7] = defaultApplication[0];
218
218
  return defaultApplication[0];
219
219
  }
220
220
  serialize() {
@@ -236,18 +236,18 @@ var validateConfigPaths = (applicationConfigsById) => {
236
236
  }
237
237
  const childApp = app;
238
238
  for (const pathMatch of childApp.routing) {
239
- for (const path6 of pathMatch.paths) {
240
- const maybeError = validatePathExpression(path6);
239
+ for (const path7 of pathMatch.paths) {
240
+ const maybeError = validatePathExpression(path7);
241
241
  if (maybeError) {
242
242
  errors.push(maybeError);
243
243
  } else {
244
- const existing = pathsByApplicationId.get(path6);
244
+ const existing = pathsByApplicationId.get(path7);
245
245
  if (existing) {
246
246
  existing.applications.push(id);
247
247
  } else {
248
- pathsByApplicationId.set(path6, {
248
+ pathsByApplicationId.set(path7, {
249
249
  applications: [id],
250
- matcher: pathToRegexp2(path6),
250
+ matcher: pathToRegexp2(path7),
251
251
  applicationId: id
252
252
  });
253
253
  }
@@ -256,24 +256,24 @@ var validateConfigPaths = (applicationConfigsById) => {
256
256
  }
257
257
  }
258
258
  const entries = Array.from(pathsByApplicationId.entries());
259
- for (const [path6, { applications: ids, matcher, applicationId }] of entries) {
259
+ for (const [path7, { applications: ids, matcher, applicationId }] of entries) {
260
260
  if (ids.length > 1) {
261
261
  errors.push(
262
- `Duplicate path "${path6}" for applications "${ids.join(", ")}"`
262
+ `Duplicate path "${path7}" for applications "${ids.join(", ")}"`
263
263
  );
264
264
  }
265
265
  for (const [
266
266
  matchPath,
267
267
  { applications: matchIds, applicationId: matchApplicationId }
268
268
  ] of entries) {
269
- if (path6 === matchPath) {
269
+ if (path7 === matchPath) {
270
270
  continue;
271
271
  }
272
272
  if (applicationId === matchApplicationId) {
273
273
  continue;
274
274
  }
275
275
  if (matcher.test(matchPath)) {
276
- const source = `"${path6}" of application${ids.length > 0 ? "s" : ""} ${ids.join(", ")}`;
276
+ const source = `"${path7}" of application${ids.length > 0 ? "s" : ""} ${ids.join(", ")}`;
277
277
  const destination = `"${matchPath}" of application${matchIds.length > 0 ? "s" : ""} ${matchIds.join(", ")}`;
278
278
  errors.push(
279
279
  `Overlapping path detected between ${source} and ${destination}`
@@ -289,39 +289,42 @@ var validateConfigPaths = (applicationConfigsById) => {
289
289
  }
290
290
  };
291
291
  var PATH_DEFAULT_PATTERN = "[^\\/#\\?]+?";
292
- function validatePathExpression(path6) {
292
+ function validatePathExpression(path7) {
293
293
  try {
294
- const tokens = parsePathRegexp(path6);
295
- if (/(?<!\\)\{/.test(path6)) {
296
- return `Optional paths are not supported: ${path6}`;
294
+ const tokens = parsePathRegexp(path7);
295
+ if (/(?<!\\)\{/.test(path7)) {
296
+ return `Optional paths are not supported: ${path7}`;
297
297
  }
298
- if (/(?<!\\|\()\?/.test(path6)) {
299
- return `Optional paths are not supported: ${path6}`;
298
+ if (/(?<!\\|\()\?/.test(path7)) {
299
+ return `Optional paths are not supported: ${path7}`;
300
300
  }
301
- if (/\/[^/]*(?<!\\):[^/]*(?<!\\):[^/]*/.test(path6)) {
302
- return `Only one wildcard is allowed per path segment: ${path6}`;
301
+ if (/\/[^/]*(?<!\\):[^/]*(?<!\\):[^/]*/.test(path7)) {
302
+ return `Only one wildcard is allowed per path segment: ${path7}`;
303
303
  }
304
304
  for (let i = 0; i < tokens.length; i++) {
305
305
  const token = tokens[i];
306
306
  if (token === void 0) {
307
- return `token ${i} in ${path6} is undefined, this shouldn't happen`;
307
+ return `token ${i} in ${path7} is undefined, this shouldn't happen`;
308
308
  }
309
309
  if (typeof token !== "string") {
310
+ if (!token.name) {
311
+ return `Only named wildcards are allowed: ${path7} (hint: add ":path" to the wildcard)`;
312
+ }
310
313
  if (token.pattern !== PATH_DEFAULT_PATTERN && // Allows (a|b|c) and ((?!a|b|c).*) regex
311
314
  // Only limited regex is supported for now, due to performance considerations
312
315
  !/^(?<allowed>[\w]+(?:\|[^|()]+)+)$|^\(\?!(?<disallowed>[\w]+(?:\|[^|()]+)+)\)\.\*$/.test(
313
316
  token.pattern
314
317
  )) {
315
- return `Path ${path6} cannot use unsupported regular expression wildcard`;
318
+ return `Path ${path7} cannot use unsupported regular expression wildcard`;
316
319
  }
317
320
  if (token.modifier && i !== tokens.length - 1) {
318
- return `Modifier ${token.modifier} is not allowed on wildcard :${token.name} in ${path6}. Modifiers are only allowed in the last path component`;
321
+ return `Modifier ${token.modifier} is not allowed on wildcard :${token.name} in ${path7}. Modifiers are only allowed in the last path component`;
319
322
  }
320
323
  }
321
324
  }
322
325
  } catch (e) {
323
326
  const message = e instanceof Error ? e.message : String(e);
324
- return `Path ${path6} could not be parsed into regexp: ${message}`;
327
+ return `Path ${path7} could not be parsed into regexp: ${message}`;
325
328
  }
326
329
  return void 0;
327
330
  }
@@ -1033,6 +1036,9 @@ function isMonorepo({
1033
1036
  if (fs2.existsSync(path2.join(repositoryRoot, "vlt-workspaces.json"))) {
1034
1037
  return true;
1035
1038
  }
1039
+ if (process.env.NX_WORKSPACE_ROOT === path2.resolve(repositoryRoot)) {
1040
+ return true;
1041
+ }
1036
1042
  const packageJsonPath = path2.join(repositoryRoot, "package.json");
1037
1043
  if (!fs2.existsSync(packageJsonPath)) {
1038
1044
  return false;
@@ -1078,8 +1084,42 @@ function findConfig({ dir }) {
1078
1084
  return null;
1079
1085
  }
1080
1086
 
1081
- // src/config/microfrontends/server/utils/get-output-file-path.ts
1087
+ // src/config/microfrontends/utils/get-application-context.ts
1088
+ import fs5 from "node:fs";
1082
1089
  import path4 from "node:path";
1090
+ function getApplicationContext(opts) {
1091
+ if (opts?.appName) {
1092
+ return { name: opts.appName };
1093
+ }
1094
+ if (process.env.NX_TASK_TARGET_PROJECT) {
1095
+ return { name: process.env.NX_TASK_TARGET_PROJECT };
1096
+ }
1097
+ try {
1098
+ const packageJsonString = fs5.readFileSync(
1099
+ path4.join(opts?.packageRoot || ".", "package.json"),
1100
+ "utf-8"
1101
+ );
1102
+ const packageJson = JSON.parse(packageJsonString);
1103
+ if (!packageJson.name) {
1104
+ throw new MicrofrontendError(
1105
+ `package.json file missing required field "name"`,
1106
+ {
1107
+ type: "packageJson",
1108
+ subtype: "missing_field_name",
1109
+ source: "@vercel/microfrontends/next"
1110
+ }
1111
+ );
1112
+ }
1113
+ return { name: packageJson.name };
1114
+ } catch (err) {
1115
+ throw MicrofrontendError.handle(err, {
1116
+ fileName: "package.json"
1117
+ });
1118
+ }
1119
+ }
1120
+
1121
+ // src/config/microfrontends/server/utils/get-output-file-path.ts
1122
+ import path5 from "node:path";
1083
1123
 
1084
1124
  // src/config/microfrontends/server/constants.ts
1085
1125
  var MFE_CONFIG_DEFAULT_FILE_PATH = "microfrontends";
@@ -1087,7 +1127,7 @@ var MFE_CONFIG_DEFAULT_FILE_NAME = "microfrontends.json";
1087
1127
 
1088
1128
  // src/config/microfrontends/server/utils/get-output-file-path.ts
1089
1129
  function getOutputFilePath() {
1090
- return path4.join(MFE_CONFIG_DEFAULT_FILE_PATH, MFE_CONFIG_DEFAULT_FILE_NAME);
1130
+ return path5.join(MFE_CONFIG_DEFAULT_FILE_PATH, MFE_CONFIG_DEFAULT_FILE_NAME);
1091
1131
  }
1092
1132
 
1093
1133
  // src/config/microfrontends/server/validation.ts
@@ -1426,8 +1466,8 @@ var MicrofrontendsServer = class extends Microfrontends {
1426
1466
  pretty: true
1427
1467
  }) {
1428
1468
  const outputPath = getOutputFilePath();
1429
- fs5.mkdirSync(dirname3(outputPath), { recursive: true });
1430
- fs5.writeFileSync(
1469
+ fs6.mkdirSync(dirname3(outputPath), { recursive: true });
1470
+ fs6.writeFileSync(
1431
1471
  outputPath,
1432
1472
  JSON.stringify(
1433
1473
  this.config.toSchemaJson(),
@@ -1513,14 +1553,8 @@ var MicrofrontendsServer = class extends Microfrontends {
1513
1553
  }
1514
1554
  try {
1515
1555
  const packageRoot = findPackageRoot(directory);
1516
- const packageJsonPath = join2(packageRoot, "package.json");
1517
- const packageJson = JSON.parse(
1518
- fs5.readFileSync(packageJsonPath, "utf-8")
1519
- );
1520
- if (!packageJson.name) {
1521
- throw new Error(`No name found in package.json at ${packageJsonPath}`);
1522
- }
1523
- const configMeta = meta ?? { fromApp: packageJson.name };
1556
+ const { name: appName } = getApplicationContext({ packageRoot });
1557
+ const configMeta = meta ?? { fromApp: appName };
1524
1558
  const maybeConfig = findConfig({ dir: packageRoot });
1525
1559
  if (maybeConfig) {
1526
1560
  return MicrofrontendsServer.fromFile({
@@ -1535,7 +1569,7 @@ var MicrofrontendsServer = class extends Microfrontends {
1535
1569
  if (isMonorepo2) {
1536
1570
  const defaultPackage = findDefaultMicrofrontendsPackage({
1537
1571
  repositoryRoot,
1538
- applicationName: packageJson.name
1572
+ applicationName: appName
1539
1573
  });
1540
1574
  const maybeConfigFromDefault = findConfig({ dir: defaultPackage });
1541
1575
  if (maybeConfigFromDefault) {
@@ -1565,7 +1599,7 @@ var MicrofrontendsServer = class extends Microfrontends {
1565
1599
  options
1566
1600
  }) {
1567
1601
  try {
1568
- const configJson = fs5.readFileSync(filePath, "utf-8");
1602
+ const configJson = fs6.readFileSync(filePath, "utf-8");
1569
1603
  const config = MicrofrontendsServer.validate(configJson);
1570
1604
  if (!isMainConfig(config) && options?.resolveMainConfig) {
1571
1605
  const repositoryRoot = findRepositoryRoot();
@@ -1613,7 +1647,7 @@ var MicrofrontendsServer = class extends Microfrontends {
1613
1647
  overrides
1614
1648
  }) {
1615
1649
  try {
1616
- const config = fs5.readFileSync(filePath, "utf-8");
1650
+ const config = fs6.readFileSync(filePath, "utf-8");
1617
1651
  const validatedConfig = MicrofrontendsServer.validate(config);
1618
1652
  if (!isMainConfig(validatedConfig)) {
1619
1653
  throw new MicrofrontendError(
@@ -1662,8 +1696,8 @@ function mfePort(packageDir) {
1662
1696
  };
1663
1697
  }
1664
1698
  function getPackageJson(packageDir) {
1665
- const filePath = path5.join(packageDir, "package.json");
1666
- return JSON.parse(fs6.readFileSync(filePath, "utf-8"));
1699
+ const filePath = path6.join(packageDir, "package.json");
1700
+ return JSON.parse(fs7.readFileSync(filePath, "utf-8"));
1667
1701
  }
1668
1702
  function loadConfig({
1669
1703
  packageDir,