-
Notifications
You must be signed in to change notification settings - Fork 2.2k
/
config.ts
268 lines (243 loc) · 7.89 KB
/
config.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
import { join, resolve } from 'path';
import { LicenseWebpackPlugin } from 'license-webpack-plugin';
import { TsconfigPathsPlugin } from 'tsconfig-paths-webpack-plugin';
import { AssetGlobPattern, BuildBuilderOptions } from './types';
import { getOutputHashFormat } from './hash-format';
import CircularDependencyPlugin = require('circular-dependency-plugin');
import ForkTsCheckerWebpackPlugin = require('fork-ts-checker-webpack-plugin');
const IGNORED_WEBPACK_WARNINGS = [
/The comment file/i,
/could not find any license/i,
];
// TODO(jack): Remove this in Nx 13 and go back to proper types
type Configuration = any;
type WebpackPluginInstance = any;
export function getBaseWebpackPartial(
options: BuildBuilderOptions,
esm?: boolean,
isScriptOptimizeOn?: boolean,
emitDecoratorMetadata?: boolean,
configuration?: string
): Configuration {
// TODO(jack): Remove this in Nx 13 and go back to proper imports
const { webpack } = require('../webpack/entry');
const { ProgressPlugin } = webpack;
const extensions = ['.ts', '.tsx', '.mjs', '.js', '.jsx'];
const mainFields = [...(esm ? ['es2015'] : []), 'module', 'main'];
const hashFormat = getOutputHashFormat(options.outputHashing);
const suffixFormat = esm ? '.esm' : '.es5';
const filename = isScriptOptimizeOn
? `[name]${hashFormat.script}${suffixFormat}.js`
: '[name].js';
const chunkFilename = isScriptOptimizeOn
? `[name]${hashFormat.chunk}${suffixFormat}.js`
: '[name].js';
const mode = isScriptOptimizeOn ? 'production' : 'development';
const webpackConfig: Configuration = {
target: 'web', // webpack defaults to 'browserslist' which breaks Fast Refresh
entry: {
main: [options.main],
},
devtool: options.sourceMap ? 'source-map' : false,
mode,
output: {
path: options.outputPath,
filename,
chunkFilename,
},
module: {
rules: [
{
test: /\.([jt])sx?$/,
loader: join(__dirname, 'web-babel-loader'),
exclude: /node_modules/,
options: {
rootMode: 'upward',
cwd: join(options.root, options.sourceRoot),
emitDecoratorMetadata,
isModern: esm,
envName: isScriptOptimizeOn ? 'production' : configuration,
babelrc: true,
cacheDirectory: true,
cacheCompression: false,
},
},
],
},
resolve: {
extensions,
alias: getAliases(options),
plugins: [
// TODO Remove the never type when module is updated
// PR opened for the proper typing here; https://github.com/dividab/tsconfig-paths-webpack-plugin/pull/66
new TsconfigPathsPlugin({
configFile: options.tsConfig,
extensions,
mainFields,
}),
],
// Search closest node_modules first, and then fallback to to default node module resolution scheme.
// This ensures we are pulling the correct versions of dependencies, such as `core-js`.
modules: [resolve(__dirname, '..', '..', 'node_modules'), 'node_modules'],
mainFields,
},
performance: {
hints: false,
},
plugins: [new webpack.DefinePlugin(getClientEnvironment(mode).stringified)],
watch: options.watch,
watchOptions: {
poll: options.poll,
},
stats: getStatsConfig(options),
};
if (isScriptOptimizeOn) {
webpackConfig.optimization = {
minimizer: [createTerserPlugin(esm, !!options.sourceMap)],
runtimeChunk: true,
};
}
const extraPlugins: WebpackPluginInstance[] = [];
if (esm) {
extraPlugins.push(
new ForkTsCheckerWebpackPlugin({
typescript: {
enabled: true,
configFile: options.tsConfig,
memoryLimit: options.memoryLimit || 2018,
},
})
);
}
if (options.progress) {
extraPlugins.push(new ProgressPlugin());
}
// TODO LicenseWebpackPlugin needs a PR for proper typing
if (options.extractLicenses) {
extraPlugins.push(
new LicenseWebpackPlugin({
stats: {
errors: false,
},
perChunkOutput: false,
outputFilename: `3rdpartylicenses.txt`,
}) as unknown as WebpackPluginInstance
);
}
if (Array.isArray(options.assets) && options.assets.length > 0) {
extraPlugins.push(createCopyPlugin(options.assets));
}
if (options.showCircularDependencies) {
extraPlugins.push(
new CircularDependencyPlugin({
exclude: /[\\\/]node_modules[\\\/]/,
})
);
}
webpackConfig.plugins = [...webpackConfig.plugins, ...extraPlugins];
return webpackConfig;
}
function getAliases(options: BuildBuilderOptions): { [key: string]: string } {
return options.fileReplacements.reduce(
(aliases, replacement) => ({
...aliases,
[replacement.replace]: replacement.with,
}),
{}
);
}
// TODO Sourcemap and cache options have been removed from plugin.
// Investigate what this mgiht change in the build process
export function createTerserPlugin(esm: boolean, sourceMap: boolean) {
// TODO(jack): Remove this in Nx 13 and go back to proper imports
const { TerserWebpackPlugin } = require('../webpack/entry');
return new TerserWebpackPlugin({
parallel: true,
terserOptions: {
ecma: esm ? 8 : 5,
safari10: true,
output: {
ascii_only: true,
comments: false,
webkit: true,
},
},
});
}
// TODO(jack): Update the typing with new version of webpack -- was returning Stats.ToStringOptions in webpack 4
// The StatsOptions type needs to be exported from webpack
// PR: https://github.com/webpack/webpack/pull/12875
function getStatsConfig(options: BuildBuilderOptions): any {
const { isWebpack5 } = require('../webpack/entry');
return {
hash: true,
timings: false,
cached: false,
cachedAssets: false,
modules: false,
warnings: true,
errors: true,
colors: !options.verbose && !options.statsJson,
chunks: !options.verbose,
assets: !!options.verbose,
chunkOrigins: !!options.verbose,
chunkModules: !!options.verbose,
children: !!options.verbose,
reasons: !!options.verbose,
version: !!options.verbose,
errorDetails: !!options.verbose,
moduleTrace: !!options.verbose,
usedExports: !!options.verbose,
warningsFilter: IGNORED_WEBPACK_WARNINGS,
};
}
// This is shamelessly taken from CRA and modified for NX use
// https://github.com/facebook/create-react-app/blob/4784997f0682e75eb32a897b4ffe34d735912e6c/packages/react-scripts/config/env.js#L71
function getClientEnvironment(mode) {
// Grab NODE_ENV and NX_* environment variables and prepare them to be
// injected into the application via DefinePlugin in webpack configuration.
const NX_APP = /^NX_/i;
const raw = Object.keys(process.env)
.filter((key) => NX_APP.test(key))
.reduce(
(env, key) => {
env[key] = process.env[key];
return env;
},
{
// Useful for determining whether we’re running in production mode.
NODE_ENV: process.env.NODE_ENV || mode,
}
);
// Stringify all values so we can feed into webpack DefinePlugin
const stringified = {
'process.env': Object.keys(raw).reduce((env, key) => {
env[key] = JSON.stringify(raw[key]);
return env;
}, {}),
};
return { stringified };
}
export function createCopyPlugin(assets: AssetGlobPattern[]) {
// TODO(jack): Remove this in Nx 13 and go back to proper imports
const { CopyWebpackPlugin } = require('../webpack/entry');
return new CopyWebpackPlugin({
patterns: assets.map((asset) => {
return {
context: asset.input,
// Now we remove starting slash to make Webpack place it from the output root.
to: asset.output,
from: asset.glob,
globOptions: {
ignore: [
'.gitkeep',
'**/.DS_Store',
'**/Thumbs.db',
...(asset.ignore ?? []),
],
dot: true,
},
};
}),
});
}