@nx/webpack 17.2.0-beta.8 → 17.2.0-beta.9
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 +4 -4
- package/plugin.d.ts +1 -0
- package/plugin.js +5 -0
- package/src/executors/dev-server/dev-server.impl.js +16 -25
- package/src/executors/dev-server/lib/get-dev-server-config.d.ts +2 -3
- package/src/executors/dev-server/lib/get-dev-server-config.js +18 -26
- package/src/executors/dev-server/schema.d.ts +8 -8
- package/src/executors/webpack/lib/normalize-options.js +5 -2
- package/src/executors/webpack/schema.d.ts +5 -3
- package/src/executors/webpack/schema.json +26 -51
- package/src/executors/webpack/webpack.impl.js +21 -19
- package/src/generators/configuration/configuration.js +69 -18
- package/src/generators/init/init.js +28 -2
- package/src/plugins/nx-webpack-plugin/lib/apply-base-config.js +146 -127
- package/src/plugins/nx-webpack-plugin/lib/apply-web-config.js +8 -0
- package/src/plugins/nx-webpack-plugin/lib/normalize-options.js +23 -1
- package/src/plugins/nx-webpack-plugin/nx-webpack-plugin-options.d.ts +132 -3
- package/src/plugins/nx-webpack-plugin/nx-webpack-plugin.d.ts +1 -2
- package/src/plugins/nx-webpack-plugin/nx-webpack-plugin.js +9 -17
- package/src/plugins/plugin.d.ts +9 -0
- package/src/plugins/plugin.js +127 -0
- package/src/utils/config.d.ts +11 -6
- package/src/utils/config.js +48 -13
- package/src/utils/has-plugin.d.ts +2 -0
- package/src/utils/has-plugin.js +11 -0
- package/src/utils/versions.d.ts +1 -0
- package/src/utils/versions.js +2 -1
- package/src/utils/webpack/read-webpack-options.d.ts +10 -0
- package/src/utils/webpack/read-webpack-options.js +41 -0
- package/src/utils/webpack/resolve-user-defined-webpack-config.d.ts +1 -0
- package/src/utils/webpack/{custom-webpack.js → resolve-user-defined-webpack-config.js} +3 -8
- package/src/utils/with-nx.d.ts +4 -5
- package/src/utils/with-nx.js +7 -1
- package/src/utils/with-web.d.ts +2 -2
- package/src/executors/webpack/lib/get-webpack-config.d.ts +0 -5
- package/src/executors/webpack/lib/get-webpack-config.js +0 -16
- package/src/utils/webpack/custom-webpack.d.ts +0 -2
|
@@ -3,16 +3,20 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
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");
|
|
6
7
|
async function configurationGenerator(tree, options) {
|
|
7
8
|
const task = await (0, init_1.webpackInitGenerator)(tree, {
|
|
8
9
|
...options,
|
|
9
10
|
skipFormat: true,
|
|
10
11
|
});
|
|
11
12
|
checkForTargetConflicts(tree, options);
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
13
|
+
if (!(0, has_plugin_1.hasPlugin)(tree)) {
|
|
14
|
+
addBuildTarget(tree, options);
|
|
15
|
+
if (options.devServer) {
|
|
16
|
+
addServeTarget(tree, options);
|
|
17
|
+
}
|
|
15
18
|
}
|
|
19
|
+
createWebpackConfig(tree, options);
|
|
16
20
|
if (!options.skipFormat) {
|
|
17
21
|
await (0, devkit_1.formatFiles)(tree);
|
|
18
22
|
}
|
|
@@ -30,7 +34,7 @@ function checkForTargetConflicts(tree, options) {
|
|
|
30
34
|
throw new Error(`Project "${project.name}" already has a serve target. Pass --skipValidation to ignore this error.`);
|
|
31
35
|
}
|
|
32
36
|
}
|
|
33
|
-
function
|
|
37
|
+
function createWebpackConfig(tree, options) {
|
|
34
38
|
const project = (0, devkit_1.readProjectConfiguration)(tree, options.project);
|
|
35
39
|
const buildOptions = {
|
|
36
40
|
target: options.target,
|
|
@@ -40,20 +44,26 @@ function addBuildTarget(tree, options) {
|
|
|
40
44
|
tsConfig: options.tsConfig ?? (0, devkit_1.joinPathFragments)(project.root, 'tsconfig.app.json'),
|
|
41
45
|
webpackConfig: (0, devkit_1.joinPathFragments)(project.root, 'webpack.config.js'),
|
|
42
46
|
};
|
|
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
|
-
}
|
|
55
47
|
if (options.target === 'web') {
|
|
56
|
-
tree.write((0, devkit_1.joinPathFragments)(project.root, 'webpack.config.js'),
|
|
48
|
+
tree.write((0, devkit_1.joinPathFragments)(project.root, 'webpack.config.js'), (0, has_plugin_1.hasPlugin)(tree)
|
|
49
|
+
? `
|
|
50
|
+
const { NxWebpackPlugin } = require('@nx/webpack');
|
|
51
|
+
|
|
52
|
+
module.exports = {
|
|
53
|
+
output: {
|
|
54
|
+
path: '${buildOptions.outputPath}',
|
|
55
|
+
},
|
|
56
|
+
plugins: [
|
|
57
|
+
new NxWebpackPlugin({
|
|
58
|
+
target: '${buildOptions.target}',
|
|
59
|
+
tsConfig: '${buildOptions.tsConfig}',
|
|
60
|
+
compiler: '${buildOptions.compiler}',
|
|
61
|
+
main: '${buildOptions.main}',
|
|
62
|
+
})
|
|
63
|
+
],
|
|
64
|
+
}
|
|
65
|
+
`
|
|
66
|
+
: `
|
|
57
67
|
const { composePlugins, withNx, withWeb } = require('@nx/webpack');
|
|
58
68
|
|
|
59
69
|
// Nx plugins for webpack.
|
|
@@ -65,7 +75,25 @@ module.exports = composePlugins(withNx(), withWeb(), (config) => {
|
|
|
65
75
|
`);
|
|
66
76
|
}
|
|
67
77
|
else {
|
|
68
|
-
tree.write((0, devkit_1.joinPathFragments)(project.root, 'webpack.config.js'),
|
|
78
|
+
tree.write((0, devkit_1.joinPathFragments)(project.root, 'webpack.config.js'), (0, has_plugin_1.hasPlugin)(tree)
|
|
79
|
+
? `
|
|
80
|
+
const { NxWebpackPlugin } = require('@nx/webpack');
|
|
81
|
+
|
|
82
|
+
module.exports = {
|
|
83
|
+
output: {
|
|
84
|
+
path: '${buildOptions.outputPath}',
|
|
85
|
+
},
|
|
86
|
+
plugins: [
|
|
87
|
+
new NxWebpackPlugin({
|
|
88
|
+
target: '${buildOptions.target}',
|
|
89
|
+
tsConfig: '${buildOptions.tsConfig}',
|
|
90
|
+
compiler: '${buildOptions.compiler}',
|
|
91
|
+
main: '${buildOptions.main}',
|
|
92
|
+
})
|
|
93
|
+
],
|
|
94
|
+
}
|
|
95
|
+
`
|
|
96
|
+
: `
|
|
69
97
|
const { composePlugins, withNx } = require('@nx/webpack');
|
|
70
98
|
|
|
71
99
|
// Nx plugins for webpack.
|
|
@@ -76,6 +104,29 @@ module.exports = composePlugins(withNx(), (config) => {
|
|
|
76
104
|
});
|
|
77
105
|
`);
|
|
78
106
|
}
|
|
107
|
+
}
|
|
108
|
+
function addBuildTarget(tree, options) {
|
|
109
|
+
const project = (0, devkit_1.readProjectConfiguration)(tree, options.project);
|
|
110
|
+
const buildOptions = {
|
|
111
|
+
target: options.target,
|
|
112
|
+
outputPath: (0, devkit_1.joinPathFragments)('dist', project.root),
|
|
113
|
+
compiler: options.compiler ?? 'swc',
|
|
114
|
+
main: options.main ?? (0, devkit_1.joinPathFragments)(project.root, 'src/main.ts'),
|
|
115
|
+
tsConfig: options.tsConfig ?? (0, devkit_1.joinPathFragments)(project.root, 'tsconfig.app.json'),
|
|
116
|
+
webpackConfig: (0, devkit_1.joinPathFragments)(project.root, 'webpack.config.js'),
|
|
117
|
+
};
|
|
118
|
+
if (options.webpackConfig) {
|
|
119
|
+
buildOptions.webpackConfig = options.webpackConfig;
|
|
120
|
+
}
|
|
121
|
+
if (options.babelConfig) {
|
|
122
|
+
buildOptions.babelConfig = options.babelConfig;
|
|
123
|
+
}
|
|
124
|
+
else if (options.compiler === 'babel') {
|
|
125
|
+
// If no babel config file is provided then write a default one, otherwise build will fail.
|
|
126
|
+
(0, devkit_1.writeJson)(tree, (0, devkit_1.joinPathFragments)(project.root, '.babelrc'), {
|
|
127
|
+
presets: ['@nx/js/babel'],
|
|
128
|
+
});
|
|
129
|
+
}
|
|
79
130
|
(0, devkit_1.updateProjectConfiguration)(tree, options.project, {
|
|
80
131
|
...project,
|
|
81
132
|
targets: {
|
|
@@ -5,10 +5,14 @@ const devkit_1 = require("@nx/devkit");
|
|
|
5
5
|
const add_swc_dependencies_1 = require("@nx/js/src/utils/swc/add-swc-dependencies");
|
|
6
6
|
const versions_1 = require("../../utils/versions");
|
|
7
7
|
async function webpackInitGenerator(tree, schema) {
|
|
8
|
+
const shouldAddPlugin = process.env.NX_PCV3 === 'true';
|
|
8
9
|
const tasks = [];
|
|
9
10
|
const devDependencies = {
|
|
10
11
|
'@nx/webpack': versions_1.nxVersion,
|
|
11
12
|
};
|
|
13
|
+
if (shouldAddPlugin) {
|
|
14
|
+
devDependencies['webpack-cli'] = versions_1.webpackCliVersion;
|
|
15
|
+
}
|
|
12
16
|
if (schema.compiler === 'swc') {
|
|
13
17
|
devDependencies['swc-loader'] = versions_1.swcLoaderVersion;
|
|
14
18
|
const addSwcTask = (0, add_swc_dependencies_1.addSwcDependencies)(tree);
|
|
@@ -27,9 +31,31 @@ async function webpackInitGenerator(tree, schema) {
|
|
|
27
31
|
if (!schema.skipFormat) {
|
|
28
32
|
await (0, devkit_1.formatFiles)(tree);
|
|
29
33
|
}
|
|
30
|
-
const
|
|
31
|
-
tasks.push(
|
|
34
|
+
const baseInstallTask = (0, devkit_1.addDependenciesToPackageJson)(tree, {}, devDependencies);
|
|
35
|
+
tasks.push(baseInstallTask);
|
|
36
|
+
if (shouldAddPlugin)
|
|
37
|
+
addPlugin(tree);
|
|
32
38
|
return (0, devkit_1.runTasksInSerial)(...tasks);
|
|
33
39
|
}
|
|
34
40
|
exports.webpackInitGenerator = webpackInitGenerator;
|
|
41
|
+
function addPlugin(tree) {
|
|
42
|
+
const nxJson = (0, devkit_1.readNxJson)(tree);
|
|
43
|
+
nxJson.plugins ??= [];
|
|
44
|
+
for (const plugin of nxJson.plugins) {
|
|
45
|
+
if (typeof plugin === 'string'
|
|
46
|
+
? plugin === '@nx/webpack/plugin'
|
|
47
|
+
: plugin.plugin === '@nx/webpack/plugin') {
|
|
48
|
+
return;
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
nxJson.plugins.push({
|
|
52
|
+
plugin: '@nx/webpack/plugin',
|
|
53
|
+
options: {
|
|
54
|
+
buildTargetName: 'build',
|
|
55
|
+
serveTargetName: 'serve',
|
|
56
|
+
previewTargetName: 'preview',
|
|
57
|
+
},
|
|
58
|
+
});
|
|
59
|
+
(0, devkit_1.updateNxJson)(tree, nxJson);
|
|
60
|
+
}
|
|
35
61
|
exports.default = webpackInitGenerator;
|
|
@@ -5,6 +5,7 @@ const path = require("path");
|
|
|
5
5
|
const license_webpack_plugin_1 = require("license-webpack-plugin");
|
|
6
6
|
const CopyWebpackPlugin = require("copy-webpack-plugin");
|
|
7
7
|
const webpack_1 = require("webpack");
|
|
8
|
+
const js_1 = require("@nx/js");
|
|
8
9
|
const stats_json_plugin_1 = require("../../stats-json-plugin");
|
|
9
10
|
const generate_package_json_plugin_1 = require("../../generate-package-json-plugin");
|
|
10
11
|
const hash_format_1 = require("../../../utils/hash-format");
|
|
@@ -21,8 +22,143 @@ const IGNORED_WEBPACK_WARNINGS = [
|
|
|
21
22
|
const extensions = ['.ts', '.tsx', '.mjs', '.js', '.jsx'];
|
|
22
23
|
const mainFields = ['module', 'main'];
|
|
23
24
|
function applyBaseConfig(options, config = {}, { useNormalizedEntry, } = {}) {
|
|
25
|
+
// Defaults that was applied from executor schema previously.
|
|
26
|
+
options.compiler ??= 'babel';
|
|
27
|
+
options.deleteOutputPath ??= true;
|
|
28
|
+
options.externalDependencies ??= 'all';
|
|
29
|
+
options.fileReplacements ??= [];
|
|
30
|
+
options.memoryLimit ??= 2048;
|
|
31
|
+
options.transformers ??= [];
|
|
32
|
+
applyNxIndependentConfig(options, config);
|
|
33
|
+
// Some of the options only work during actual tasks, not when reading the webpack config during CreateNodes.
|
|
34
|
+
if (!process.env['NX_TASK_TARGET_PROJECT'])
|
|
35
|
+
return;
|
|
36
|
+
applyNxDependentConfig(options, config, { useNormalizedEntry });
|
|
37
|
+
}
|
|
38
|
+
exports.applyBaseConfig = applyBaseConfig;
|
|
39
|
+
function applyNxIndependentConfig(options, config) {
|
|
40
|
+
const hashFormat = (0, hash_format_1.getOutputHashFormat)(options.outputHashing);
|
|
41
|
+
config.context = path.join(options.root, options.projectRoot);
|
|
42
|
+
config.target ??= options.target;
|
|
43
|
+
config.node = false;
|
|
44
|
+
config.mode =
|
|
45
|
+
// When the target is Node avoid any optimizations, such as replacing `process.env.NODE_ENV` with build time value.
|
|
46
|
+
config.target === 'node'
|
|
47
|
+
? 'none'
|
|
48
|
+
: // Otherwise, make sure it matches `process.env.NODE_ENV`.
|
|
49
|
+
// When mode is development or production, webpack will automatically
|
|
50
|
+
// configure DefinePlugin to replace `process.env.NODE_ENV` with the
|
|
51
|
+
// build-time value. Thus, we need to make sure it's the same value to
|
|
52
|
+
// avoid conflicts.
|
|
53
|
+
//
|
|
54
|
+
// When the NODE_ENV is something else (e.g. test), then set it to none
|
|
55
|
+
// to prevent extra behavior from webpack.
|
|
56
|
+
process.env.NODE_ENV === 'development' ||
|
|
57
|
+
process.env.NODE_ENV === 'production'
|
|
58
|
+
? process.env.NODE_ENV
|
|
59
|
+
: 'none';
|
|
60
|
+
// 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.
|
|
61
|
+
// So to mitigate this we enable in memory caching when target is Node and in watch mode.
|
|
62
|
+
config.cache =
|
|
63
|
+
options.target === 'node' && options.watch ? { type: 'memory' } : undefined;
|
|
64
|
+
config.devtool =
|
|
65
|
+
options.sourceMap === 'hidden'
|
|
66
|
+
? 'hidden-source-map'
|
|
67
|
+
: options.sourceMap
|
|
68
|
+
? 'source-map'
|
|
69
|
+
: false;
|
|
70
|
+
config.output = {
|
|
71
|
+
...config.output,
|
|
72
|
+
path: config.output?.path ??
|
|
73
|
+
(options.outputPath
|
|
74
|
+
? path.join(options.root, options.outputPath)
|
|
75
|
+
: undefined),
|
|
76
|
+
filename: config.output?.filename ?? options.outputHashing
|
|
77
|
+
? `[name]${hashFormat.script}.js`
|
|
78
|
+
: '[name].js',
|
|
79
|
+
chunkFilename: config.output?.chunkFilename ?? options.outputHashing
|
|
80
|
+
? `[name]${hashFormat.chunk}.js`
|
|
81
|
+
: '[name].js',
|
|
82
|
+
hashFunction: config.output?.hashFunction ?? 'xxhash64',
|
|
83
|
+
// Disabled for performance
|
|
84
|
+
pathinfo: config.output?.pathinfo ?? false,
|
|
85
|
+
// Use CJS for Node since it has the widest support.
|
|
86
|
+
scriptType: config.output?.scriptType ?? options.target === 'node'
|
|
87
|
+
? undefined
|
|
88
|
+
: 'module',
|
|
89
|
+
};
|
|
90
|
+
config.watch = options.watch;
|
|
91
|
+
config.watchOptions = {
|
|
92
|
+
poll: options.poll,
|
|
93
|
+
};
|
|
94
|
+
config.profile = options.statsJson;
|
|
95
|
+
config.performance = {
|
|
96
|
+
...config.performance,
|
|
97
|
+
hints: false,
|
|
98
|
+
};
|
|
99
|
+
config.experiments = { ...config.experiments, cacheUnaffected: true };
|
|
100
|
+
config.ignoreWarnings = [
|
|
101
|
+
(x) => IGNORED_WEBPACK_WARNINGS.some((r) => typeof x === 'string' ? r.test(x) : r.test(x.message)),
|
|
102
|
+
];
|
|
103
|
+
config.optimization = {
|
|
104
|
+
...config.optimization,
|
|
105
|
+
sideEffects: true,
|
|
106
|
+
minimize: typeof options.optimization === 'object'
|
|
107
|
+
? !!options.optimization.scripts
|
|
108
|
+
: !!options.optimization,
|
|
109
|
+
minimizer: [
|
|
110
|
+
options.compiler !== 'swc'
|
|
111
|
+
? new TerserPlugin({
|
|
112
|
+
parallel: true,
|
|
113
|
+
terserOptions: {
|
|
114
|
+
keep_classnames: true,
|
|
115
|
+
ecma: (0, get_terser_ecma_version_1.getTerserEcmaVersion)(path.join(options.root, options.projectRoot)),
|
|
116
|
+
safari10: true,
|
|
117
|
+
format: {
|
|
118
|
+
ascii_only: true,
|
|
119
|
+
comments: false,
|
|
120
|
+
webkit: true,
|
|
121
|
+
},
|
|
122
|
+
},
|
|
123
|
+
extractComments: false,
|
|
124
|
+
})
|
|
125
|
+
: new TerserPlugin({
|
|
126
|
+
minify: TerserPlugin.swcMinify,
|
|
127
|
+
// `terserOptions` options will be passed to `swc`
|
|
128
|
+
terserOptions: {
|
|
129
|
+
module: true,
|
|
130
|
+
mangle: false,
|
|
131
|
+
},
|
|
132
|
+
}),
|
|
133
|
+
],
|
|
134
|
+
runtimeChunk: false,
|
|
135
|
+
concatenateModules: true,
|
|
136
|
+
};
|
|
137
|
+
config.stats = {
|
|
138
|
+
hash: true,
|
|
139
|
+
timings: false,
|
|
140
|
+
cached: false,
|
|
141
|
+
cachedAssets: false,
|
|
142
|
+
modules: false,
|
|
143
|
+
warnings: true,
|
|
144
|
+
errors: true,
|
|
145
|
+
colors: !options.verbose && !options.statsJson,
|
|
146
|
+
chunks: !options.verbose,
|
|
147
|
+
assets: !!options.verbose,
|
|
148
|
+
chunkOrigins: !!options.verbose,
|
|
149
|
+
chunkModules: !!options.verbose,
|
|
150
|
+
children: !!options.verbose,
|
|
151
|
+
reasons: !!options.verbose,
|
|
152
|
+
version: !!options.verbose,
|
|
153
|
+
errorDetails: !!options.verbose,
|
|
154
|
+
moduleTrace: !!options.verbose,
|
|
155
|
+
usedExports: !!options.verbose,
|
|
156
|
+
};
|
|
157
|
+
}
|
|
158
|
+
function applyNxDependentConfig(options, config, { useNormalizedEntry } = {}) {
|
|
159
|
+
const tsConfig = options.tsConfig ?? (0, js_1.getRootTsConfigPath)();
|
|
24
160
|
const plugins = [
|
|
25
|
-
new nx_tsconfig_paths_webpack_plugin_1.NxTsconfigPathsWebpackPlugin(
|
|
161
|
+
new nx_tsconfig_paths_webpack_plugin_1.NxTsconfigPathsWebpackPlugin({ tsConfig }),
|
|
26
162
|
];
|
|
27
163
|
const executorContext = {
|
|
28
164
|
projectName: options.projectName,
|
|
@@ -34,9 +170,9 @@ function applyBaseConfig(options, config = {}, { useNormalizedEntry, } = {}) {
|
|
|
34
170
|
if (!options?.skipTypeChecking) {
|
|
35
171
|
plugins.push(new ForkTsCheckerWebpackPlugin({
|
|
36
172
|
typescript: {
|
|
37
|
-
configFile: path.isAbsolute(
|
|
38
|
-
?
|
|
39
|
-
: path.join(options.root,
|
|
173
|
+
configFile: path.isAbsolute(tsConfig)
|
|
174
|
+
? tsConfig
|
|
175
|
+
: path.join(options.root, tsConfig),
|
|
40
176
|
memoryLimit: options.memoryLimit || 2018,
|
|
41
177
|
},
|
|
42
178
|
}));
|
|
@@ -109,7 +245,7 @@ function applyBaseConfig(options, config = {}, { useNormalizedEntry, } = {}) {
|
|
|
109
245
|
}));
|
|
110
246
|
}
|
|
111
247
|
if (options.generatePackageJson && executorContext) {
|
|
112
|
-
plugins.push(new generate_package_json_plugin_1.GeneratePackageJsonPlugin(options));
|
|
248
|
+
plugins.push(new generate_package_json_plugin_1.GeneratePackageJsonPlugin({ ...options, tsConfig }));
|
|
113
249
|
}
|
|
114
250
|
if (options.statsJson) {
|
|
115
251
|
plugins.push(new stats_json_plugin_1.StatsJsonPlugin());
|
|
@@ -129,113 +265,17 @@ function applyBaseConfig(options, config = {}, { useNormalizedEntry, } = {}) {
|
|
|
129
265
|
callback();
|
|
130
266
|
});
|
|
131
267
|
}
|
|
132
|
-
const hashFormat = (0, hash_format_1.getOutputHashFormat)(options.outputHashing);
|
|
133
|
-
config.context = path.join(options.root, options.projectRoot);
|
|
134
|
-
config.target ??= options.target;
|
|
135
|
-
config.node = false;
|
|
136
|
-
config.mode =
|
|
137
|
-
// When the target is Node avoid any optimizations, such as replacing `process.env.NODE_ENV` with build time value.
|
|
138
|
-
config.target === 'node'
|
|
139
|
-
? 'none'
|
|
140
|
-
: // Otherwise, make sure it matches `process.env.NODE_ENV`.
|
|
141
|
-
// When mode is development or production, webpack will automatically
|
|
142
|
-
// configure DefinePlugin to replace `process.env.NODE_ENV` with the
|
|
143
|
-
// build-time value. Thus, we need to make sure it's the same value to
|
|
144
|
-
// avoid conflicts.
|
|
145
|
-
//
|
|
146
|
-
// When the NODE_ENV is something else (e.g. test), then set it to none
|
|
147
|
-
// to prevent extra behavior from webpack.
|
|
148
|
-
process.env.NODE_ENV === 'development' ||
|
|
149
|
-
process.env.NODE_ENV === 'production'
|
|
150
|
-
? process.env.NODE_ENV
|
|
151
|
-
: 'none';
|
|
152
|
-
// 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.
|
|
153
|
-
// So to mitigate this we enable in memory caching when target is Node and in watch mode.
|
|
154
|
-
config.cache =
|
|
155
|
-
options.target === 'node' && options.watch ? { type: 'memory' } : undefined;
|
|
156
|
-
config.devtool =
|
|
157
|
-
options.sourceMap === 'hidden'
|
|
158
|
-
? 'hidden-source-map'
|
|
159
|
-
: options.sourceMap
|
|
160
|
-
? 'source-map'
|
|
161
|
-
: false;
|
|
162
|
-
config.output = {
|
|
163
|
-
...config.output,
|
|
164
|
-
path: config.output?.path ??
|
|
165
|
-
(options.outputPath
|
|
166
|
-
? path.join(options.root, options.outputPath)
|
|
167
|
-
: undefined),
|
|
168
|
-
filename: config.output?.filename ?? options.outputHashing
|
|
169
|
-
? `[name]${hashFormat.script}.js`
|
|
170
|
-
: '[name].js',
|
|
171
|
-
chunkFilename: config.output?.chunkFilename ?? options.outputHashing
|
|
172
|
-
? `[name]${hashFormat.chunk}.js`
|
|
173
|
-
: '[name].js',
|
|
174
|
-
hashFunction: config.output?.hashFunction ?? 'xxhash64',
|
|
175
|
-
// Disabled for performance
|
|
176
|
-
pathinfo: config.output?.pathinfo ?? false,
|
|
177
|
-
// Use CJS for Node since it has the widest support.
|
|
178
|
-
scriptType: config.output?.scriptType ?? options.target === 'node'
|
|
179
|
-
? undefined
|
|
180
|
-
: 'module',
|
|
181
|
-
};
|
|
182
|
-
config.watch = options.watch;
|
|
183
|
-
config.watchOptions = {
|
|
184
|
-
poll: options.poll,
|
|
185
|
-
};
|
|
186
|
-
config.profile = options.statsJson;
|
|
187
268
|
config.resolve = {
|
|
188
269
|
...config.resolve,
|
|
189
270
|
extensions: [...extensions, ...(config?.resolve?.extensions ?? [])],
|
|
190
|
-
alias: options.fileReplacements
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
271
|
+
alias: options.fileReplacements &&
|
|
272
|
+
options.fileReplacements.reduce((aliases, replacement) => ({
|
|
273
|
+
...aliases,
|
|
274
|
+
[replacement.replace]: replacement.with,
|
|
275
|
+
}), {}),
|
|
194
276
|
mainFields,
|
|
195
277
|
};
|
|
196
278
|
config.externals = externals;
|
|
197
|
-
config.optimization = {
|
|
198
|
-
...config.optimization,
|
|
199
|
-
sideEffects: true,
|
|
200
|
-
minimize: typeof options.optimization === 'object'
|
|
201
|
-
? !!options.optimization.scripts
|
|
202
|
-
: !!options.optimization,
|
|
203
|
-
minimizer: [
|
|
204
|
-
options.compiler !== 'swc'
|
|
205
|
-
? new TerserPlugin({
|
|
206
|
-
parallel: true,
|
|
207
|
-
terserOptions: {
|
|
208
|
-
keep_classnames: true,
|
|
209
|
-
ecma: (0, get_terser_ecma_version_1.getTerserEcmaVersion)(path.join(options.root, options.projectRoot)),
|
|
210
|
-
safari10: true,
|
|
211
|
-
format: {
|
|
212
|
-
ascii_only: true,
|
|
213
|
-
comments: false,
|
|
214
|
-
webkit: true,
|
|
215
|
-
},
|
|
216
|
-
},
|
|
217
|
-
extractComments: false,
|
|
218
|
-
})
|
|
219
|
-
: new TerserPlugin({
|
|
220
|
-
minify: TerserPlugin.swcMinify,
|
|
221
|
-
// `terserOptions` options will be passed to `swc`
|
|
222
|
-
terserOptions: {
|
|
223
|
-
module: true,
|
|
224
|
-
mangle: false,
|
|
225
|
-
},
|
|
226
|
-
}),
|
|
227
|
-
],
|
|
228
|
-
runtimeChunk: false,
|
|
229
|
-
concatenateModules: true,
|
|
230
|
-
};
|
|
231
|
-
config.performance = {
|
|
232
|
-
...config.performance,
|
|
233
|
-
hints: false,
|
|
234
|
-
};
|
|
235
|
-
config.experiments = { ...config.experiments, cacheUnaffected: true };
|
|
236
|
-
config.ignoreWarnings = [
|
|
237
|
-
(x) => IGNORED_WEBPACK_WARNINGS.some((r) => typeof x === 'string' ? r.test(x) : r.test(x.message)),
|
|
238
|
-
];
|
|
239
279
|
config.module = {
|
|
240
280
|
...config.module,
|
|
241
281
|
// Enabled for performance
|
|
@@ -267,27 +307,6 @@ function applyBaseConfig(options, config = {}, { useNormalizedEntry, } = {}) {
|
|
|
267
307
|
(0, compiler_loaders_1.createLoaderFromCompiler)(options),
|
|
268
308
|
].filter((r) => !!r),
|
|
269
309
|
};
|
|
270
|
-
config.stats = {
|
|
271
|
-
hash: true,
|
|
272
|
-
timings: false,
|
|
273
|
-
cached: false,
|
|
274
|
-
cachedAssets: false,
|
|
275
|
-
modules: false,
|
|
276
|
-
warnings: true,
|
|
277
|
-
errors: true,
|
|
278
|
-
colors: !options.verbose && !options.statsJson,
|
|
279
|
-
chunks: !options.verbose,
|
|
280
|
-
assets: !!options.verbose,
|
|
281
|
-
chunkOrigins: !!options.verbose,
|
|
282
|
-
chunkModules: !!options.verbose,
|
|
283
|
-
children: !!options.verbose,
|
|
284
|
-
reasons: !!options.verbose,
|
|
285
|
-
version: !!options.verbose,
|
|
286
|
-
errorDetails: !!options.verbose,
|
|
287
|
-
moduleTrace: !!options.verbose,
|
|
288
|
-
usedExports: !!options.verbose,
|
|
289
|
-
};
|
|
290
310
|
config.plugins ??= [];
|
|
291
311
|
config.plugins.push(...plugins);
|
|
292
312
|
}
|
|
293
|
-
exports.applyBaseConfig = applyBaseConfig;
|
|
@@ -13,6 +13,14 @@ const instantiate_script_plugins_1 = require("./instantiate-script-plugins");
|
|
|
13
13
|
const CssMinimizerPlugin = require("css-minimizer-webpack-plugin");
|
|
14
14
|
const MiniCssExtractPlugin = require("mini-css-extract-plugin");
|
|
15
15
|
function applyWebConfig(options, config = {}, { useNormalizedEntry, } = {}) {
|
|
16
|
+
if (!process.env['NX_TASK_TARGET_PROJECT'])
|
|
17
|
+
return;
|
|
18
|
+
// Defaults that was applied from executor schema previously.
|
|
19
|
+
options.runtimeChunk ??= true; // need this for HMR and other things to work
|
|
20
|
+
options.extractCss ??= true;
|
|
21
|
+
options.generateIndexHtml ??= true;
|
|
22
|
+
options.styles ??= [];
|
|
23
|
+
options.scripts ??= [];
|
|
16
24
|
const plugins = [];
|
|
17
25
|
const stylesOptimization = typeof options.optimization === 'object'
|
|
18
26
|
? options.optimization.styles
|
|
@@ -32,9 +32,11 @@ function normalizeOptions(options) {
|
|
|
32
32
|
else {
|
|
33
33
|
Object.assign(combinedOptions, originalTargetOptions, options);
|
|
34
34
|
}
|
|
35
|
+
normalizeRelativePaths(projectNode.data.root, options);
|
|
35
36
|
const sourceRoot = projectNode.data.sourceRoot ?? projectNode.data.root;
|
|
36
|
-
if (!options.main)
|
|
37
|
+
if (!options.main) {
|
|
37
38
|
throw new Error(`Missing "main" option for the entry file. Set this option in your Nx webpack plugin.`);
|
|
39
|
+
}
|
|
38
40
|
return {
|
|
39
41
|
...options,
|
|
40
42
|
assets: options.assets
|
|
@@ -115,3 +117,23 @@ function normalizeFileReplacements(root, fileReplacements) {
|
|
|
115
117
|
: [];
|
|
116
118
|
}
|
|
117
119
|
exports.normalizeFileReplacements = normalizeFileReplacements;
|
|
120
|
+
function normalizeRelativePaths(projectRoot, options) {
|
|
121
|
+
for (const [fieldName, fieldValue] of Object.entries(options)) {
|
|
122
|
+
if (isRelativePath(fieldValue)) {
|
|
123
|
+
options[fieldName] = (0, path_1.join)(projectRoot, fieldValue);
|
|
124
|
+
}
|
|
125
|
+
else if (Array.isArray(fieldValue)) {
|
|
126
|
+
for (let i = 0; i < fieldValue.length; i++) {
|
|
127
|
+
if (isRelativePath(fieldValue[i])) {
|
|
128
|
+
fieldValue[i] = (0, path_1.join)(projectRoot, fieldValue[i]);
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
function isRelativePath(val) {
|
|
135
|
+
return (typeof val === 'string' &&
|
|
136
|
+
(val.startsWith('./') ||
|
|
137
|
+
// Windows
|
|
138
|
+
val.startsWith('.\\')));
|
|
139
|
+
}
|