@nx/webpack 17.0.4 → 17.0.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.
Files changed (80) hide show
  1. package/LICENSE +1 -1
  2. package/README.md +4 -9
  3. package/generators.json +2 -2
  4. package/index.d.ts +1 -3
  5. package/index.js +2 -7
  6. package/migrations.json +0 -6
  7. package/package.json +5 -6
  8. package/src/executors/dev-server/dev-server.impl.js +17 -31
  9. package/src/executors/dev-server/lib/get-dev-server-config.d.ts +3 -2
  10. package/src/executors/dev-server/lib/get-dev-server-config.js +26 -18
  11. package/src/executors/dev-server/schema.d.ts +8 -8
  12. package/src/executors/webpack/lib/get-webpack-config.d.ts +5 -0
  13. package/src/executors/webpack/lib/get-webpack-config.js +16 -0
  14. package/src/executors/webpack/lib/normalize-options.d.ts +2 -1
  15. package/src/executors/webpack/lib/normalize-options.js +53 -9
  16. package/src/executors/webpack/schema.d.ts +7 -10
  17. package/src/executors/webpack/schema.json +50 -30
  18. package/src/executors/webpack/webpack.impl.js +16 -36
  19. package/src/generators/configuration/configuration.d.ts +2 -3
  20. package/src/generators/configuration/configuration.js +23 -95
  21. package/src/generators/configuration/schema.d.ts +0 -1
  22. package/src/generators/configuration/schema.json +1 -1
  23. package/src/generators/init/init.d.ts +0 -1
  24. package/src/generators/init/init.js +23 -48
  25. package/src/generators/init/schema.d.ts +2 -4
  26. package/src/generators/init/schema.json +13 -18
  27. package/src/plugins/generate-package-json-plugin.d.ts +4 -6
  28. package/src/plugins/generate-package-json-plugin.js +14 -11
  29. package/src/utils/config.d.ts +6 -11
  30. package/src/utils/config.js +13 -48
  31. package/src/utils/get-client-environment.js +1 -4
  32. package/src/utils/module-federation/dependencies.js +3 -15
  33. package/src/utils/module-federation/get-remotes-for-host.js +1 -1
  34. package/src/utils/module-federation/remotes.js +1 -1
  35. package/src/utils/module-federation/secondary-entry-points.js +1 -1
  36. package/src/utils/module-federation/typescript.js +9 -14
  37. package/src/utils/versions.d.ts +0 -1
  38. package/src/utils/versions.js +1 -2
  39. package/src/utils/webpack/custom-webpack.d.ts +2 -0
  40. package/src/utils/webpack/{resolve-user-defined-webpack-config.js → custom-webpack.js} +8 -14
  41. package/src/utils/webpack/deprecated-stylus-loader.js +2 -2
  42. package/src/utils/webpack/interpolate-env-variables-to-index.js +1 -4
  43. package/src/utils/webpack/plugins/postcss-cli-resources.js +2 -2
  44. package/src/utils/with-nx.d.ts +58 -4
  45. package/src/utils/with-nx.js +362 -22
  46. package/src/utils/with-web.d.ts +2 -2
  47. package/src/utils/with-web.js +488 -9
  48. package/plugin.d.ts +0 -1
  49. package/plugin.js +0 -5
  50. package/src/migrations/update-17-2-1/webpack-config-setup.d.ts +0 -2
  51. package/src/migrations/update-17-2-1/webpack-config-setup.js +0 -31
  52. package/src/plugins/nx-typescript-webpack-plugin/nx-tsconfig-paths-webpack-plugin.d.ts +0 -8
  53. package/src/plugins/nx-typescript-webpack-plugin/nx-tsconfig-paths-webpack-plugin.js +0 -31
  54. package/src/plugins/nx-webpack-plugin/lib/apply-base-config.d.ts +0 -5
  55. package/src/plugins/nx-webpack-plugin/lib/apply-base-config.js +0 -318
  56. package/src/plugins/nx-webpack-plugin/lib/apply-web-config.d.ts +0 -5
  57. package/src/plugins/nx-webpack-plugin/lib/apply-web-config.js +0 -372
  58. package/src/plugins/nx-webpack-plugin/lib/compiler-loaders.d.ts +0 -53
  59. package/src/plugins/nx-webpack-plugin/lib/compiler-loaders.js +0 -78
  60. package/src/plugins/nx-webpack-plugin/lib/get-terser-ecma-version.d.ts +0 -1
  61. package/src/plugins/nx-webpack-plugin/lib/get-terser-ecma-version.js +0 -35
  62. package/src/plugins/nx-webpack-plugin/lib/instantiate-script-plugins.d.ts +0 -3
  63. package/src/plugins/nx-webpack-plugin/lib/instantiate-script-plugins.js +0 -42
  64. package/src/plugins/nx-webpack-plugin/lib/normalize-options.d.ts +0 -4
  65. package/src/plugins/nx-webpack-plugin/lib/normalize-options.js +0 -148
  66. package/src/plugins/nx-webpack-plugin/lib/stylesheet-loaders.d.ts +0 -73
  67. package/src/plugins/nx-webpack-plugin/lib/stylesheet-loaders.js +0 -117
  68. package/src/plugins/nx-webpack-plugin/nx-webpack-plugin-options.d.ts +0 -216
  69. package/src/plugins/nx-webpack-plugin/nx-webpack-plugin-options.js +0 -2
  70. package/src/plugins/nx-webpack-plugin/nx-webpack-plugin.d.ts +0 -17
  71. package/src/plugins/nx-webpack-plugin/nx-webpack-plugin.js +0 -48
  72. package/src/plugins/plugin.d.ts +0 -9
  73. package/src/plugins/plugin.js +0 -132
  74. package/src/utils/ensure-dependencies.d.ts +0 -6
  75. package/src/utils/ensure-dependencies.js +0 -28
  76. package/src/utils/has-plugin.d.ts +0 -2
  77. package/src/utils/has-plugin.js +0 -11
  78. package/src/utils/webpack/read-webpack-options.d.ts +0 -10
  79. package/src/utils/webpack/read-webpack-options.js +0 -41
  80. package/src/utils/webpack/resolve-user-defined-webpack-config.d.ts +0 -3
