@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
@@ -1,34 +1,374 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.withNx = void 0;
4
- const apply_base_config_1 = require("../plugins/nx-webpack-plugin/lib/apply-base-config");
5
- const normalize_options_1 = require("../plugins/nx-webpack-plugin/lib/normalize-options");
3
+ exports.createLoaderFromCompiler = exports.withNx = void 0;
4
+ const path = require("path");
5
+ const path_1 = require("path");
6
+ const webpack_1 = require("webpack");
7
+ const tsconfig_paths_webpack_plugin_1 = require("tsconfig-paths-webpack-plugin");
8
+ const js_1 = require("@nx/js");
9
+ const license_webpack_plugin_1 = require("license-webpack-plugin");
10
+ const stats_json_plugin_1 = require("../plugins/stats-json-plugin");
11
+ const create_copy_plugin_1 = require("./create-copy-plugin");
12
+ const generate_package_json_plugin_1 = require("../plugins/generate-package-json-plugin");
13
+ const hash_format_1 = require("./hash-format");
14
+ const fs_1 = require("fs");
15
+ const TerserPlugin = require("terser-webpack-plugin");
16
+ const nodeExternals = require("webpack-node-externals");
17
+ const ForkTsCheckerWebpackPlugin = require("fork-ts-checker-webpack-plugin");
18
+ const browserslist = require("browserslist");
19
+ const VALID_BROWSERSLIST_FILES = ['.browserslistrc', 'browserslist'];
20
+ const ES5_BROWSERS = [
21
+ 'ie 10',
22
+ 'ie 11',
23
+ 'safari 11',
24
+ 'safari 11.1',
25
+ 'safari 12',
26
+ 'safari 12.1',
27
+ 'safari 13',
28
+ 'ios_saf 13.0',
29
+ 'ios_saf 13.3',
30
+ ];
31
+ function getTerserEcmaVersion(projectRoot) {
32
+ let pathToBrowserslistFile = '';
33
+ for (const browserslistFile of VALID_BROWSERSLIST_FILES) {
34
+ const fullPathToFile = (0, path_1.join)(projectRoot, browserslistFile);
35
+ if ((0, fs_1.existsSync)(fullPathToFile)) {
36
+ pathToBrowserslistFile = fullPathToFile;
37
+ break;
38
+ }
39
+ }
40
+ if (!pathToBrowserslistFile) {
41
+ return 2020;
42
+ }
43
+ const env = browserslist.loadConfig({ path: pathToBrowserslistFile });
44
+ const browsers = browserslist(env);
45
+ return browsers.some((b) => ES5_BROWSERS.includes(b)) ? 5 : 2020;
46
+ }
47
+ const IGNORED_WEBPACK_WARNINGS = [
48
+ /The comment file/i,
49
+ /could not find any license/i,
50
+ ];
51
+ const extensions = ['.ts', '.tsx', '.mjs', '.js', '.jsx'];
52
+ const mainFields = ['module', 'main'];
6
53
  const processed = new Set();
7
54
  /**
8
55
  * @param {WithNxOptions} pluginOptions
9
56
  * @returns {NxWebpackPlugin}
10
57
  */
