forked from angular/angular-cli
-
Notifications
You must be signed in to change notification settings - Fork 1
/
options.ts
240 lines (221 loc) · 8.15 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
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
/**
* @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 fs from 'node:fs';
import { createRequire } from 'node:module';
import path from 'node:path';
import { normalizeAssetPatterns, normalizeOptimization, normalizeSourceMaps } from '../../utils';
import { normalizeCacheOptions } from '../../utils/normalize-cache';
import { generateEntryPoints } from '../../utils/package-chunk-sort';
import { getIndexInputFile, getIndexOutputFile } from '../../utils/webpack-browser-config';
import { globalScriptsByBundleName, 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 = normalizeDirectoryPath(
path.join(workspaceRoot, (projectMetadata.root as string | undefined) ?? ''),
);
const projectSourceRoot = normalizeDirectoryPath(
path.join(workspaceRoot, (projectMetadata.sourceRoot as string | undefined) ?? 'src'),
);
const cacheOptions = normalizeCacheOptions(projectMetadata, workspaceRoot);
const mainEntryPoint = path.join(workspaceRoot, options.main);
const tsconfig = path.join(workspaceRoot, options.tsConfig);
const outputPath = normalizeDirectoryPath(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) });
}
}
const globalScripts: { name: string; files: string[]; initial: boolean }[] = [];
if (options.scripts?.length) {
for (const { bundleName, paths, inject } of globalScriptsByBundleName(options.scripts)) {
globalScripts.push({ name: bundleName, files: paths, initial: inject });
}
}
let tailwindConfiguration: { file: string; package: string } | undefined;
const tailwindConfigurationPath = findTailwindConfigurationFile(workspaceRoot, projectRoot);
if (tailwindConfigurationPath) {
// Create a node resolver at the project root as a directory
const resolver = createRequire(projectRoot + '/');
try {
tailwindConfiguration = {
file: tailwindConfigurationPath,
package: resolver.resolve('tailwindcss'),
};
} catch {
const relativeTailwindConfigPath = path.relative(workspaceRoot, tailwindConfigurationPath);
context.logger.warn(
`Tailwind CSS configuration file found (${relativeTailwindConfigPath})` +
` but the 'tailwindcss' package is not installed.` +
` To enable Tailwind CSS, please install the 'tailwindcss' package.`,
);
}
}
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,
};
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 {
allowedCommonJsDependencies,
aot,
baseHref,
buildOptimizer,
crossOrigin,
externalDependencies,
extractLicenses,
inlineStyleLanguage = 'css',
poll,
polyfills,
preserveSymlinks,
statsJson,
stylePreprocessorOptions,
subresourceIntegrity,
verbose,
watch,
progress,
} = options;
// Return all the normalized options
return {
advancedOptimizations: buildOptimizer,
allowedCommonJsDependencies,
baseHref,
cacheOptions,
crossOrigin,
externalDependencies,
extractLicenses,
inlineStyleLanguage,
jit: !aot,
stats: !!statsJson,
polyfills: polyfills === undefined || Array.isArray(polyfills) ? polyfills : [polyfills],
poll,
progress: progress ?? true,
// If not explicitly set, default to the Node.js process argument
preserveSymlinks: preserveSymlinks ?? process.execArgv.includes('--preserve-symlinks'),
stylePreprocessorOptions,
subresourceIntegrity,
verbose,
watch,
workspaceRoot,
entryPoints,
optimizationOptions,
outputPath,
sourcemapOptions,
tsconfig,
projectRoot,
assets,
outputNames,
fileReplacements,
globalStyles,
globalScripts,
serviceWorkerOptions,
indexHtmlOptions,
tailwindConfiguration,
};
}
function findTailwindConfigurationFile(
workspaceRoot: string,
projectRoot: string,
): string | undefined {
// A configuration file can exist in the project or workspace root
// The list of valid config files can be found:
// https://github.com/tailwindlabs/tailwindcss/blob/8845d112fb62d79815b50b3bae80c317450b8b92/src/util/resolveConfigPath.js#L46-L52
const tailwindConfigFiles = ['tailwind.config.js', 'tailwind.config.cjs'];
for (const basePath of [projectRoot, workspaceRoot]) {
for (const configFile of tailwindConfigFiles) {
// Project level configuration should always take precedence.
const fullPath = path.join(basePath, configFile);
if (fs.existsSync(fullPath)) {
return fullPath;
}
}
}
return undefined;
}
/**
* Normalize a directory path string.
* Currently only removes a trailing slash if present.
* @param path A path string.
* @returns A normalized path string.
*/
function normalizeDirectoryPath(path: string): string {
const last = path[path.length - 1];
if (last === '/' || last === '\\') {
return path.slice(0, -1);
}
return path;
}