@nx/webpack 0.0.0-pr-26891-95f6262 → 0.0.0-pr-26482-1bdb7bd

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nx/webpack",
3
- "version": "0.0.0-pr-26891-95f6262",
3
+ "version": "0.0.0-pr-26482-1bdb7bd",
4
4
  "private": false,
5
5
  "description": "The Nx Plugin for Webpack contains executors and generators that support building applications using Webpack.",
6
6
  "repository": {
@@ -33,6 +33,7 @@
33
33
  "@babel/core": "^7.23.2",
34
34
  "@phenomnomnominal/tsquery": "~5.0.1",
35
35
  "@module-federation/sdk": "^0.2.3",
36
+ "@module-federation/enhanced": "^0.2.3",
36
37
  "ajv": "^8.12.0",
37
38
  "autoprefixer": "^10.4.9",
38
39
  "babel-loader": "^9.1.2",
@@ -41,7 +42,9 @@
41
42
  "copy-webpack-plugin": "^10.2.4",
42
43
  "css-loader": "^6.4.0",
43
44
  "css-minimizer-webpack-plugin": "^5.0.0",
45
+ "express": "^4.19.2",
44
46
  "fork-ts-checker-webpack-plugin": "7.2.13",
47
+ "http-proxy-middleware": "^3.0.0",
45
48
  "less": "4.1.3",
46
49
  "less-loader": "11.1.0",
47
50
  "license-webpack-plugin": "^4.0.2",
@@ -66,9 +69,9 @@
66
69
  "webpack-dev-server": "^4.9.3",
67
70
  "webpack-node-externals": "^3.0.0",
68
71
  "webpack-subresource-integrity": "^5.1.0",
69
- "@nx/devkit": "0.0.0-pr-26891-95f6262",
70
- "@nx/js": "0.0.0-pr-26891-95f6262",
71
- "@nrwl/webpack": "0.0.0-pr-26891-95f6262"
72
+ "@nx/devkit": "0.0.0-pr-26482-1bdb7bd",
73
+ "@nx/js": "0.0.0-pr-26482-1bdb7bd",
74
+ "@nrwl/webpack": "0.0.0-pr-26482-1bdb7bd"
72
75
  },
73
76
  "publishConfig": {
74
77
  "access": "public"
@@ -271,6 +271,7 @@ function applyWebConfig(options, config = {}, { useNormalizedEntry, } = {}) {
271
271
  }
272
272
  config.output = {
273
273
  ...config.output,
274
+ assetModuleFilename: '[name].[contenthash:20][ext]',
274
275
  crossOriginLoading: options.subresourceIntegrity
275
276
  ? 'anonymous'
276
277
  : false,
@@ -338,9 +339,6 @@ function applyWebConfig(options, config = {}, { useNormalizedEntry, } = {}) {
338
339
  maxSize: 10_000, // 10 kB
339
340
  },
340
341
  },
341
- generator: {
342
- filename: `[name]${hashFormat.file}[ext]`,
343
- },
344
342
  },
345
343
  // SVG: same as image but we need to separate it so it can be swapped for SVGR in the React plugin.
346
344
  {
@@ -351,17 +349,11 @@ function applyWebConfig(options, config = {}, { useNormalizedEntry, } = {}) {
351
349
  maxSize: 10_000, // 10 kB
352
350
  },
353
351
  },
354
- generator: {
355
- filename: `[name]${hashFormat.file}[ext]`,
356
- },
357
352
  },
358
353
  // Fonts: Emit separate file and export the URL.
359
354
  {
360
355
  test: /\.(eot|otf|ttf|woff|woff2)$/,
361
356
  type: 'asset/resource',
362
- generator: {
363
- filename: `[name]${hashFormat.file}[ext]`,
364
- },
365
357
  },
366
358
  ...rules,
367
359
  ],
@@ -62,5 +62,6 @@ function ensureNxWebpackExecutionContext(ctx) {
62
62
  cwd: process.cwd(),
63
63
  root: devkit_1.workspaceRoot,
64
64
  isVerbose: process.env['NX_VERBOSE_LOGGING'] === 'true',
65
+ projectGraph,
65
66
  };
66
67
  }
@@ -10,6 +10,7 @@ export declare function getRemotes(devRemotes: string[], skipRemotes: string[],
10
10
  devRemotes: any[];
11
11
  dynamicRemotes: any[];
12
12
  remotePorts: any[];
13
+ staticRemotePort: number;
13
14
  };
14
15
  export declare function getModuleFederationConfig(tsconfigPath: string, workspaceRoot: string, projectRoot: string, pluginName?: 'react' | 'angular'): any;
15
16
  export {};
@@ -57,14 +57,19 @@ function getRemotes(devRemotes, skipRemotes, config, context, pathToManifestFile
57
57
  ? (0, find_matching_projects_1.findMatchingProjects)(devRemotes, context.projectGraph.nodes)
58
58
  : (0, find_matching_projects_1.findMatchingProjects)([devRemotes], context.projectGraph.nodes));