11
- function withNx(pluginOptions = {}) {
12
- return function configure(config, { options, context }) {
58
+ function withNx(pluginOptions) {
59
+ return function configure(config, { options, context, }) {
13
60
  if (processed.has(config))
14
61
  return config;
15
- (0, apply_base_config_1.applyBaseConfig)({
16
- ...options,
17
- ...pluginOptions,
18
- target: options.target ?? 'web',
19
- assets: options.assets
20
- ? options.assets
21
- : pluginOptions.assets
22
- ? (0, normalize_options_1.normalizeAssets)(pluginOptions.assets, options.root, options.sourceRoot, options.projectRoot)
23
- : [],
24
- root: context.root,
25
- projectName: context.projectName,
26
- targetName: context.targetName,
27
- configurationName: context.configurationName,
28
- projectGraph: context.projectGraph,
29
- }, config);
30
- processed.add(config);
31
- return config;
62
+ const plugins = [];
63
+ if (!pluginOptions?.skipTypeChecking) {
64
+ plugins.push(new ForkTsCheckerWebpackPlugin({
65
+ typescript: {
66
+ configFile: options.tsConfig,
67
+ memoryLimit: options.memoryLimit || 2018,
68
+ },
69
+ }));
70
+ }
71
+ const entry = {};
72
+ if (options.main) {
73
+ const mainEntry = options.outputFileName
74
+ ? path.parse(options.outputFileName).name
75
+ : 'main';
76
+ entry[mainEntry] = [options.main];
77
+ }
78
+ if (options.additionalEntryPoints) {
79
+ for (const { entryName, entryPath } of options.additionalEntryPoints) {
80
+ entry[entryName] = entryPath;
81
+ }
82
+ }
83
+ if (options.polyfills) {
84
+ entry['polyfills'] = [
85
+ ...(entry['polyfills'] || []),
86
+ path.resolve(options.root, options.polyfills),
87
+ ];
88
+ }
89
+ if (options.progress) {
90
+ plugins.push(new webpack_1.ProgressPlugin({ profile: options.verbose }));
91
+ }
92
+ if (options.extractLicenses) {
93
+ plugins.push(new license_webpack_plugin_1.LicenseWebpackPlugin({
94
+ stats: {
95
+ warnings: false,
96
+ errors: false,
97
+ },
98
+ perChunkOutput: false,
99
+ outputFilename: `3rdpartylicenses.txt`,
100
+ }));
101
+ }
102
+ if (Array.isArray(options.assets) && options.assets.length > 0) {
103
+ plugins.push((0, create_copy_plugin_1.createCopyPlugin)(options.assets));
104
+ }
105
+ if (options.generatePackageJson && context) {
106
+ plugins.push(new generate_package_json_plugin_1.GeneratePackageJsonPlugin(options, context));
107
+ }
108
+ if (options.statsJson) {
109
+ plugins.push(new stats_json_plugin_1.StatsJsonPlugin());
110
+ }
111
+ let externals = [];
112
+ if (options.target === 'node' && options.externalDependencies === 'all') {
113
+ const modulesDir = `${options.root}/node_modules`;
114
+ externals.push(nodeExternals({ modulesDir }));
115
+ }
116
+ else if (Array.isArray(options.externalDependencies)) {
117
+ externals.push(function (ctx, callback) {
118
+ if (options.externalDependencies.includes(ctx.request)) {
119
+ // not bundled
120
+ return callback(null, `commonjs ${ctx.request}`);
121
+ }
122
+ // bundled
123
+ callback();
124
+ });
125
+ }
126
+ const hashFormat = (0, hash_format_1.getOutputHashFormat)(options.outputHashing);
127
+ const filename = options.outputHashing
128
+ ? `[name]${hashFormat.script}.js`
129
+ : '[name].js';
130
+ const chunkFilename = options.outputHashing
131
+ ? `[name]${hashFormat.chunk}.js`
132
+ : '[name].js';
133
+ const updated = {
134
+ ...config,
135
+ context: context
136
+ ? path.join(context.root, options.projectRoot)
137
+ : undefined,
138
+ target: options.target,
139
+ node: false,
140
+ mode:
141
+ // When the target is Node avoid any optimizations, such as replacing `process.env.NODE_ENV` with build time value.
142
+ options.target === 'node'
143
+ ? 'none'
144
+ : // Otherwise, make sure it matches `process.env.NODE_ENV`.
145
+ // When mode is development or production, webpack will automatically
146
+ // configure DefinePlugin to replace `process.env.NODE_ENV` with the
147
+ // build-time value. Thus, we need to make sure it's the same value to
148
+ // avoid conflicts.
149
+ //
150
+ // When the NODE_ENV is something else (e.g. test), then set it to none
151
+ // to prevent extra behavior from webpack.
152
+ process.env.NODE_ENV === 'development' ||
153
+ process.env.NODE_ENV === 'production'
154
+ ? process.env.NODE_ENV
155
+ : 'none',
156
+ // When target is Node, the Webpack mode will be set to 'none' which disables in memory caching and causes a full rebuild on every change.
157
+ // So to mitigate this we enable in memory caching when target is Node and in watch mode.
158
+ cache: options.target === 'node' && options.watch
159
+ ? { type: 'memory' }
160
+ : undefined,
161
+ devtool: options.sourceMap === 'hidden'
162
+ ? 'hidden-source-map'
163
+ : options.sourceMap
164
+ ? 'source-map'
165
+ : false,
166
+ entry,
167
+ output: {
168
+ ...config.output,
169
+ libraryTarget: options.target === 'node' ? 'commonjs' : undefined,
170
+ path: options.outputPath,
171
+ filename,
172
+ chunkFilename,
173
+ hashFunction: 'xxhash64',
174
+ // Disabled for performance
175
+ pathinfo: false,
176
+ // Use CJS for Node since it has the widest support.
177
+ scriptType: options.target === 'node' ? undefined : 'module',
178
+ },
179
+ watch: options.watch,
180
+ watchOptions: {
181
+ poll: options.poll,
182
+ },
183
+ profile: options.statsJson,
184
+ resolve: {
185
+ ...config.resolve,
186
+ extensions: [...extensions, ...(config?.resolve?.extensions ?? [])],
187
+ alias: options.fileReplacements.reduce((aliases, replacement) => ({
188
+ ...aliases,
189
+ [replacement.replace]: replacement.with,
190
+ }), {}),
191
+ plugins: [
192
+ ...(config.resolve?.plugins ?? []),
193
+ new tsconfig_paths_webpack_plugin_1.TsconfigPathsPlugin({
194
+ configFile: options.tsConfig,
195
+ extensions: [...extensions, ...(config?.resolve?.extensions ?? [])],
196
+ mainFields,
197
+ }),
198
+ ],
199
+ mainFields,
200
+ },
201
+ externals,
202
+ optimization: {
203
+ ...config.optimization,
204
+ sideEffects: true,
205
+ minimize: typeof options.optimization === 'object'
206
+ ? !!options.optimization.scripts
207
+ : !!options.optimization,
208
+ minimizer: [
209
+ options.compiler !== 'swc'
210
+ ? new TerserPlugin({
211
+ parallel: true,
212
+ terserOptions: {
213
+ keep_classnames: true,
214
+ ecma: getTerserEcmaVersion((0, path_1.join)(options.root, options.projectRoot)),
215
+ safari10: true,
216
+ format: {
217
+ ascii_only: true,
218
+ comments: false,
219
+ webkit: true,
220
+ },
221
+ },
222
+ extractComments: false,
223
+ })
224
+ : new TerserPlugin({
225
+ minify: TerserPlugin.swcMinify,
226
+ // `terserOptions` options will be passed to `swc`
227
+ terserOptions: {
228
+ module: true,
229
+ mangle: false,
230
+ },
231
+ }),
232
+ ],
233
+ runtimeChunk: false,
234
+ concatenateModules: true,
235
+ },
236
+ performance: {
237
+ ...config.performance,
238
+ hints: false,
239
+ },
240
+ experiments: { ...config.experiments, cacheUnaffected: true },
241
+ ignoreWarnings: [
242
+ (x) => IGNORED_WEBPACK_WARNINGS.some((r) => typeof x === 'string' ? r.test(x) : r.test(x.message)),
243
+ ],
244
+ module: {
245
+ ...config.module,
246
+ // Enabled for performance
247
+ unsafeCache: true,
248
+ rules: [
249
+ ...(config?.module?.rules ?? []),
250
+ options.sourceMap && {
251
+ test: /\.js$/,
252
+ enforce: 'pre',
253
+ loader: require.resolve('source-map-loader'),
254
+ },
255
+ {
256
+ // There's an issue resolving paths without fully specified extensions
257
+ // See: https://github.com/graphql/graphql-js/issues/2721
258
+ // TODO(jack): Add a flag to turn this option on like Next.js does via experimental flag.
259
+ // See: https://github.com/vercel/next.js/pull/29880
260
+ test: /\.m?jsx?$/,
261
+ resolve: {
262
+ fullySpecified: false,
263
+ },
264
+ },
265
+ // There's an issue when using buildable libs and .js files (instead of .ts files),
266
+ // where the wrong type is used (commonjs vs esm) resulting in export-imports throwing errors.
267
+ // See: https://github.com/nrwl/nx/issues/10990
268
+ {
269
+ test: /\.js$/,
270
+ type: 'javascript/auto',
271
+ },
272
+ createLoaderFromCompiler(options),
273
+ ].filter((r) => !!r),
274
+ },
275
+ plugins: (config.plugins ?? []).concat(plugins),
276
+ stats: {
277
+ hash: true,
278
+ timings: false,
279
+ cached: false,
280
+ cachedAssets: false,
281
+ modules: false,
282
+ warnings: true,
283
+ errors: true,
284
+ colors: !options.verbose && !options.statsJson,
285
+ chunks: !options.verbose,
286
+ assets: !!options.verbose,
287
+ chunkOrigins: !!options.verbose,
288
+ chunkModules: !!options.verbose,
289
+ children: !!options.verbose,
290
+ reasons: !!options.verbose,
291
+ version: !!options.verbose,
292
+ errorDetails: !!options.verbose,
293
+ moduleTrace: !!options.verbose,
294
+ usedExports: !!options.verbose,
295
+ },
296
+ };
297
+ processed.add(updated);
298
+ return updated;
32
299
  };
33
300
  }
34
301
  exports.withNx = withNx;
302
+ function createLoaderFromCompiler(options) {
303
+ switch (options.compiler) {
304
+ case 'swc':
305
+ return {
306
+ test: /\.([jt])sx?$/,
307
+ loader: require.resolve('swc-loader'),
308
+ exclude: /node_modules/,
309
+ options: {
310
+ jsc: {
311
+ parser: {
312
+ syntax: 'typescript',
313
+ decorators: true,
314
+ tsx: true,
315
+ },
316
+ transform: {
317
+ react: {
318
+ runtime: 'automatic',
319
+ },
320
+ },
321
+ loose: true,
322
+ },
323
+ },
324
+ };
325
+ case 'tsc':
326
+ const { loadTsTransformers } = require('@nx/js');
327
+ const { compilerPluginHooks, hasPlugin } = loadTsTransformers(options.transformers);
328
+ return {
329
+ test: /\.([jt])sx?$/,
330
+ loader: require.resolve(`ts-loader`),
331
+ exclude: /node_modules/,
332
+ options: {
333
+ configFile: options.tsConfig,
334
+ transpileOnly: !hasPlugin,
335
+ // https://github.com/TypeStrong/ts-loader/pull/685
336
+ experimentalWatchApi: true,
337
+ getCustomTransformers: (program) => ({
338
+ before: compilerPluginHooks.beforeHooks.map((hook) => hook(program)),
339
+ after: compilerPluginHooks.afterHooks.map((hook) => hook(program)),
340
+ afterDeclarations: compilerPluginHooks.afterDeclarationsHooks.map((hook) => hook(program)),
341
+ }),
342
+ },
343
+ };
344
+ case 'babel':
345
+ const tsConfig = (0, js_1.readTsConfig)(options.tsConfig);
346
+ const babelConfig = {
347
+ test: /\.([jt])sx?$/,
348
+ loader: path.join(__dirname, './web-babel-loader'),
349
+ exclude: /node_modules/,
350
+ options: {
351
+ cwd: path.join(options.root, options.sourceRoot),
352
+ emitDecoratorMetadata: tsConfig.options.emitDecoratorMetadata,
353
+ isModern: true,
354
+ isTest: process.env.NX_CYPRESS_COMPONENT_TEST === 'true',
355
+ envName: process.env.BABEL_ENV ?? process.env.NODE_ENV,
356
+ cacheDirectory: true,
357
+ cacheCompression: false,
358
+ },
359
+ };
360
+ if (options.babelUpwardRootMode) {
361
+ babelConfig.options['rootMode'] = 'upward';
362
+ babelConfig.options['babelrc'] = true;
363
+ }
364
+ else {
365
+ babelConfig.options['configFile'] = options.babelConfig
366
+ ? path.join(options.root, options.babelConfig)
367
+ : path.join(options.root, options.projectRoot, '.babelrc');
368
+ }
369
+ return babelConfig;
370
+ default:
371
+ return null;
372
+ }
373
+ }
374
+ exports.createLoaderFromCompiler = createLoaderFromCompiler;
@@ -1,4 +1,4 @@
1
- import { NxComposableWebpackPlugin } from './config';
1
+ import { NxWebpackPlugin } from './config';
2
2
  import { ExtraEntryPointClass, NormalizedWebpackExecutorOptions } from '../executors/webpack/schema';
3
3
  export interface WithWebOptions {
4
4
  baseHref?: string;
@@ -19,4 +19,4 @@ export type MergedOptions = Omit<NormalizedWebpackExecutorOptions, keyof WithWeb
19
19
  * @param {WithWebOptions} pluginOptions
20
20
  * @returns {NxWebpackPlugin}
21
21
  */
22
- export declare function withWeb(pluginOptions?: WithWebOptions): NxComposableWebpackPlugin;
22
+ export declare function withWeb(pluginOptions?: WithWebOptions): NxWebpackPlugin;