@nx/webpack 19.4.0-beta.1 → 19.4.0-beta.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 (39) hide show
  1. package/generators.json +11 -1
  2. package/index.d.ts +3 -1
  3. package/index.js +5 -1
  4. package/package.json +5 -4
  5. package/plugin.d.ts +1 -1
  6. package/plugin.js +2 -1
  7. package/src/executors/dev-server/dev-server.impl.js +7 -1
  8. package/src/executors/webpack/webpack.impl.js +5 -0
  9. package/src/generators/convert-config-to-webpack-plugin/convert-config-to-webpack-plugin.d.ts +7 -0
  10. package/src/generators/convert-config-to-webpack-plugin/convert-config-to-webpack-plugin.js +93 -0
  11. package/src/generators/convert-config-to-webpack-plugin/lib/extract-webpack-options.d.ts +6 -0
  12. package/src/generators/convert-config-to-webpack-plugin/lib/extract-webpack-options.js +106 -0
  13. package/src/generators/convert-config-to-webpack-plugin/lib/normalize-path-options.d.ts +2 -0
  14. package/src/generators/convert-config-to-webpack-plugin/lib/normalize-path-options.js +75 -0
  15. package/src/generators/convert-config-to-webpack-plugin/lib/utils.d.ts +1 -0
  16. package/src/generators/convert-config-to-webpack-plugin/lib/utils.js +14 -0
  17. package/src/generators/convert-config-to-webpack-plugin/lib/validate-project.d.ts +9 -0
  18. package/src/generators/convert-config-to-webpack-plugin/lib/validate-project.js +44 -0
  19. package/src/generators/convert-config-to-webpack-plugin/schema.json +19 -0
  20. package/src/generators/convert-to-inferred/convert-to-inferred.d.ts +7 -0
  21. package/src/generators/convert-to-inferred/convert-to-inferred.js +71 -0
  22. package/src/generators/convert-to-inferred/schema.json +19 -0
  23. package/src/generators/convert-to-inferred/utils/ast.d.ts +3 -0
  24. package/src/generators/convert-to-inferred/utils/ast.js +40 -0
  25. package/src/generators/convert-to-inferred/utils/build-post-target-transformer.d.ts +6 -0
  26. package/src/generators/convert-to-inferred/utils/build-post-target-transformer.js +219 -0
  27. package/src/generators/convert-to-inferred/utils/index.d.ts +3 -0
  28. package/src/generators/convert-to-inferred/utils/index.js +6 -0
  29. package/src/generators/convert-to-inferred/utils/serve-post-target-transformer.d.ts +6 -0
  30. package/src/generators/convert-to-inferred/utils/serve-post-target-transformer.js +250 -0
  31. package/src/generators/convert-to-inferred/utils/types.d.ts +11 -0
  32. package/src/generators/convert-to-inferred/utils/types.js +2 -0
  33. package/src/plugins/nx-webpack-plugin/lib/apply-base-config.js +5 -1
  34. package/src/plugins/nx-webpack-plugin/lib/normalize-options.js +1 -1
  35. package/src/plugins/plugin.d.ts +5 -1
  36. package/src/plugins/plugin.js +62 -41
  37. package/src/plugins/use-legacy-nx-plugin/use-legacy-nx-plugin.d.ts +29 -0
  38. package/src/plugins/use-legacy-nx-plugin/use-legacy-nx-plugin.js +59 -0
  39. package/src/utils/module-federation/get-remotes-for-host.js +1 -1
@@ -1,61 +1,74 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.createNodes = exports.createDependencies = void 0;
3
+ exports.createNodes = exports.createNodesV2 = exports.createDependencies = void 0;
4
4
  const devkit_1 = require("@nx/devkit");
5
- const path_1 = require("path");
5
+ const calculate_hash_for_create_nodes_1 = require("@nx/devkit/src/utils/calculate-hash-for-create-nodes");
6
6
  const get_named_inputs_1 = require("@nx/devkit/src/utils/get-named-inputs");
7
+ const js_1 = require("@nx/js");
7
8
  const fs_1 = require("fs");
