-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathnormalize-options.ts
213 lines (196 loc) · 5.9 KB
/
normalize-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
import { FileReplacement } from '@nx/angular-rspack-compiler';
import {
PluginAngularOptions,
type DevServerOptions,
OutputPath,
NormalizedPluginAngularOptions,
} from './plugin-options';
import { join, resolve } from 'node:path';
import { existsSync } from 'node:fs';
/**
* Resolves file replacement paths to absolute paths based on the provided root directory.
*
* @param fileReplacements - Array of file replacements with relative paths.
* @param root - The root directory to resolve the paths against.
* @returns Array of file replacements resolved against the root.
*/
export function resolveFileReplacements(
fileReplacements: FileReplacement[],
root: string
): FileReplacement[] {
return fileReplacements.map((fileReplacement) => ({
replace: resolve(root, fileReplacement.replace),
with: resolve(root, fileReplacement.with),
}));
}
export function getHasServer({
server,
ssr,
}: Pick<PluginAngularOptions, 'server' | 'ssr'>): boolean {
const root = process.cwd();
return !!(
server &&
ssr &&
(ssr as { entry: string }).entry &&
existsSync(join(root, server)) &&
existsSync(join(root, (ssr as { entry: string }).entry))
);
}
export function validateSsr(ssr: PluginAngularOptions['ssr']) {
if (!ssr) {
return;
}
if (ssr === true) {
throw new Error(
'The "ssr" option should be an object or false. Please check the documentation.'
);
}
if (typeof ssr === 'object')
if (!ssr.entry) {
throw new Error(
'The "ssr" option should have an "entry" property. Please check the documentation.'
);
} else if (ssr.experimentalPlatform === 'neutral') {
console.warn(
'The "ssr.experimentalPlatform" option is not currently supported. Node will be used as the platform.'
);
}
}
export function validateOptimization(
optimization: PluginAngularOptions['optimization']
) {
if (typeof optimization === 'boolean' || optimization === undefined) {
return;
}
if (typeof optimization === 'object')
console.warn(
'The "optimization" option currently only supports a boolean value. Please check the documentation.'
);
}
function validateChunkOptions(options: Partial<PluginAngularOptions>) {
if (options.namedChunks !== undefined) {
console.warn(`The "namedChunks" option is not supported with Rsbuild.`);
}
if (options.commonChunk !== undefined) {
console.warn(`The "commonChunk" option is not supported with Rsbuild.`);
}
if (options.vendorChunk !== undefined) {
console.warn(`The "vendorChunk" option is not supported with Rsbuild.`);
}
}
export const DEFAULT_PLUGIN_ANGULAR_OPTIONS: PluginAngularOptions = {
index: './src/index.html',
browser: './src/main.ts',
server: undefined,
ssr: undefined,
fileReplacements: [],
hasServer: false,
polyfills: [],
assets: ['./public'],
styles: ['./src/styles.css'],
scripts: [],
aot: true,
inlineStyleLanguage: 'css',
tsConfig: join(process.cwd(), 'tsconfig.app.json'),
optimization: true,
outputPath: normalizeOutputPath(process.cwd(), undefined),
outputHashing: 'all',
useTsProjectReferences: false,
namedChunks: false,
skipTypeChecking: false,
devServer: {
port: 4200,
},
};
export function normalizeOptions(
options: Partial<PluginAngularOptions> = {}
): NormalizedPluginAngularOptions {
const root = process.cwd();
const {
fileReplacements = [],
server,
ssr,
optimization,
devServer,
...restOptions
} = options;
validateSsr(ssr);
const normalizedSsr = !ssr
? false
: typeof ssr === 'object'
? {
entry: ssr.entry,
experimentalPlatform: 'node' as const, // @TODO: Add support for neutral platform
}
: ssr;
validateOptimization(optimization);
const normalizedOptimization = optimization !== false; // @TODO: Add support for optimization options
const aot = options.aot ?? true;
const advancedOptimizations = aot && normalizedOptimization;
validateChunkOptions(options);
return {
...DEFAULT_PLUGIN_ANGULAR_OPTIONS,
...restOptions,
...(server != null ? { server } : {}),
...(ssr != null ? { ssr: normalizedSsr } : {}),
optimization: normalizedOptimization,
outputPath: normalizeOutputPath(root, options.outputPath),
advancedOptimizations,
aot,
outputHashing: options.outputHashing ?? 'all',
namedChunks: options.namedChunks ?? false,
fileReplacements: resolveFileReplacements(fileReplacements, root),
hasServer: getHasServer({ server, ssr: normalizedSsr }),
devServer: normalizeDevServer(devServer),
};
}
function normalizeDevServer(
devServer: DevServerOptions | undefined
): DevServerOptions & { port: number } {
const defaultPort = 4200;
if (!devServer) {
return { port: defaultPort };
}
return {
...devServer,
port: devServer.port ?? defaultPort,
};
}
/**
* This is slightly different to the Rspack solution as Rsbuild will use only relative paths
* from the base directory provided as the outputPath.
*/
export function normalizeOutputPath(
root: string,
outputPath: string | OutputPath | undefined
): OutputPath {
const defaultBase = join(root, 'dist');
const defaultBrowser = join(defaultBase, 'browser');
if (!outputPath) {
return {
base: defaultBase,
browser: defaultBrowser,
server: join(defaultBase, 'server'),
media: 'media',
};
}
if (typeof outputPath === 'string') {
if (!outputPath.startsWith(root)) {
outputPath = join(root, outputPath);
}
return {
base: outputPath,
browser: join(outputPath, 'browser'),
server: join(outputPath, 'server'),
media: 'media',
};
}
const providedBase = outputPath.base ?? defaultBase;
const providedBrowser = outputPath.browser ?? join(providedBase, 'browser');
return {
base: providedBase,
browser: providedBrowser,
server: outputPath.server ?? join(outputPath.base ?? defaultBase, 'server'),
media: outputPath.media ?? 'media',
};
}