@@ -27,7 +27,8 @@
27
27
  "compiler": {
28
28
  "type": "string",
29
29
  "description": "The compiler to use.",
30
- "enum": ["babel", "swc", "tsc"]
30
+ "enum": ["babel", "swc", "tsc"],
31
+ "default": "babel"
31
32
  },
32
33
  "outputPath": {
33
34
  "type": "string",
@@ -39,7 +40,8 @@
39
40
  "type": "string",
40
41
  "alias": "platform",
41
42
  "description": "Target platform for the build, same as the Webpack target option.",
42
- "enum": ["node", "web", "webworker"]
43
+ "enum": ["node", "web", "webworker"],
44
+ "default": "web"
43
45
  },
44
46
  "deleteOutputPath": {
45
47
  "type": "boolean",
@@ -48,7 +50,8 @@
48
50
  },
49
51
  "watch": {
50
52
  "type": "boolean",
51
- "description": "Enable re-building when files change."
53
+ "description": "Enable re-building when files change.",
54
+ "default": false
52
55
  },
53
56
  "baseHref": {
54
57
  "type": "string",
@@ -60,18 +63,22 @@
60
63
  },
61
64
  "vendorChunk": {
62
65
  "type": "boolean",
63
- "description": "Use a separate bundle containing only vendor libraries."
66
+ "description": "Use a separate bundle containing only vendor libraries.",
67
+ "default": true
64
68
  },
65
69
  "commonChunk": {
66
70
  "type": "boolean",
67
- "description": "Use a separate bundle containing code used across multiple bundles."
71
+ "description": "Use a separate bundle containing code used across multiple bundles.",
72
+ "default": true
68
73
  },
69
74
  "runtimeChunk": {
70
75
  "type": "boolean",
71
- "description": "Use a separate bundle containing the runtime."
76
+ "description": "Use a separate bundle containing the runtime.",
77
+ "default": true
72
78
  },
73
79
  "sourceMap": {
74
80
  "description": "Output sourcemaps. Use 'hidden' for use with error reporting tools without generating sourcemap comment.",
81
+ "default": true,
75
82
  "oneOf": [
76
83
  {
77
84
  "type": "boolean"
@@ -83,11 +90,13 @@
83
90
  },
84
91
  "progress": {
85
92
  "type": "boolean",
86
- "description": "Log progress to the console while building."
93
+ "description": "Log progress to the console while building.",
94
+ "default": false
87
95
  },
88
96
  "assets": {
89
97
  "type": "array",
90
98
  "description": "List of static application assets.",
99
+ "default": [],
91
100
  "items": {
92
101
  "$ref": "#/definitions/assetPattern"
93
102
  }
@@ -103,22 +112,26 @@
103
112
  "description": "External Scripts which will be included before the main application entry.",
104
113
  "items": {
105
114
  "$ref": "#/definitions/extraEntryPoint"
106
- }
115
+ },
116
+ "default": []
107
117
  },
108
118
  "styles": {
109
119
  "type": "array",
110
120
  "description": "External Styles which will be included with the application",
111
121
  "items": {
112
122
  "$ref": "#/definitions/extraEntryPoint"
113
- }
123
+ },
124
+ "default": []
114
125
  },
