-
Notifications
You must be signed in to change notification settings - Fork 2.2k
/
web.config.ts
301 lines (283 loc) · 8.32 KB
/
web.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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
import * as path from 'path';
import { posix, resolve } from 'path';
import { readTsConfig } from '@nrwl/workspace/src/utilities/typescript';
import { ScriptTarget } from 'typescript';
import { getHashDigest, interpolateName } from 'loader-utils';
import { Configuration } from 'webpack';
import { WebWebpackExecutorOptions } from '../executors/webpack/webpack.impl';
import { convertBuildOptions } from './normalize';
// TODO(jack): These should be inlined in a single function so it is easier to understand
import { getBaseWebpackPartial } from './config';
import { getBrowserConfig } from './webpack/partials/browser';
import { getCommonConfig } from './webpack/partials/common';
import { getStylesConfig } from './webpack/partials/styles';
import MiniCssExtractPlugin = require('mini-css-extract-plugin');
import webpackMerge = require('webpack-merge');
import postcssImports = require('postcss-import');
// PostCSS options depend on the webpack loader, but we need to set the `config` path as a string due to this check:
// https://github.com/webpack-contrib/postcss-loader/blob/0d342b1/src/utils.js#L36
interface PostcssOptions {
(loader: any): any;
config?: string;
}
export function getWebConfig(
workspaceRoot,
projectRoot,
sourceRoot,
options: WebWebpackExecutorOptions,
esm?: boolean,
isScriptOptimizeOn?: boolean,
configuration?: string
) {
const tsConfig = readTsConfig(options.tsConfig);
if (isScriptOptimizeOn) {
// Angular CLI uses an environment variable (NG_BUILD_DIFFERENTIAL_FULL)
// to determine whether to use the scriptTargetOverride
// or the tsConfig target
// We want to force the target if overridden
tsConfig.options.target = ScriptTarget.ES5;
}
const wco: any = {
root: workspaceRoot,
projectRoot: resolve(workspaceRoot, projectRoot),
sourceRoot: resolve(workspaceRoot, sourceRoot),
buildOptions: convertBuildOptions(options),
esm,
console,
tsConfig,
tsConfigPath: options.tsConfig,
};
// TODO(jack): Replace merge behavior with an inlined config so it is easier to understand.
return webpackMerge.merge([
_getBaseWebpackPartial(
options,
esm,
isScriptOptimizeOn,
tsConfig.options.emitDecoratorMetadata,
configuration
),
getPolyfillsPartial(
options.polyfills,
options.es2015Polyfills,
esm,
isScriptOptimizeOn
),
getStylesPartial(
wco.root,
wco.projectRoot,
wco.buildOptions,
options.extractCss,
options.postcssConfig
),
getCommonPartial(wco),
getBrowserConfig(wco),
]);
}
function _getBaseWebpackPartial(
options: WebWebpackExecutorOptions,
esm: boolean,
isScriptOptimizeOn: boolean,
emitDecoratorMetadata: boolean,
configuration?: string
) {
let partial = getBaseWebpackPartial(options, {
esm,
isScriptOptimizeOn,
emitDecoratorMetadata,
configuration,
});
delete partial.resolve.mainFields;
return partial;
}
function getCommonPartial(wco: any): Configuration {
const commonConfig: Configuration = <Configuration>getCommonConfig(wco);
delete commonConfig.entry;
delete commonConfig.resolve.modules;
delete commonConfig.resolve.extensions;
delete commonConfig.output.path;
delete commonConfig.module;
return commonConfig;
}
export function getStylesPartial(
workspaceRoot: string,
projectRoot: string,
options: any,
extractCss: boolean,
postcssConfig?: string
): Configuration {
const includePaths: string[] = [];
if (options?.stylePreprocessorOptions?.includePaths?.length > 0) {
options.stylePreprocessorOptions.includePaths.forEach(
(includePath: string) =>
includePaths.push(path.resolve(workspaceRoot, includePath))
);
}
const partial = getStylesConfig(workspaceRoot, options, includePaths);
const rules = partial.module.rules.map((rule) => {
if (!Array.isArray(rule.use)) {
return rule;
}
rule.use = rule.use.map((loaderConfig) => {
if (
typeof loaderConfig === 'object' &&
loaderConfig.loader === require.resolve('raw-loader')
) {
return {
loader: require.resolve('style-loader'),
};
}
return loaderConfig;
});
return rule;
});
const loaderModulesOptions = {
modules: {
mode: 'local',
getLocalIdent: getCSSModuleLocalIdent,
},
importLoaders: 1,
};
const postcssOptions: PostcssOptions = () => ({
plugins: [
postcssImports({
addModulesDirectories: includePaths,
resolve: (url: string) => (url.startsWith('~') ? url.slice(1) : url),
}),
],
});
// If a path to postcssConfig is passed in, set it for app and all libs, otherwise
// use automatic detection.
if (typeof postcssConfig === 'string') {
postcssOptions.config = path.join(workspaceRoot, postcssConfig);
}
const commonLoaders = [
{
loader: extractCss
? MiniCssExtractPlugin.loader
: require.resolve('style-loader'),
},
{
loader: require.resolve('css-loader'),
options: loaderModulesOptions,
},
{
loader: require.resolve('postcss-loader'),
options: {
implementation: require('postcss'),
postcssOptions: postcssOptions,
},
},
];
partial.module.rules = [
{
test: /\.css$|\.scss$|\.sass$|\.less$|\.styl$/,
oneOf: [
{
test: /\.module\.css$/,
use: commonLoaders,
},
{
test: /\.module\.(scss|sass)$/,
use: [
...commonLoaders,
{
loader: require.resolve('sass-loader'),
options: {
implementation: require('sass'),
sassOptions: {
fiber: false,
precision: 8,
includePaths,
},
},
},
],
},
{
test: /\.module\.less$/,
use: [
...commonLoaders,
{
loader: require.resolve('less-loader'),
options: {
lessOptions: {
paths: includePaths,
},
},
},
],
},
{
test: /\.module\.styl$/,
use: [
...commonLoaders,
{
loader: require.resolve('stylus-loader'),
options: {
stylusOptions: {
include: includePaths,
},
},
},
],
},
...rules,
],
},
];
return partial;
}
export function getPolyfillsPartial(
polyfills: string,
es2015Polyfills: string,
esm: boolean,
isScriptOptimizeOn: boolean
): Configuration {
const config = {
entry: {} as { [key: string]: string[] },
};
if (polyfills && esm && isScriptOptimizeOn) {
// Safari 10.1 supports <script type="module"> but not <script nomodule>.
// Need to patch it up so the browser doesn't load both sets.
config.entry.polyfills = [
require.resolve('@nrwl/web/src/utils/webpack/safari-nomodule.js'),
...(polyfills ? [polyfills] : []),
];
} else if (es2015Polyfills && !esm && isScriptOptimizeOn) {
config.entry.polyfills = [
es2015Polyfills,
...(polyfills ? [polyfills] : []),
];
} else {
if (polyfills) {
config.entry.polyfills = [polyfills];
}
if (es2015Polyfills) {
config.entry['polyfills-es5'] = [es2015Polyfills];
}
}
return config;
}
function getCSSModuleLocalIdent(context, localIdentName, localName, options) {
// Use the filename or folder name, based on some uses the index.js / index.module.(css|scss|sass) project style
const fileNameOrFolder = context.resourcePath.match(
/index\.module\.(css|scss|sass|styl)$/
)
? '[folder]'
: '[name]';
// Create a hash based on a the file location and class name. Will be unique across a project, and close to globally unique.
const hash = getHashDigest(
posix.relative(context.rootContext, context.resourcePath) + localName,
'md5',
'base64',
5
);
// Use loaderUtils to find the file or folder name
const className = interpolateName(
context,
`${fileNameOrFolder}_${localName}__${hash}`,
options
);
// Remove the .module that appears in every classname when based on the file and replace all "." with "_".
return className.replace('.module_', '_').replace(/\./g, '_');
}