9
+ const file_hasher_1 = require("nx/src/hasher/file-hasher");
10
+ const cache_directory_1 = require("nx/src/utils/cache-directory");
11
+ const path_1 = require("path");
8
12
  const read_webpack_options_1 = require("../utils/webpack/read-webpack-options");
9
13
  const resolve_user_defined_webpack_config_1 = require("../utils/webpack/resolve-user-defined-webpack-config");
10
- const js_1 = require("@nx/js");
11
- const cache_directory_1 = require("nx/src/utils/cache-directory");
12
- const calculate_hash_for_create_nodes_1 = require("@nx/devkit/src/utils/calculate-hash-for-create-nodes");
13
- const cachePath = (0, path_1.join)(cache_directory_1.workspaceDataDirectory, 'webpack.hash');
14
- const targetsCache = readTargetsCache();
15
- function readTargetsCache() {
14
+ function readTargetsCache(cachePath) {
16
15
  return (0, fs_1.existsSync)(cachePath) ? (0, devkit_1.readJsonFile)(cachePath) : {};
17
16
  }
18
- function writeTargetsToCache() {
19
- const oldCache = readTargetsCache();
20
- (0, devkit_1.writeJsonFile)(cachePath, {
21
- ...oldCache,
22
- ...targetsCache,
23
- });
17
+ function writeTargetsToCache(cachePath, results) {
18
+ (0, devkit_1.writeJsonFile)(cachePath, results);
24
19
  }
20
+ /**
21
+ * @deprecated The 'createDependencies' function is now a no-op. This functionality is included in 'createNodesV2'.
22
+ */
25
23
  const createDependencies = () => {
26
- writeTargetsToCache();
27
24
  return [];
28
25
  };
29
26
  exports.createDependencies = createDependencies;
27
+ const webpackConfigGlob = '**/webpack.config.{js,ts,mjs,cjs}';
28
+ exports.createNodesV2 = [
29
+ webpackConfigGlob,
30
+ async (configFilePaths, options, context) => {
31
+ const optionsHash = (0, file_hasher_1.hashObject)(options);
32
+ const cachePath = (0, path_1.join)(cache_directory_1.workspaceDataDirectory, `webpack-${optionsHash}.hash`);
33
+ const targetsCache = readTargetsCache(cachePath);
34
+ const normalizedOptions = normalizeOptions(options);
35
+ try {
36
+ return await (0, devkit_1.createNodesFromFiles)((configFile, options, context) => createNodesInternal(configFile, options, context, targetsCache), configFilePaths, normalizedOptions, context);
37
+ }
38
+ finally {
39
+ writeTargetsToCache(cachePath, targetsCache);
40
+ }
41
+ },
42
+ ];
30
43
  exports.createNodes = [
31
- '**/webpack.config.{js,ts,mjs,cjs}',
44
+ webpackConfigGlob,
32
45
  async (configFilePath, options, context) => {
33
- options ??= {};
34
- options.buildTargetName ??= 'build';
35
- options.serveTargetName ??= 'serve';
36
- options.serveStaticTargetName ??= 'serve-static';
37
- options.previewTargetName ??= 'preview';
38
- const projectRoot = (0, path_1.dirname)(configFilePath);
39
- // Do not create a project if package.json and project.json isn't there.
40
- const siblingFiles = (0, fs_1.readdirSync)((0, path_1.join)(context.workspaceRoot, projectRoot));
41
- if (!siblingFiles.includes('package.json') &&
42
- !siblingFiles.includes('project.json')) {
43
- return {};
44
- }
45
- const hash = await (0, calculate_hash_for_create_nodes_1.calculateHashForCreateNodes)(projectRoot, options, context, [(0, js_1.getLockFileName)((0, devkit_1.detectPackageManager)(context.workspaceRoot))]);
46
- const targets = targetsCache[hash]
47
- ? targetsCache[hash]
48
- : await createWebpackTargets(configFilePath, projectRoot, options, context);
49
- return {
50
- projects: {
51
- [projectRoot]: {
52
- projectType: 'application',
53
- targets,
54
- },
55
- },
56
- };
46
+ devkit_1.logger.warn('`createNodes` is deprecated. Update your plugin to utilize createNodesV2 instead. In Nx 20, this will change to the createNodesV2 API.');
47
+ const normalizedOptions = normalizeOptions(options);
48
+ return createNodesInternal(configFilePath, normalizedOptions, context, {});
57
49
  },
58
50
  ];
51
+ async function createNodesInternal(configFilePath, options, context, targetsCache) {
52
+ const projectRoot = (0, path_1.dirname)(configFilePath);
53
+ // Do not create a project if package.json and project.json isn't there.
54
+ const siblingFiles = (0, fs_1.readdirSync)((0, path_1.join)(context.workspaceRoot, projectRoot));
55
+ if (!siblingFiles.includes('package.json') &&
56
+ !siblingFiles.includes('project.json')) {
57
+ return {};
58
+ }
59
+ const hash = await (0, calculate_hash_for_create_nodes_1.calculateHashForCreateNodes)(projectRoot, options, context, [(0, js_1.getLockFileName)((0, devkit_1.detectPackageManager)(context.workspaceRoot))]);
60
+ targetsCache[hash] ??= await createWebpackTargets(configFilePath, projectRoot, options, context);
61
+ const { targets, metadata } = targetsCache[hash];
62
+ return {
63
+ projects: {
64
+ [projectRoot]: {
65
+ projectType: 'application',
66
+ targets,
67
+ metadata,
68
+ },
69
+ },
70
+ };
71
+ }
59
72
  async function createWebpackTargets(configFilePath, projectRoot, options, context) {
60
73
  const namedInputs = (0, get_named_inputs_1.getNamedInputs)(projectRoot, context);
61
74
  const webpackConfig = (0, resolve_user_defined_webpack_config_1.resolveUserDefinedWebpackConfig)((0, path_1.join)(context.workspaceRoot, configFilePath), (0, js_1.getRootTsConfigPath)(), true);
@@ -105,7 +118,7 @@ async function createWebpackTargets(configFilePath, projectRoot, options, contex
105
118
  spa: true,
106
119
  },
107
120
  };
108
- return targets;
121
+ return { targets, metadata: {} };
109
122
  }
110
123
  function normalizeOutputPath(outputPath, projectRoot) {
111
124
  if (!outputPath) {
@@ -135,3 +148,11 @@ function normalizeOutputPath(outputPath, projectRoot) {
135
148
  }
136
149
  }
137
150
  }
151
+ function normalizeOptions(options) {
152
+ return {
153
+ buildTargetName: options?.buildTargetName ?? 'build',
154
+ serveTargetName: options?.serveTargetName ?? 'serve',
155
+ serveStaticTargetName: options?.serveStaticTargetName ?? 'serve-static',
156
+ previewTargetName: options?.previewTargetName ?? 'preview',
157
+ };
158
+ }
@@ -0,0 +1,29 @@
1
+ import { NxWebpackExecutionContext } from '../../utils/config';
2
+ import { NxAppWebpackPluginOptions } from '../nx-webpack-plugin/nx-app-webpack-plugin-options';
3
+ import { Configuration } from 'webpack';
4
+ /**
5
+ * This function is used to wrap the legacy plugin function to be used with the `composePlugins` function.
6
+ * Initially the webpack config would be passed to the legacy plugin function and the options would be passed as a second argument.
7
+ * example:
8
+ * module.exports = composePlugins(
9
+ withNx(),
10
+ (config) => {
11
+ return config;
12
+ }
13
+ );
14
+
15
+ Since composePlugins is async, this function is used to wrap the legacy plugin function to be async.
16
+ Using the nxUseLegacyPlugin function, the first argument is the legacy plugin function and the second argument is the options.
17
+ The context options are created and passed to the legacy plugin function.
18
+
19
+ module.exports = async () => ({
20
+ plugins: [
21
+ ...otherPlugins,
22
+ await nxUseLegacyPlugin(require({path}), options),
23
+ ],
24
+ });
25
+ * @param fn The legacy plugin function usually from `combinedPlugins`
26
+ * @param executorOptions The options passed usually inside the executor or the config file
27
+ * @returns Webpack configuration
28
+ */
29
+ export declare function useLegacyNxPlugin(fn: (config: Configuration, ctx: NxWebpackExecutionContext) => Promise<Configuration>, executorOptions: NxAppWebpackPluginOptions): Promise<(config: Configuration) => Promise<Configuration>>;
@@ -0,0 +1,59 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.useLegacyNxPlugin = void 0;
4
+ const devkit_1 = require("@nx/devkit");
5
+ const normalize_options_1 = require("../nx-webpack-plugin/lib/normalize-options");
6
+ /**
7
+ * This function is used to wrap the legacy plugin function to be used with the `composePlugins` function.
8
+ * Initially the webpack config would be passed to the legacy plugin function and the options would be passed as a second argument.
9
+ * example:
10
+ * module.exports = composePlugins(
11
+ withNx(),
12
+ (config) => {
13
+ return config;
14
+ }
15
+ );
16
+
17
+ Since composePlugins is async, this function is used to wrap the legacy plugin function to be async.
18
+ Using the nxUseLegacyPlugin function, the first argument is the legacy plugin function and the second argument is the options.
19
+ The context options are created and passed to the legacy plugin function.
20
+
21
+ module.exports = async () => ({
22
+ plugins: [
23
+ ...otherPlugins,
24
+ await nxUseLegacyPlugin(require({path}), options),
25
+ ],
26
+ });
27
+ * @param fn The legacy plugin function usually from `combinedPlugins`
28
+ * @param executorOptions The options passed usually inside the executor or the config file
29
+ * @returns Webpack configuration
30
+ */
31
+ async function useLegacyNxPlugin(fn, executorOptions) {
32
+ if (global.NX_GRAPH_CREATION) {
33
+ return;
34
+ }
35
+ const options = (0, normalize_options_1.normalizeOptions)(executorOptions);
36
+ const projectGraph = (0, devkit_1.readCachedProjectGraph)();
37
+ const projectName = process.env.NX_TASK_TARGET_PROJECT;
38
+ const project = projectGraph.nodes[projectName];
39
+ const targetName = process.env.NX_TASK_TARGET_TARGET;
40
+ const context = {
41
+ cwd: process.cwd(),
42
+ isVerbose: process.env.NX_VERBOSE_LOGGING === 'true',
43
+ root: project.data.root,
44
+ projectGraph: (0, devkit_1.readCachedProjectGraph)(),
45
+ target: project.data.targets[targetName],
46
+ targetName: targetName,
47
+ projectName: projectName,
48
+ };
49
+ const configuration = process.env.NX_TASK_TARGET_CONFIGURATION;
50
+ return async (config) => {
51
+ const ctx = {
52
+ context,
53
+ options: options,
54
+ configuration,
55
+ };
56
+ return await fn(config, ctx);
57
+ };
58
+ }
59
+ exports.useLegacyNxPlugin = useLegacyNxPlugin;
@@ -48,7 +48,7 @@ function getRemotes(devRemotes, skipRemotes, config, context, pathToManifestFile
48
48
  devkit_1.logger.info(`Remotes not served automatically: ${[...remotesToSkip.values()].join(', ')}`);
49
49
  }
50
50
  const knownRemotes = Array.from(collectedRemotes).filter((r) => !remotesToSkip.has(r));
51
- const knownDynamicRemotes = dynamicRemotes.filter((r) => !remotesToSkip.has(r));
51
+ const knownDynamicRemotes = dynamicRemotes.filter((r) => !remotesToSkip.has(r) && context.projectGraph.nodes[r]);
52
52
  devkit_1.logger.info(`NX Starting module federation dev-server for ${chalk.bold(context.projectName)} with ${[...knownRemotes, ...knownDynamicRemotes].length} remotes`);
53
53
  const devServeApps = new Set(!devRemotes
54
54
  ? []