115
126
  "namedChunks": {
116
127
  "type": "boolean",
117
- "description": "Names the produced bundles according to their entry file."
128
+ "description": "Names the produced bundles according to their entry file.",
129
+ "default": true
118
130
  },
119
131
  "outputHashing": {
120
132
  "type": "string",
121
133
  "description": "Define the output filename cache-busting hashing mode.",
134
+ "default": "none",
122
135
  "enum": ["none", "all", "media", "bundles"]
123
136
  },
124
137
  "stylePreprocessorOptions": {
@@ -130,7 +143,8 @@
130
143
  "type": "array",
131
144
  "items": {
132
145
  "type": "string"
133
- }
146
+ },
147
+ "default": []
134
148
  }
135
149
  },
136
150
  "additionalProperties": false
@@ -161,11 +175,13 @@
161
175
  },
162
176
  "generatePackageJson": {
163
177
  "type": "boolean",
164
- "description": "Generates a `package.json` and pruned lock file with the project's `node_module` dependencies populated for installing in a container. If a `package.json` exists in the project's directory, it will be reused with dependencies populated."
178
+ "description": "Generates a `package.json` and pruned lock file with the project's `node_module` dependencies populated for installing in a container. If a `package.json` exists in the project's directory, it will be reused with dependencies populated.",
179
+ "default": false
165
180
  },
166
181
  "transformers": {
167
182
  "type": "array",
168
183
  "description": "List of TypeScript Compiler Transfomers Plugins.",
184
+ "default": [],
169
185
  "aliases": ["tsPlugins"],
170
186
  "items": {
171
187
  "$ref": "#/definitions/transformerPattern"
@@ -207,15 +223,18 @@
207
223
  }
208
224
  }
209
225
  ],
210
- "description": "Dependencies to keep external to the bundle. (`all` (default), `none`, or an array of module names)"
226
+ "description": "Dependencies to keep external to the bundle. (`all` (default), `none`, or an array of module names)",
227
+ "default": "all"
211
228
  },
212
229
  "extractCss": {
213
230
  "type": "boolean",
214
- "description": "Extract CSS into a `.css` file."
231
+ "description": "Extract CSS into a `.css` file.",
232
+ "default": true
215
233
  },
216
234
  "subresourceIntegrity": {
217
235
  "type": "boolean",
218
- "description": "Enables the use of subresource integrity validation."
236
+ "description": "Enables the use of subresource integrity validation.",
237
+ "default": false
219
238
  },
220
239
  "polyfills": {
221
240
  "type": "string",
@@ -225,30 +244,28 @@
225
244
  },
226
245
  "verbose": {
227
246
  "type": "boolean",
228
- "description": "Emits verbose output"
247
+ "description": "Emits verbose output",
248
+ "default": false
229
249
  },
230
250
  "statsJson": {
231
251
  "type": "boolean",
232
- "description": "Generates a 'stats.json' file which can be analyzed using tools such as: 'webpack-bundle-analyzer' or `<https://webpack.github.io/analyse>`."
252
+ "description": "Generates a 'stats.json' file which can be analyzed using tools such as: 'webpack-bundle-analyzer' or `<https://webpack.github.io/analyse>`.",
253
+ "default": false
233
254
  },
234
255
  "isolatedConfig": {
235
256
  "type": "boolean",
236
257
  "description": "Do not apply Nx webpack plugins automatically. Plugins need to be applied in the project's webpack.config.js file (e.g. withNx, withReact, etc.).",
237
- "default": true,
238
- "x-deprecated": "Automatic configuration of Webpack is deprecated in favor of an explicit 'webpack.config.js' file. This option will be removed in Nx 19. See https://nx.dev/recipes/webpack/webpack-config-setup."
239
- },
240
- "standardWebpackConfigFunction": {
241
- "type": "boolean",
242
- "description": "Set to true if the webpack config exports a standard webpack function, not an Nx-specific one. See: https://webpack.js.org/configuration/configuration-types/#exporting-a-function",
243
258
  "default": false
244
259
  },
245
260
  "extractLicenses": {
246
261
  "type": "boolean",
247
- "description": "Extract all licenses in a separate file, in the case of production builds only."
262
+ "description": "Extract all licenses in a separate file, in the case of production builds only.",
263
+ "default": false
248
264
  },
249
265
  "memoryLimit": {
250
266
  "type": "number",
251
- "description": "Memory limit for type checking service process in `MB`."
267
+ "description": "Memory limit for type checking service process in `MB`.",
268
+ "default": 2048
252
269
  },
253
270
  "fileReplacements": {
254
271
  "description": "Replace files with other files in the build.",
@@ -269,16 +286,18 @@
269
286
  },
270
287
  "additionalProperties": false,
271
288
  "required": ["replace", "with"]
272
- }
289
+ },
290
+ "default": []
273
291
  },