59
59
  const staticRemotes = knownRemotes.filter((r) => !devServeApps.has(r));
60
- const devServeRemotes = [...knownRemotes, ...dynamicRemotes].filter((r) => devServeApps.has(r));
60
+ const devServeRemotes = [...knownRemotes, ...knownDynamicRemotes].filter((r) => devServeApps.has(r));
61
61
  const staticDynamicRemotes = knownDynamicRemotes.filter((r) => !devServeApps.has(r));
62
62
  const remotePorts = [...devServeRemotes, ...staticDynamicRemotes].map((r) => context.projectGraph.nodes[r].data.targets['serve'].options.port);
63
+ const staticRemotePort = Math.max(...[
64
+ ...remotePorts,
65
+ ...staticRemotes.map((r) => context.projectGraph.nodes[r].data.targets['serve'].options.port),
66
+ ]) + 1;
63
67
  return {
64
68
  staticRemotes,
65
69
  devRemotes: devServeRemotes,
66
70
  dynamicRemotes: staticDynamicRemotes,
67
71
  remotePorts,
72
+ staticRemotePort,
68
73
  };
69
74
  }
70
75
  function getModuleFederationConfig(tsconfigPath, workspaceRoot, projectRoot, pluginName = 'react') {
@@ -33,6 +33,12 @@ export interface ModuleFederationConfig {
33
33
  exposes?: Record<string, string>;
34
34
  shared?: SharedFunction;
35
35
  additionalShared?: AdditionalSharedConfig;
36
+ /**
37
+ * `nxRuntimeLibraryControlPlugin` is a runtime module federation plugin to ensure
38
+ * that shared libraries are resolved from a remote with live reload capabilities.
39
+ * If you run into any issues with loading shared libraries, try disabling this option.
40
+ */
41
+ disableNxRuntimeLibraryControlPlugin?: boolean;
36
42
  }
37
43
  export type NxModuleFederationConfigOverride = Omit<moduleFederationPlugin.ModuleFederationPluginOptions, 'exposes' | 'remotes' | 'name' | 'library' | 'shared' | 'filename' | 'remoteType'>;
38
44
  export type WorkspaceLibrarySecondaryEntryPoint = {
@@ -0,0 +1,12 @@
1
+ import type { ExecutorContext } from '@nx/devkit';
2
+ export type StaticRemoteConfig = {
3
+ basePath: string;
4
+ outputPath: string;
5
+ urlSegment: string;
6
+ port: number;
7
+ };
8
+ export type StaticRemotesConfig = {
9
+ remotes: string[];
10
+ config: Record<string, StaticRemoteConfig> | undefined;
11
+ };
12
+ export declare function parseStaticRemotesConfig(staticRemotes: string[] | undefined, context: ExecutorContext): StaticRemotesConfig;
@@ -0,0 +1,18 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.parseStaticRemotesConfig = parseStaticRemotesConfig;
4
+ const path_1 = require("path");
5
+ function parseStaticRemotesConfig(staticRemotes, context) {
6
+ if (!staticRemotes?.length) {
7
+ return { remotes: [], config: undefined };
8
+ }
9
+ const config = {};
10
+ for (const app of staticRemotes) {
11
+ const outputPath = context.projectGraph.nodes[app].data.targets['build'].options.outputPath;
12
+ const basePath = (0, path_1.dirname)(outputPath);
13
+ const urlSegment = (0, path_1.basename)(outputPath);
14
+ const port = context.projectGraph.nodes[app].data.targets['serve'].options.port;
15
+ config[app] = { basePath, outputPath, urlSegment, port };
16
+ }
17
+ return { remotes: staticRemotes, config };
18
+ }
@@ -0,0 +1,3 @@
1
+ import type { FederationRuntimePlugin } from '@module-federation/enhanced/runtime';
2
+ declare const nxRuntimeLibraryControlPlugin: () => FederationRuntimePlugin;
3
+ export default nxRuntimeLibraryControlPlugin;
@@ -0,0 +1,54 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const runtimeStore = {
4
+ sharedPackagesFromDev: {},
5
+ };
6
+ if (process.env.NX_MF_DEV_REMOTES) {
7
+ // process.env.NX_MF_DEV_REMOTES is replaced by an array value via DefinePlugin, even though the original value is a stringified array.
8
+ runtimeStore.devRemotes = process.env
9
+ .NX_MF_DEV_REMOTES;
10
+ }
11
+ const nxRuntimeLibraryControlPlugin = function () {
12
+ return {
13
+ name: 'nx-runtime-library-control-plugin',
14
+ beforeInit(args) {
15
+ runtimeStore.name = args.options.name;
16
+ return args;
17
+ },
18
+ resolveShare: (args) => {
19
+ const { shareScopeMap, scope, pkgName, version, GlobalFederation } = args;
20
+ const originalResolver = args.resolver;
21
+ args.resolver = function () {
22
+ if (!runtimeStore.sharedPackagesFromDev[pkgName]) {
23
+ if (!GlobalFederation.__INSTANCES__) {
24
+ return originalResolver();
25
+ }
26
+ else if (!runtimeStore.devRemotes) {
27
+ return originalResolver();
28
+ }
29
+ const devRemoteInstanceToUse = GlobalFederation.__INSTANCES__.find((instance) => instance.options.shared[pkgName] &&
30
+ runtimeStore.devRemotes.find((dr) => instance.name === dr));
31
+ if (!devRemoteInstanceToUse) {
32
+ return originalResolver();
33
+ }
34
+ runtimeStore.sharedPackagesFromDev[pkgName] =
35
+ devRemoteInstanceToUse.name;
36
+ }
37
+ const remoteInstanceName = runtimeStore.sharedPackagesFromDev[pkgName];
38
+ const remoteInstance = GlobalFederation.__INSTANCES__.find((instance) => instance.name === remoteInstanceName);
39
+ try {
40
+ const remotePkgInfo = remoteInstance.options.shared[pkgName].find((shared) => shared.from === remoteInstanceName);
41
+ remotePkgInfo.useIn.push(runtimeStore.name);
42
+ remotePkgInfo.useIn = Array.from(new Set(remotePkgInfo.useIn));
43
+ shareScopeMap[scope][pkgName][version] = remotePkgInfo;
44
+ return remotePkgInfo;
45
+ }
46
+ catch {
47
+ return originalResolver();
48
+ }
49
+ };
50
+ return args;
51
+ },
52
+ };
53
+ };
54
+ exports.default = nxRuntimeLibraryControlPlugin;
@@ -52,7 +52,12 @@ function shareWorkspaceLibraries(workspaceLibs, tsConfigPath = process.env.NX_TS
52
52
  }
53
53
  const webpack = require('webpack');
54
54
  return {
55
- getAliases: () => pathMappings.reduce((aliases, library) => ({ ...aliases, [library.name]: library.path }), {}),
55
+ getAliases: () => pathMappings.reduce((aliases, library) => ({
56
+ ...aliases,
57
+ // If the library path ends in a wildcard, remove it as webpack can't handle this in resolve.alias
58
+ // e.g. path/to/my/lib/* -> path/to/my/lib
59
+ [library.name]: library.path.replace(/\/\*$/, ''),
60
+ }), {}),
56
61
  getLibraries: (projectRoot, eager) => {
57
62
  let pkgJson = null;
58
63
  if (projectRoot &&
@@ -97,7 +102,17 @@ function shareWorkspaceLibraries(workspaceLibs, tsConfigPath = process.env.NX_TS
97
102
  for (const library of pathMappings) {
98
103
  const libFolder = (0, path_1.normalize)((0, path_1.dirname)(library.path));
99
104
  if (!from.startsWith(libFolder) && to.startsWith(libFolder)) {
100
- req.request = library.name;
105
+ const newReq = library.name.endsWith('/*')
106
+ ? /**
107
+ * req usually is in the form of "../../../path/to/file"
108
+ * library.path is usually in the form of "/Users/username/path/to/Workspace/path/to/library"
109
+ *
110
+ * When a wildcard is used in the TS path mappings, we want to get everything after the import to
111
+ * re-route the request correctly inline with the webpack resolve.alias
112
+ */
113
+ (0, path_1.join)(library.name, req.request.split(library.path.replace(devkit_1.workspaceRoot, '').replace('/*', ''))[1])
114
+ : library.name;
115
+ req.request = newReq;
101
116
  }
102
117
  }
103
118
  }),
@@ -0,0 +1,2 @@
1
+ import { StaticRemotesConfig } from './parse-static-remotes-config';
2
+ export declare function startRemoteProxies(staticRemotesConfig: StaticRemotesConfig, mappedLocationsOfRemotes: Record<string, string>): void;
@@ -0,0 +1,20 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.startRemoteProxies = startRemoteProxies;
4
+ const devkit_1 = require("@nx/devkit");
5
+ function startRemoteProxies(staticRemotesConfig, mappedLocationsOfRemotes) {
6
+ const { createProxyMiddleware } = require('http-proxy-middleware');
7
+ const express = require('express');
8
+ devkit_1.logger.info(`NX Starting static remotes proxies...`);
9
+ for (const app of staticRemotesConfig.remotes) {
10
+ const expressProxy = express();
11
+ expressProxy.use(createProxyMiddleware({
12
+ target: mappedLocationsOfRemotes[app],
13
+ changeOrigin: true,
14
+ }));
15
+ const proxyServer = expressProxy.listen(staticRemotesConfig.config[app].port);
16
+ process.on('SIGTERM', () => proxyServer.close());
17
+ process.on('exit', () => proxyServer.close());
18
+ }
19
+ devkit_1.logger.info(`NX Static remotes proxies started successfully`);
20
+ }