/
options.ts
170 lines (156 loc) · 5.6 KB
/
options.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
/**
* @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 { BuilderContext } from '@angular-devkit/architect';
import * as path from 'path';
import { normalizeAssetPatterns, normalizeOptimization, normalizeSourceMaps } from '../../utils';
import { normalizePolyfills } from '../../utils/normalize-polyfills';
import { generateEntryPoints } from '../../utils/package-chunk-sort';
import { getIndexInputFile, getIndexOutputFile } from '../../utils/webpack-browser-config';
import { normalizeGlobalStyles } from '../../webpack/utils/helpers';
import { Schema as BrowserBuilderOptions, OutputHashing } from './schema';
export type NormalizedBrowserOptions = Awaited<ReturnType<typeof normalizeOptions>>;
/**
* Normalize the user provided options by creating full paths for all path based options
* and converting multi-form options into a single form that can be directly used
* by the build process.
*
* @param context The context for current builder execution.
* @param projectName The name of the project for the current execution.
* @param options An object containing the options to use for the build.
* @returns An object containing normalized options required to perform the build.
*/
export async function normalizeOptions(
context: BuilderContext,
projectName: string,
options: BrowserBuilderOptions,
) {
const workspaceRoot = context.workspaceRoot;
const projectMetadata = await context.getProjectMetadata(projectName);
const projectRoot = path.join(workspaceRoot, (projectMetadata.root as string | undefined) ?? '');
const projectSourceRoot = path.join(
workspaceRoot,
(projectMetadata.sourceRoot as string | undefined) ?? 'src',
);
// Normalize options
const mainEntryPoint = path.join(workspaceRoot, options.main);
// Currently esbuild do not support multiple files per entry-point
const [polyfillsEntryPoint, ...remainingPolyfills] = normalizePolyfills(
options.polyfills,
workspaceRoot,
);
if (remainingPolyfills.length) {
context.logger.warn(
`The 'polyfills' option currently does not support multiple entries by this experimental builder. The first entry will be used.`,
);
}
const tsconfig = path.join(workspaceRoot, options.tsConfig);
const outputPath = path.join(workspaceRoot, options.outputPath);
const optimizationOptions = normalizeOptimization(options.optimization);
const sourcemapOptions = normalizeSourceMaps(options.sourceMap ?? false);
const assets = options.assets?.length
? normalizeAssetPatterns(options.assets, workspaceRoot, projectRoot, projectSourceRoot)
: undefined;
const outputNames = {
bundles:
options.outputHashing === OutputHashing.All || options.outputHashing === OutputHashing.Bundles
? '[name].[hash]'
: '[name]',
media:
options.outputHashing === OutputHashing.All || options.outputHashing === OutputHashing.Media
? '[name].[hash]'
: '[name]',
};
if (options.resourcesOutputPath) {
outputNames.media = path.join(options.resourcesOutputPath, outputNames.media);
}
let fileReplacements: Record<string, string> | undefined;
if (options.fileReplacements) {
for (const replacement of options.fileReplacements) {
fileReplacements ??= {};
fileReplacements[path.join(workspaceRoot, replacement.replace)] = path.join(
workspaceRoot,
replacement.with,
);
}
}
const globalStyles: { name: string; files: string[]; initial: boolean }[] = [];
if (options.styles?.length) {
const { entryPoints: stylesheetEntrypoints, noInjectNames } = normalizeGlobalStyles(
options.styles || [],
);
for (const [name, files] of Object.entries(stylesheetEntrypoints)) {
globalStyles.push({ name, files, initial: !noInjectNames.includes(name) });
}
}
let serviceWorkerOptions;
if (options.serviceWorker) {
// If ngswConfigPath is not specified, the default is 'ngsw-config.json' within the project root
serviceWorkerOptions = options.ngswConfigPath
? path.join(workspaceRoot, options.ngswConfigPath)
: path.join(projectRoot, 'ngsw-config.json');
}
// Setup bundler entry points
const entryPoints: Record<string, string> = {
main: mainEntryPoint,
};
if (polyfillsEntryPoint) {
entryPoints['polyfills'] = polyfillsEntryPoint;
}
let indexHtmlOptions;
if (options.index) {
indexHtmlOptions = {
input: path.join(workspaceRoot, getIndexInputFile(options.index)),
// The output file will be created within the configured output path
output: getIndexOutputFile(options.index),
// TODO: Use existing information from above to create the insertion order
insertionOrder: generateEntryPoints({
scripts: options.scripts ?? [],
styles: options.styles ?? [],
}),
};
}
// Initial options to keep
const {
baseHref,
buildOptimizer,
crossOrigin,
externalDependencies,
poll,
preserveSymlinks,
stylePreprocessorOptions,
subresourceIntegrity,
verbose,
watch,
} = options;
// Return all the normalized options
return {
advancedOptimizations: buildOptimizer,
baseHref,
crossOrigin,
externalDependencies,
poll,
preserveSymlinks,
stylePreprocessorOptions,
subresourceIntegrity,
verbose,
watch,
workspaceRoot,
entryPoints,
optimizationOptions,
outputPath,
sourcemapOptions,
tsconfig,
projectRoot,
assets,
outputNames,
fileReplacements,
globalStyles,
serviceWorkerOptions,
indexHtmlOptions,
};
}