274
292
  "buildLibsFromSource": {
275
293
  "type": "boolean",
276
- "description": "Read buildable libraries from source instead of building them separately. If set to `false`, the `tsConfig` option must also be set to remap paths.",
294
+ "description": "Read buildable libraries from source instead of building them separately.",
277
295
  "default": true
278
296
  },
279
297
  "generateIndexHtml": {
280
298
  "type": "boolean",
281
- "description": "Generates `index.html` file to the output path. This can be turned off if using a webpack plugin to generate HTML such as `html-webpack-plugin`."
299
+ "description": "Generates `index.html` file to the output path. This can be turned off if using a webpack plugin to generate HTML such as `html-webpack-plugin`.",
300
+ "default": true
282
301
  },
283
302
  "postcssConfig": {
284
303
  "type": "string",
@@ -293,7 +312,8 @@
293
312
  },
294
313
  "babelUpwardRootMode": {
295
314
  "type": "boolean",
296
- "description": "Whether to set rootmode to upward. See https://babeljs.io/docs/en/options#rootmode"
315
+ "description": "Whether to set rootmode to upward. See https://babeljs.io/docs/en/options#rootmode",
316
+ "default": false
297
317
  },
298
318
  "babelConfig": {
299
319
  "type": "string",
@@ -301,7 +321,7 @@
301
321
  "x-completion-type": "file"
302
322
  }
303
323
  },
