@vercel/microfrontends 1.2.0 → 1.2.2

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 (50) hide show
  1. package/dist/bin/cli.cjs +67 -21
  2. package/dist/config.cjs +3 -1
  3. package/dist/config.cjs.map +1 -1
  4. package/dist/config.js +3 -1
  5. package/dist/config.js.map +1 -1
  6. package/dist/experimental/sveltekit.cjs +35 -18
  7. package/dist/experimental/sveltekit.cjs.map +1 -1
  8. package/dist/experimental/sveltekit.js +35 -18
  9. package/dist/experimental/sveltekit.js.map +1 -1
  10. package/dist/experimental/vite.cjs +35 -18
  11. package/dist/experimental/vite.cjs.map +1 -1
  12. package/dist/experimental/vite.d.ts +3 -2
  13. package/dist/experimental/vite.js +35 -18
  14. package/dist/experimental/vite.js.map +1 -1
  15. package/dist/microfrontends/server.cjs +35 -18
  16. package/dist/microfrontends/server.cjs.map +1 -1
  17. package/dist/microfrontends/server.js +35 -18
  18. package/dist/microfrontends/server.js.map +1 -1
  19. package/dist/microfrontends/utils.cjs +176 -2
  20. package/dist/microfrontends/utils.cjs.map +1 -1
  21. package/dist/microfrontends/utils.d.ts +11 -1
  22. package/dist/microfrontends/utils.js +174 -1
  23. package/dist/microfrontends/utils.js.map +1 -1
  24. package/dist/next/client.cjs +1 -1
  25. package/dist/next/client.cjs.map +1 -1
  26. package/dist/next/client.js +1 -1
  27. package/dist/next/client.js.map +1 -1
  28. package/dist/next/config.cjs +35 -18
  29. package/dist/next/config.cjs.map +1 -1
  30. package/dist/next/config.d.ts +16 -2
  31. package/dist/next/config.js +35 -18
  32. package/dist/next/config.js.map +1 -1
  33. package/dist/next/endpoints.cjs +3 -1
  34. package/dist/next/endpoints.cjs.map +1 -1
  35. package/dist/next/endpoints.js +3 -1
  36. package/dist/next/endpoints.js.map +1 -1
  37. package/dist/next/middleware.cjs +7 -3
  38. package/dist/next/middleware.cjs.map +1 -1
  39. package/dist/next/middleware.js +7 -3
  40. package/dist/next/middleware.js.map +1 -1
  41. package/dist/next/testing.cjs +3 -1
  42. package/dist/next/testing.cjs.map +1 -1
  43. package/dist/next/testing.d.ts +28 -0
  44. package/dist/next/testing.js +3 -1
  45. package/dist/next/testing.js.map +1 -1
  46. package/dist/utils/mfe-port.cjs +35 -18
  47. package/dist/utils/mfe-port.cjs.map +1 -1
  48. package/dist/utils/mfe-port.js +35 -18
  49. package/dist/utils/mfe-port.js.map +1 -1
  50. package/package.json +1 -1
@@ -20,8 +20,22 @@ interface WithMicrofrontendsOptions {
20
20
  }
21
21
 
22
22
  /**
23
- * A function that wraps the next.config.js file to automatically configure zones
24
- * based on the microfrontends configuration.
23
+ * Automatically configures your Next.js application to work with microfrontends.
24
+ *
25
+ * This function should wrap your Next.js config object before it is exported. It
26
+ * will automatically set up the necessary fields and environment variables for
27
+ * microfrontends to work.
28
+ *
29
+ * See the [Getting Started](https://vercel.com/docs/microfrontends/quickstart) guide for more information.
30
+ *
31
+ * @example Wrapping your Next.js config
32
+ * ```js
33
+ * import { withMicrofrontends } from '@vercel/microfrontends/next/config';
34
+ *
35
+ * const nextConfig = { ... };
36
+ *
37
+ * export default withMicrofrontends(nextConfig);
38
+ * ```
25
39
  */
26
40
  declare function withMicrofrontends(nextConfig: NextConfig, opts?: WithMicrofrontendsOptions): NextConfig;
27
41
 
@@ -176,11 +176,11 @@ function findRepositoryRoot(startDir) {
176
176
  currentDir = path.dirname(currentDir);
177
177
  }
178
178
  throw new Error(
179
- "Repository root not found. Specify the root of the repository with the `repository.root` option."
179
+ `Could not find the root of the repository for ${startDir}. Please ensure that the directory is part of a Git repository. If you suspect that this should work, please file an issue to the Vercel team.`
180
180
  );
181
181
  }
182
182
 
183
- // src/config/microfrontends/utils/find-default-package.ts
183
+ // src/config/microfrontends/utils/infer-microfrontends-location.ts
184
184
  import { dirname } from "node:path";
185
185
  import { readFileSync } from "node:fs";
186
186
  import { parse } from "jsonc-parser";
@@ -192,9 +192,9 @@ var CONFIGURATION_FILENAMES = [
192
192
  "microfrontends.json"
193
193
  ];
194
194
 
195
- // src/config/microfrontends/utils/find-default-package.ts
195
+ // src/config/microfrontends/utils/infer-microfrontends-location.ts
196
196
  var configCache = {};
