-
Notifications
You must be signed in to change notification settings - Fork 12k
/
Copy pathhelpers.ts
339 lines (300 loc) · 9.73 KB
/
helpers.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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
/**
* @license
* Copyright Google LLC All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/
import type { ObjectPattern } from 'copy-webpack-plugin';
import { createHash } from 'crypto';
import { existsSync } from 'fs';
import glob from 'glob';
import * as path from 'path';
import { ScriptTarget } from 'typescript';
import type { Configuration, WebpackOptionsNormalized } from 'webpack';
import {
AssetPatternClass,
OutputHashing,
ScriptElement,
StyleElement,
} from '../../builders/browser/schema';
import { WebpackConfigOptions } from '../../utils/build-options';
import { VERSION } from '../../utils/package-version';
export interface HashFormat {
chunk: string;
extract: string;
file: string;
script: string;
}
export type WebpackStatsOptions = Exclude<Configuration['stats'], string | boolean | undefined>;
export function getOutputHashFormat(outputHashing = OutputHashing.None, length = 20): HashFormat {
const hashTemplate = `.[contenthash:${length}]`;
switch (outputHashing) {
case 'media':
return {
chunk: '',
extract: '',
file: hashTemplate,
script: '',
};
case 'bundles':
return {
chunk: hashTemplate,
extract: hashTemplate,
file: '',
script: hashTemplate,
};
case 'all':
return {
chunk: hashTemplate,
extract: hashTemplate,
file: hashTemplate,
script: hashTemplate,
};
case 'none':
default:
return {
chunk: '',
extract: '',
file: '',
script: '',
};
}
}
export type NormalizedEntryPoint = Required<Exclude<ScriptElement | StyleElement, string>>;
export function normalizeExtraEntryPoints(
extraEntryPoints: (ScriptElement | StyleElement)[],
defaultBundleName: string,
): NormalizedEntryPoint[] {
return extraEntryPoints.map((entry) => {
if (typeof entry === 'string') {
return { input: entry, inject: true, bundleName: defaultBundleName };
}
const { inject = true, ...newEntry } = entry;
let bundleName;
if (entry.bundleName) {
bundleName = entry.bundleName;
} else if (!inject) {
// Lazy entry points use the file name as bundle name.
bundleName = path.parse(entry.input).name;
} else {
bundleName = defaultBundleName;
}
return { ...newEntry, inject, bundleName };
});
}
export function assetNameTemplateFactory(hashFormat: HashFormat): (resourcePath: string) => string {
const visitedFiles = new Map<string, string>();
return (resourcePath: string) => {
if (hashFormat.file) {
// File names are hashed therefore we don't need to handle files with the same file name.
return `[name]${hashFormat.file}.[ext]`;
}
const filename = path.basename(resourcePath);
// Check if the file with the same name has already been processed.
const visited = visitedFiles.get(filename);
if (!visited) {
// Not visited.
visitedFiles.set(filename, resourcePath);
return filename;
} else if (visited === resourcePath) {
// Same file.
return filename;
}
// File has the same name but it's in a different location.
return '[path][name].[ext]';
};
}
export function getInstrumentationExcludedPaths(
root: string,
excludedPaths: string[],
): Set<string> {
const excluded = new Set<string>();
for (const excludeGlob of excludedPaths) {
glob
.sync(excludeGlob, { nodir: true, cwd: root, root, nomount: true })
.forEach((p) => excluded.add(path.join(root, p)));
}
return excluded;
}
export function getCacheSettings(
wco: WebpackConfigOptions,
angularVersion: string,
): WebpackOptionsNormalized['cache'] {
const { enabled, path: cacheDirectory } = wco.buildOptions.cache;
if (enabled) {
return {
type: 'filesystem',
profile: wco.buildOptions.verbose,
cacheDirectory: path.join(cacheDirectory, 'angular-webpack'),
maxMemoryGenerations: 1,
// We use the versions and build options as the cache name. The Webpack configurations are too
// dynamic and shared among different build types: test, build and serve.
// None of which are "named".
name: createHash('sha1')
.update(angularVersion)
.update(VERSION)
.update(wco.projectRoot)
.update(JSON.stringify(wco.tsConfig))
.update(
JSON.stringify({
...wco.buildOptions,
// Needed because outputPath changes on every build when using i18n extraction
// https://github.com/angular/angular-cli/blob/736a5f89deaca85f487b78aec9ff66d4118ceb6a/packages/angular_devkit/build_angular/src/utils/i18n-options.ts#L264-L265
outputPath: undefined,
}),
)
.digest('hex'),
};
}
if (wco.buildOptions.watch) {
return {
type: 'memory',
maxGenerations: 1,
};
}
return false;
}
export function globalScriptsByBundleName(
root: string,
scripts: ScriptElement[],
): { bundleName: string; inject: boolean; paths: string[] }[] {
return normalizeExtraEntryPoints(scripts, 'scripts').reduce(
(prev: { bundleName: string; paths: string[]; inject: boolean }[], curr) => {
const { bundleName, inject, input } = curr;
let resolvedPath = path.resolve(root, input);
if (!existsSync(resolvedPath)) {
try {
resolvedPath = require.resolve(input, { paths: [root] });
} catch {
throw new Error(`Script file ${input} does not exist.`);
}
}
const existingEntry = prev.find((el) => el.bundleName === bundleName);
if (existingEntry) {
if (existingEntry.inject && !inject) {
// All entries have to be lazy for the bundle to be lazy.
throw new Error(`The ${bundleName} bundle is mixing injected and non-injected scripts.`);
}
existingEntry.paths.push(resolvedPath);
} else {
prev.push({
bundleName,
inject,
paths: [resolvedPath],
});
}
return prev;
},
[],
);
}
export function assetPatterns(root: string, assets: AssetPatternClass[]) {
return assets.map((asset: AssetPatternClass, index: number): ObjectPattern => {
// Resolve input paths relative to workspace root and add slash at the end.
// eslint-disable-next-line prefer-const
let { input, output, ignore = [], glob } = asset;
input = path.resolve(root, input).replace(/\\/g, '/');
input = input.endsWith('/') ? input : input + '/';
output = output.endsWith('/') ? output : output + '/';
if (output.startsWith('..')) {
throw new Error('An asset cannot be written to a location outside of the output path.');
}
return {
context: input,
// Now we remove starting slash to make Webpack place it from the output root.
to: output.replace(/^\//, ''),
from: glob,
noErrorOnMissing: true,
force: true,
globOptions: {
dot: true,
followSymbolicLinks: !!asset.followSymlinks,
ignore: [
'.gitkeep',
'**/.DS_Store',
'**/Thumbs.db',
// Negate patterns needs to be absolute because copy-webpack-plugin uses absolute globs which
// causes negate patterns not to match.
// See: https://github.com/webpack-contrib/copy-webpack-plugin/issues/498#issuecomment-639327909
...ignore,
].map((i) => path.posix.join(input, i)),
},
priority: index,
};
});
}
export function externalizePackages(
context: string,
request: string | undefined,
callback: (error?: Error, result?: string) => void,
): void {
if (!request) {
return;
}
// Absolute & Relative paths are not externals
if (request.startsWith('.') || path.isAbsolute(request)) {
callback();
return;
}
try {
require.resolve(request, { paths: [context] });
callback(undefined, request);
} catch {
// Node couldn't find it, so it must be user-aliased
callback();
}
}
export function getStatsOptions(verbose = false): WebpackStatsOptions {
const webpackOutputOptions: WebpackStatsOptions = {
all: false, // Fallback value for stats options when an option is not defined. It has precedence over local webpack defaults.
colors: true,
hash: true, // required by custom stat output
timings: true, // required by custom stat output
chunks: true, // required by custom stat output
builtAt: true, // required by custom stat output
warnings: true,
errors: true,
assets: true, // required by custom stat output
cachedAssets: true, // required for bundle size calculators
// Needed for markAsyncChunksNonInitial.
ids: true,
entrypoints: true,
};
const verboseWebpackOutputOptions: WebpackStatsOptions = {
// The verbose output will most likely be piped to a file, so colors just mess it up.
colors: false,
usedExports: true,
optimizationBailout: true,
reasons: true,
children: true,
assets: true,
version: true,
chunkModules: true,
errorDetails: true,
errorStack: true,
moduleTrace: true,
logging: 'verbose',
modulesSpace: Infinity,
};
return verbose
? { ...webpackOutputOptions, ...verboseWebpackOutputOptions }
: webpackOutputOptions;
}
export function getMainFieldsAndConditionNames(
target: ScriptTarget,
platformServer: boolean,
): Pick<WebpackOptionsNormalized['resolve'], 'mainFields' | 'conditionNames'> {
const mainFields = platformServer
? ['es2015', 'module', 'main']
: ['es2015', 'browser', 'module', 'main'];
const conditionNames = ['es2015', '...'];
if (target >= ScriptTarget.ES2020) {
mainFields.unshift('es2020');
conditionNames.unshift('es2020');
}
return {
mainFields,
conditionNames,
};
}