304
- "required": [],
324
+ "required": ["tsConfig", "main"],
305
325
  "definitions": {
306
326
  "assetPattern": {
307
327
  "oneOf": [
@@ -7,52 +7,40 @@ const rxjs_1 = require("rxjs");
7
7
  const operators_1 = require("rxjs/operators");
8
8
  const path_1 = require("path");
9
9
  const buildable_libs_utils_1 = require("@nx/js/src/utils/buildable-libs-utils");
10
+ const get_webpack_config_1 = require("./lib/get-webpack-config");
10
11
  const run_webpack_1 = require("./lib/run-webpack");
11
12
  const fs_1 = require("../../utils/fs");
12
- const resolve_user_defined_webpack_config_1 = require("../../utils/webpack/resolve-user-defined-webpack-config");
13
+ const custom_webpack_1 = require("../../utils/webpack/custom-webpack");
13
14
  const normalize_options_1 = require("./lib/normalize-options");
14
- const config_1 = require("../../utils/config");
15
- const with_nx_1 = require("../../utils/with-nx");
16
- const js_1 = require("@nx/js");
17
- const with_web_1 = require("../../utils/with-web");
18
- async function getWebpackConfigs(options, context) {
15
+ async function getWebpackConfigs(options, projectRoot, context) {
19
16
  if (options.isolatedConfig && !options.webpackConfig) {
20
17
  throw new Error(`Using "isolatedConfig" without a "webpackConfig" is not supported.`);
21
18
  }
22
- let userDefinedWebpackConfig = null;
19
+ let customWebpack = null;
23
20
  if (options.webpackConfig) {
24
- userDefinedWebpackConfig = (0, resolve_user_defined_webpack_config_1.resolveUserDefinedWebpackConfig)(options.webpackConfig, (0, js_1.getRootTsConfigPath)());
25
- if (typeof userDefinedWebpackConfig.then === 'function') {
26
- userDefinedWebpackConfig = await userDefinedWebpackConfig;
21
+ customWebpack = (0, custom_webpack_1.resolveCustomWebpackConfig)(options.webpackConfig, options.tsConfig.startsWith(context.root)
22
+ ? options.tsConfig
23
+ : (0, path_1.join)(context.root, options.tsConfig));
24
+ if (typeof customWebpack.then === 'function') {
25
+ customWebpack = await customWebpack;
27
26
  }
28
27
  }
29
28
  const config = options.isolatedConfig
30
29
  ? {}
31
- : (options.target === 'web'
32
- ? (0, config_1.composePluginsSync)((0, with_nx_1.withNx)(options), (0, with_web_1.withWeb)(options))
33
- : (0, with_nx_1.withNx)(options))({}, { options, context });
34
- if (typeof userDefinedWebpackConfig === 'function' &&
35
- ((0, config_1.isNxWebpackComposablePlugin)(userDefinedWebpackConfig) ||
36
- !options.standardWebpackConfigFunction)) {
37
- // Old behavior, call the Nx-specific webpack config function that user exports
38
- return await userDefinedWebpackConfig(config, {
30
+ : (0, get_webpack_config_1.getWebpackConfig)(context, options);
31
+ if (customWebpack) {
32
+ return await customWebpack(config, {
39
33
  options,
40
34
  context,
41
35
  configuration: context.configurationName, // backwards compat
42
36
  });
43
37
  }
44
- else if (userDefinedWebpackConfig) {
45
- // New behavior, we want the webpack config to export object
46
- return userDefinedWebpackConfig;
47
- }
48
38
  else {
49
- // Fallback case, if we cannot find a webpack config path
39
+ // If the user has no webpackConfig specified then we always have to apply
50
40
  return config;
51
41
  }
52
42
  }
53
43
  async function* webpackExecutor(_options, context) {
54
- // Default to production build.
55
- process.env['NODE_ENV'] ||= 'production';
56
44
  const metadata = context.projectsConfigurations.projects[context.projectName];
57
45
  const sourceRoot = metadata.sourceRoot;
58
46
  const options = (0, normalize_options_1.normalizeOptions)(_options, context.root, metadata.root, sourceRoot);
@@ -64,12 +52,6 @@ async function* webpackExecutor(_options, context) {
64
52
  process.env.NODE_ENV ||= isScriptOptimizeOn
65
53
  ? 'production'
66
54
  : 'development';
67
- process.env.NX_BUILD_LIBS_FROM_SOURCE = `${options.buildLibsFromSource}`;
68
- process.env.NX_BUILD_TARGET = (0, devkit_1.targetToTargetString)({
69
- project: context.projectName,
70
- target: context.targetName,
71
- configuration: context.configurationName,
72
- });
73
55
  if (options.compiler === 'swc') {
74
56
  try {
75
57
  require.resolve('swc-loader');
@@ -79,6 +61,7 @@ async function* webpackExecutor(_options, context) {
79
61
  devkit_1.logger.error(`Missing SWC dependencies: @swc/core, swc-loader. Make sure you install them first.`);
80
62
  return {
81
63
  success: false,
64
+ outfile: (0, path_1.resolve)(context.root, options.outputPath, options.outputFileName),
82
65
  options,
83
66
  };
84
67
  }
@@ -86,17 +69,16 @@ async function* webpackExecutor(_options, context) {
86
69
  if (!options.buildLibsFromSource && context.targetName) {
87
70
  const { dependencies } = (0, buildable_libs_utils_1.calculateProjectBuildableDependencies)(context.taskGraph, context.projectGraph, context.root, context.projectName, context.targetName, context.configurationName);
88
71
  options.tsConfig = (0, buildable_libs_utils_1.createTmpTsConfig)(options.tsConfig, context.root, metadata.root, dependencies);
89
- process.env.NX_TSCONFIG_PATH = options.tsConfig;
90
72
  }
91
73
  // Delete output path before bundling
92
- if (options.deleteOutputPath && options.outputPath) {
74
+ if (options.deleteOutputPath) {
93
75
  (0, fs_1.deleteOutputDir)(context.root, options.outputPath);
94
76
  }
95
77
  if (options.generatePackageJson && metadata.projectType !== 'application') {
96
78
  devkit_1.logger.warn((0, devkit_1.stripIndents) `The project ${context.projectName} is using the 'generatePackageJson' option which is deprecated for library projects. It should only be used for applications.
97
79
  For libraries, configure the project to use the '@nx/dependency-checks' ESLint rule instead (https://nx.dev/packages/eslint-plugin/documents/dependency-checks).`);
98
80
  }
99
- const configs = await getWebpackConfigs(options, context);
81
+ const configs = await getWebpackConfigs(options, metadata.root, context);
100
82
  return yield* (0, rxjs_for_await_1.eachValueFrom)((0, rxjs_1.of)(configs).pipe((0, operators_1.mergeMap)((config) => (Array.isArray(config) ? (0, rxjs_1.from)(config) : (0, rxjs_1.of)(config))),
101
83
  // Run build sequentially and bail when first one fails.
102
84
  (0, operators_1.mergeScan)((acc, config) => {
@@ -112,8 +94,6 @@ async function* webpackExecutor(_options, context) {
112
94
  // Collect build results as an array.
113
95
  (0, operators_1.bufferCount)(Array.isArray(configs) ? configs.length : 1), (0, operators_1.switchMap)(async (results) => {
114
96
  const success = results.every((result) => Boolean(result) && !result.hasErrors());
115
- // TODO(jack): This should read output from webpack config if provided.
116
- // The outfile is only used by NestJS, where `@nx/js:node` executor requires it to run the file.
117
97
  return {
118
98
  success,
119
99
  outfile: (0, path_1.resolve)(context.root, options.outputPath, options.outputFileName),
@@ -1,5 +1,4 @@
1
- import { GeneratorCallback, Tree } from '@nx/devkit';
1
+ import type { Tree } from '@nx/devkit';
2
2
  import { ConfigurationGeneratorSchema } from './schema';
3
- export declare function configurationGenerator(tree: Tree, options: ConfigurationGeneratorSchema): Promise<GeneratorCallback>;
4
- export declare function configurationGeneratorInternal(tree: Tree, options: ConfigurationGeneratorSchema): Promise<GeneratorCallback>;
3
+ export declare function configurationGenerator(tree: Tree, options: ConfigurationGeneratorSchema): Promise<import("@nx/devkit").GeneratorCallback>;
5
4
  export default configurationGenerator;
@@ -1,44 +1,24 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.configurationGeneratorInternal = exports.configurationGenerator = void 0;
3
+ exports.configurationGenerator = void 0;
4
4
  const devkit_1 = require("@nx/devkit");
5
5
  const init_1 = require("../init/init");
6
- const has_plugin_1 = require("../../utils/has-plugin");
7
- const add_build_target_defaults_1 = require("@nx/devkit/src/generators/add-build-target-defaults");
8
- const ensure_dependencies_1 = require("../../utils/ensure-dependencies");
9
- function configurationGenerator(tree, options) {
10
- return configurationGeneratorInternal(tree, { addPlugin: false, ...options });
11
- }
12
- exports.configurationGenerator = configurationGenerator;
13
- async function configurationGeneratorInternal(tree, options) {
14
- const tasks = [];
15
- const nxJson = (0, devkit_1.readNxJson)(tree);
16
- const addPluginDefault = process.env.NX_ADD_PLUGINS !== 'false' &&
17
- nxJson.useInferencePlugins !== false;
18
- options.addPlugin ??= addPluginDefault;
19
- const initTask = await (0, init_1.webpackInitGenerator)(tree, {
6
+ async function configurationGenerator(tree, options) {
7
+ const task = await (0, init_1.webpackInitGenerator)(tree, {
20
8
  ...options,
21
9
  skipFormat: true,
22
10
  });
23
- tasks.push(initTask);
24
- const depsTask = (0, ensure_dependencies_1.ensureDependencies)(tree, {
25
- compiler: options.compiler === 'babel' ? undefined : options.compiler,
26
- });
27
- tasks.push(depsTask);
28
11
  checkForTargetConflicts(tree, options);
29
- if (!(0, has_plugin_1.hasPlugin)(tree)) {
30
- addBuildTarget(tree, options);
31
- if (options.devServer) {
32
- addServeTarget(tree, options);
33
- }
12
+ addBuildTarget(tree, options);
13
+ if (options.devServer) {
14
+ addServeTarget(tree, options);
34
15
  }
35
- createWebpackConfig(tree, options);
36
16
  if (!options.skipFormat) {
37
17
  await (0, devkit_1.formatFiles)(tree);
38
18
  }
39
- return (0, devkit_1.runTasksInSerial)(...tasks);
19
+ return task;
40
20
  }
41
- exports.configurationGeneratorInternal = configurationGeneratorInternal;
21
+ exports.configurationGenerator = configurationGenerator;
42
22
  function checkForTargetConflicts(tree, options) {
43
23
  if (options.skipValidation)
44
24
  return;
@@ -50,7 +30,7 @@ function checkForTargetConflicts(tree, options) {
50
30
  throw new Error(`Project "${project.name}" already has a serve target. Pass --skipValidation to ignore this error.`);
51
31
  }
52
32
  }
53
- function createWebpackConfig(tree, options) {
33
+ function addBuildTarget(tree, options) {
54
34
  const project = (0, devkit_1.readProjectConfiguration)(tree, options.project);
55
35
  const buildOptions = {
56
36
  target: options.target,
@@ -60,28 +40,20 @@ function createWebpackConfig(tree, options) {
60
40
  tsConfig: options.tsConfig ?? (0, devkit_1.joinPathFragments)(project.root, 'tsconfig.app.json'),
61
41
  webpackConfig: (0, devkit_1.joinPathFragments)(project.root, 'webpack.config.js'),
62
42
  };
43
+ if (options.webpackConfig) {
44
+ buildOptions.webpackConfig = options.webpackConfig;
45
+ }
46
+ if (options.babelConfig) {
47
+ buildOptions.babelConfig = options.babelConfig;
48
+ }
49
+ else if (options.compiler === 'babel') {
50
+ // If no babel config file is provided then write a default one, otherwise build will fail.
51
+ (0, devkit_1.writeJson)(tree, (0, devkit_1.joinPathFragments)(project.root, '.babelrc'), {
52
+ presets: ['@nx/js/babel'],
53
+ });
54
+ }
63
55
  if (options.target === 'web') {
64
- tree.write((0, devkit_1.joinPathFragments)(project.root, 'webpack.config.js'), (0, has_plugin_1.hasPlugin)(tree)
65
- ? `
66
- const { NxWebpackPlugin } = require('@nx/webpack');
67
- const { join } = require('path');
68
-
69
- module.exports = {
70
- output: {
71
- path: join(__dirname, '${(0, devkit_1.offsetFromRoot)(project.root)}${buildOptions.outputPath}'),
72
- },
73
- plugins: [
74
- new NxWebpackPlugin({
75
- target: '${buildOptions.target}',
76
- tsConfig: '${buildOptions.tsConfig}',
77
- compiler: '${buildOptions.compiler}',
78
- main: '${buildOptions.main}',
79
- outputHashing: '${buildOptions.target !== 'web' ? 'none' : 'all'}',
80
- })
81
- ],
82
- }
83
- `
84
- : `
56
+ tree.write((0, devkit_1.joinPathFragments)(project.root, 'webpack.config.js'), `
85
57
  const { composePlugins, withNx, withWeb } = require('@nx/webpack');
86
58
 
87
59
  // Nx plugins for webpack.
@@ -93,27 +65,7 @@ module.exports = composePlugins(withNx(), withWeb(), (config) => {
93
65
  `);
94
66
  }
95
67
  else {
96
- tree.write((0, devkit_1.joinPathFragments)(project.root, 'webpack.config.js'), (0, has_plugin_1.hasPlugin)(tree)
97
- ? `
98
- const { NxWebpackPlugin } = require('@nx/webpack');
99
- const { join } = require('path');
100
-
101
- module.exports = {
102
- output: {
103
- path: join(__dirname, '${(0, devkit_1.offsetFromRoot)(project.root)}${buildOptions.outputPath}'),
104
- },
105
- plugins: [
106
- new NxWebpackPlugin({
107
- target: '${buildOptions.target}',
108
- tsConfig: '${buildOptions.tsConfig}',
109
- compiler: '${buildOptions.compiler}',
110
- main: '${buildOptions.main}',
111
- outputHashing: '${buildOptions.target !== 'web' ? 'none' : 'all'}',
112
- })
113
- ],
114
- }
115
- `
116
- : `
68
+ tree.write((0, devkit_1.joinPathFragments)(project.root, 'webpack.config.js'), `
117
69
  const { composePlugins, withNx } = require('@nx/webpack');
118
70
 
119
71
  // Nx plugins for webpack.
@@ -124,30 +76,6 @@ module.exports = composePlugins(withNx(), (config) => {
124
76
  });
125
77
  `);
126
78
  }
127
- }
128
- function addBuildTarget(tree, options) {
129
- (0, add_build_target_defaults_1.addBuildTargetDefaults)(tree, '@nx/webpack:webpack');
130
- const project = (0, devkit_1.readProjectConfiguration)(tree, options.project);
131
- const buildOptions = {
132
- target: options.target,
133
- outputPath: (0, devkit_1.joinPathFragments)('dist', project.root),
134
- compiler: options.compiler ?? 'swc',
135
- main: options.main ?? (0, devkit_1.joinPathFragments)(project.root, 'src/main.ts'),
136
- tsConfig: options.tsConfig ?? (0, devkit_1.joinPathFragments)(project.root, 'tsconfig.app.json'),
137
- webpackConfig: (0, devkit_1.joinPathFragments)(project.root, 'webpack.config.js'),
138
- };
139
- if (options.webpackConfig) {
140
- buildOptions.webpackConfig = options.webpackConfig;
141
- }
142
- if (options.babelConfig) {
143
- buildOptions.babelConfig = options.babelConfig;
144
- }
145
- else if (options.compiler === 'babel') {
146
- // If no babel config file is provided then write a default one, otherwise build will fail.
147
- (0, devkit_1.writeJson)(tree, (0, devkit_1.joinPathFragments)(project.root, '.babelrc'), {
148
- presets: ['@nx/js/babel'],
149
- });
150
- }
151
79
  (0, devkit_1.updateProjectConfiguration)(tree, options.project, {
152
80
  ...project,
153
81
  targets: {
@@ -10,5 +10,4 @@ export interface ConfigurationGeneratorSchema {
10
10
  target?: 'node' | 'web' | 'webworker';
11
11
  webpackConfig?: string;
12
12
  babelConfig?: string;
13
- addPlugin?: boolean;
14
13
  }
@@ -1,5 +1,5 @@
1
1
  {
2
- "$schema": "https://json-schema.org/schema",
2
+ "$schema": "http://json-schema.org/schema",
3
3
  "$id": "NxWebpackProject",
4
4
  "cli": "nx",
5
5
  "title": "Add Webpack Configuration to a project",
@@ -1,5 +1,4 @@
1
1
  import { GeneratorCallback, Tree } from '@nx/devkit';
2
2
  import { Schema } from './schema';
3
3
  export declare function webpackInitGenerator(tree: Tree, schema: Schema): Promise<GeneratorCallback>;
4
- export declare function webpackInitGeneratorInternal(tree: Tree, schema: Schema): Promise<GeneratorCallback>;
5
4
  export default webpackInitGenerator;
@@ -1,60 +1,35 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.webpackInitGeneratorInternal = exports.webpackInitGenerator = void 0;
3
+ exports.webpackInitGenerator = void 0;
4
4
  const devkit_1 = require("@nx/devkit");
5
- const update_package_scripts_1 = require("@nx/devkit/src/utils/update-package-scripts");
6
- const plugin_1 = require("../../plugins/plugin");
5
+ const add_swc_dependencies_1 = require("@nx/js/src/utils/swc/add-swc-dependencies");
7
6
  const versions_1 = require("../../utils/versions");
8
- function webpackInitGenerator(tree, schema) {
9
- return webpackInitGeneratorInternal(tree, { addPlugin: false, ...schema });
10
- }
11
- exports.webpackInitGenerator = webpackInitGenerator;
12
- async function webpackInitGeneratorInternal(tree, schema) {
13
- const nxJson = (0, devkit_1.readNxJson)(tree);
14
- const addPluginDefault = process.env.NX_ADD_PLUGINS !== 'false' &&
15
- nxJson.useInferencePlugins !== false;
16
- schema.addPlugin ??= addPluginDefault;
17
- if (schema.addPlugin) {
18
- addPlugin(tree);
7
+ async function webpackInitGenerator(tree, schema) {
8
+ const tasks = [];
9
+ const devDependencies = {
10
+ '@nx/webpack': versions_1.nxVersion,
11
+ };
12
+ if (schema.compiler === 'swc') {
13
+ devDependencies['swc-loader'] = versions_1.swcLoaderVersion;
14
+ const addSwcTask = (0, add_swc_dependencies_1.addSwcDependencies)(tree);
15
+ tasks.push(addSwcTask);
19
16
  }
20
- let installTask = () => { };
21
- if (!schema.skipPackageJson) {
22
- const devDependencies = {
23
- '@nx/webpack': versions_1.nxVersion,
24
- '@nx/web': versions_1.nxVersion,
25
- };
26
- if (schema.addPlugin) {
27
- devDependencies['webpack-cli'] = versions_1.webpackCliVersion;
28
- }
29
- installTask = (0, devkit_1.addDependenciesToPackageJson)(tree, {}, devDependencies, undefined, schema.keepExistingVersions);
17
+ if (schema.compiler === 'tsc') {
18
+ devDependencies['tslib'] = versions_1.tsLibVersion;
30
19
  }
31
- if (schema.updatePackageScripts) {
32
- await (0, update_package_scripts_1.updatePackageScripts)(tree, plugin_1.createNodes);
20
+ if (schema.uiFramework === 'react') {
21
+ devDependencies['@pmmmwh/react-refresh-webpack-plugin'] =
22
+ versions_1.reactRefreshWebpackPluginVersion;
23
+ devDependencies['@svgr/webpack'] = versions_1.svgrWebpackVersion;
24
+ devDependencies['react-refresh'] = versions_1.reactRefreshVersion;
25
+ devDependencies['url-loader'] = versions_1.urlLoaderVersion;
33
26
  }
34
27
  if (!schema.skipFormat) {
35
28
  await (0, devkit_1.formatFiles)(tree);
36
29
  }
37
- return installTask;
38
- }
39
- exports.webpackInitGeneratorInternal = webpackInitGeneratorInternal;
40
- function addPlugin(tree) {
41
- const nxJson = (0, devkit_1.readNxJson)(tree);
42
- nxJson.plugins ??= [];
43
- for (const plugin of nxJson.plugins) {
44
- if (typeof plugin === 'string'
45
- ? plugin === '@nx/webpack/plugin'
46
- : plugin.plugin === '@nx/webpack/plugin') {
47
- return;
48
- }
49
- }
50
- nxJson.plugins.push({
51
- plugin: '@nx/webpack/plugin',
52
- options: {
53
- buildTargetName: 'build',
54
- serveTargetName: 'serve',
55
- previewTargetName: 'preview',
56
- },
57
- });
58
- (0, devkit_1.updateNxJson)(tree, nxJson);
30
+ const baseInstalTask = (0, devkit_1.addDependenciesToPackageJson)(tree, {}, devDependencies);
31
+ tasks.push(baseInstalTask);
32
+ return (0, devkit_1.runTasksInSerial)(...tasks);
59
33
  }
34
+ exports.webpackInitGenerator = webpackInitGenerator;
60
35
  exports.default = webpackInitGenerator;
@@ -1,7 +1,5 @@
1
1
  export interface Schema {
2
+ compiler?: 'babel' | 'swc' | 'tsc';
3
+ uiFramework?: 'react' | 'none';
2
4
  skipFormat?: boolean;
3
- skipPackageJson?: boolean;
4
- keepExistingVersions?: boolean;
5
- updatePackageScripts?: boolean;
6
- addPlugin?: boolean;
7
5
  }