197
- function findDefaultMicrofrontendsPackages({
197
+ function findPackageWithMicrofrontendsConfig({
198
198
  repositoryRoot,
199
199
  applicationName
200
200
  }) {
@@ -219,19 +219,29 @@ function findDefaultMicrofrontendsPackages({
219
219
  const microfrontendsJson = parse(microfrontendsJsonContent);
220
220
  if (microfrontendsJson.applications[applicationName]) {
221
221
  matchingPaths.push(microfrontendsJsonPath);
222
+ } else {
223
+ for (const [_, app] of Object.entries(
224
+ microfrontendsJson.applications
225
+ )) {
226
+ if (app.packageName === applicationName) {
227
+ matchingPaths.push(microfrontendsJsonPath);
228
+ }
229
+ }
222
230
  }
223
231
  } catch (error) {
224
232
  }
225
233
  }
226
234
  if (matchingPaths.length > 1) {
227
- throw new Error(
228
- `Found multiple default applications referencing "${applicationName}" in the repository, but only one is allowed.
229
- ${matchingPaths.join("\n \u2022 ")}`
235
+ throw new MicrofrontendError(
236
+ `Found multiple \`microfrontends.json\` files in the repository referencing the application "${applicationName}", but only one is allowed.
237
+ ${matchingPaths.join("\n \u2022 ")}`,
238
+ { type: "config", subtype: "inference_failed" }
230
239
  );
231
240
  }
232
241
  if (matchingPaths.length === 0) {
233
- throw new Error(
234
- `Could not find default application with "applications.${applicationName}"`
242
+ throw new MicrofrontendError(
243
+ `Could not find a \`microfrontends.json\` file in the repository that contains "applications.${applicationName}". Microfrontends defined in separate repositories are not supported yet.`,
244
+ { type: "config", subtype: "inference_failed" }
235
245
  );
236
246
  }
237
247
  const [packageJsonPath] = matchingPaths;
@@ -240,15 +250,16 @@ ${matchingPaths.join("\n \u2022 ")}`
240
250
  return null;
241
251
  }
242
252
  }
243
- function findDefaultMicrofrontendsPackage(opts) {
253
+ function inferMicrofrontendsLocation(opts) {
244
254
  const cacheKey = `${opts.repositoryRoot}-${opts.applicationName}`;
245
255
  if (configCache[cacheKey]) {
246
256
  return configCache[cacheKey];
247
257
  }
248
- const result = findDefaultMicrofrontendsPackages(opts);
258
+ const result = findPackageWithMicrofrontendsConfig(opts);
249
259
  if (!result) {
250
- throw new Error(
251
- "Error trying to resolve the main microfrontends configuration"
260
+ throw new MicrofrontendError(
261
+ `Could not infer the location of the \`microfrontends.json\` file for application "${opts.applicationName}" starting in directory "${opts.repositoryRoot}".`,
262
+ { type: "config", subtype: "inference_failed" }
252
263
  );
253
264
  }
254
265
  configCache[cacheKey] = result;
@@ -299,7 +310,7 @@ function findPackageRoot(startDir) {
299
310
  currentDir = path3.dirname(currentDir);
300
311
  }
301
312
  throw new Error(
302
- "Package root not found. Specify the root of the package with the `package.root` option."
313
+ `The root of the package that contains the \`package.json\` file for the \`${startDir}\` directory could not be found.`
303
314
  );
304
315
  }
305
316
 
@@ -340,7 +351,9 @@ var MicrofrontendConfigClient = class {
340
351
  */
341
352
  static fromEnv(config, opts) {
342
353
  if (!config) {
343
- throw new Error("No microfrontends configuration found");
354
+ throw new Error(
355
+ "Could not construct MicrofrontendConfigClient: configuration is empty or undefined. Did you set up your application with `withMicrofrontends`?"
356
+ );
344
357
  }
345
358
  return new MicrofrontendConfigClient(
346
359
  JSON.parse(config),
@@ -1362,7 +1375,7 @@ var MicrofrontendsServer = class {
1362
1375
  const repositoryRoot = findRepositoryRoot();
1363
1376
  const isMonorepo2 = isMonorepo({ repositoryRoot });
1364
1377
  if (isMonorepo2) {
1365
- const defaultPackage = findDefaultMicrofrontendsPackage({
1378
+ const defaultPackage = inferMicrofrontendsLocation({
1366
1379
  repositoryRoot,
1367
1380
  applicationName: appName
1368
1381
  });
@@ -1374,13 +1387,17 @@ var MicrofrontendsServer = class {
1374
1387
  });
1375
1388
  }
1376
1389
  }
1377
- throw new Error("Unable to infer");
1390
+ throw new MicrofrontendError(
1391
+ "Unable to automatically infer the location of the `microfrontends.json` file. Microfrontends defined in separate repositories are not supported yet. If you suspect this is thrown in error, please reach out to the Vercel team.",
1392
+ { type: "config", subtype: "inference_failed" }
1393
+ );
1378
1394
  } catch (e) {
1379
1395
  if (e instanceof MicrofrontendError) {
1380
1396
  throw e;
1381
1397
  }
1398
+ const errorMessage = e instanceof Error ? e.message : String(e);
1382
1399
  throw new MicrofrontendError(
1383
- "Unable to locate and parse microfrontends configuration",
1400
+ `Unable to locate and parse the \`microfrontends.json\` configuration file. Original error message: ${errorMessage}`,
1384
1401
  { cause: e, type: "config", subtype: "inference_failed" }
1385
1402
  );
1386
1403
  }