-
Notifications
You must be signed in to change notification settings - Fork 12k
/
Copy pathoptions.ts
140 lines (130 loc) · 4.43 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
/**
* @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.dev/license
*/
import { BuilderContext, targetFromTargetString } from '@angular-devkit/architect';
import path from 'node:path';
import { normalizeCacheOptions } from '../../utils/normalize-cache';
import { normalizeOptimization } from '../../utils/normalize-optimization';
import { isEsbuildBased } from './builder';
import { Schema as DevServerOptions } from './schema';
export type NormalizedDevServerOptions = 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: DevServerOptions,
) {
const { workspaceRoot, logger } = context;
const projectMetadata = await context.getProjectMetadata(projectName);
const projectRoot = path.join(workspaceRoot, (projectMetadata.root as string | undefined) ?? '');
const cacheOptions = normalizeCacheOptions(projectMetadata, workspaceRoot);
// Target specifier defaults to the current project's build target using the provided dev-server
// configuration if a configuration is present or the 'development' configuration if not.
const buildTargetSpecifier =
options.buildTarget ?? `::${context.target?.configuration || 'development'}`;
const buildTarget = targetFromTargetString(buildTargetSpecifier, projectName, 'build');
// Get the application builder options.
const browserBuilderName = await context.getBuilderNameForTarget(buildTarget);
const rawBuildOptions = await context.getTargetOptions(buildTarget);
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const buildOptions = (await context.validateOptions(rawBuildOptions, browserBuilderName)) as any;
const optimization = normalizeOptimization(buildOptions.optimization);
if (options.prebundle !== false && isEsbuildBased(browserBuilderName)) {
if (!cacheOptions.enabled) {
// Warn if the initial options provided by the user enable prebundling but caching is disabled
logger.warn(
'Prebundling has been configured but will not be used because caching has been disabled.',
);
} else if (optimization.scripts) {
// Warn if the initial options provided by the user enable prebundling but script optimization is enabled.
logger.warn(
'Prebundling has been configured but will not be used because scripts optimization is enabled.',
);
}
}
let inspect: false | { host?: string; port?: number } = false;
const inspectRaw = options.inspect;
if (inspectRaw === true || inspectRaw === '' || inspectRaw === 'true') {
inspect = {
host: undefined,
port: undefined,
};
} else if (typeof inspectRaw === 'string' && inspectRaw !== 'false') {
const port = +inspectRaw;
if (isFinite(port)) {
inspect = {
host: undefined,
port,
};
} else {
const [host, port] = inspectRaw.split(':');
inspect = {
host,
port: isNaN(+port) ? undefined : +port,
};
}
}
// Initial options to keep
const {
host,
port,
poll,
open,
verbose,
watch,
allowedHosts,
disableHostCheck,
liveReload,
hmr,
headers,
proxyConfig,
servePath,
publicHost,
ssl,
sslCert,
sslKey,
forceEsbuild,
prebundle,
} = options;
// Return all the normalized options
return {
buildTarget,
host: host ?? 'localhost',
port: port ?? 4200,
poll,
open,
verbose,
watch,
liveReload: !!liveReload,
hmr,
headers,
workspaceRoot,
projectRoot,
cacheOptions,
allowedHosts,
disableHostCheck,
proxyConfig,
servePath,
publicHost,
ssl,
sslCert,
sslKey,
forceEsbuild,
// Prebundling defaults to true but requires caching to function
prebundle: cacheOptions.enabled && !optimization.scripts && (prebundle ?? true),
inspect,
};
}