-
Notifications
You must be signed in to change notification settings - Fork 12k
/
Copy pathindex.ts
292 lines (262 loc) · 10 KB
/
index.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
/**
* @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 { assertCompatibleAngularVersion, purgeStaleBuildCache } from '@angular/build/private';
import { BuilderContext, BuilderOutput, createBuilder } from '@angular-devkit/architect';
import { runWebpack } from '@angular-devkit/build-webpack';
import { readFile } from 'node:fs/promises';
import * as path from 'node:path';
import { Observable, concatMap, from } from 'rxjs';
import webpack, { Configuration } from 'webpack';
import { getCommonConfig, getStylesConfig } from '../../tools/webpack/configs';
import { isPackageInstalled } from '../../tools/webpack/utils/helpers';
import {
statsErrorsToString,
statsHasErrors,
statsHasWarnings,
statsWarningsToString,
webpackStatsLogger,
} from '../../tools/webpack/utils/stats';
import { ExecutionTransformer } from '../../transforms';
import {
NormalizedBrowserBuilderSchema,
deleteOutputDir,
normalizeAssetPatterns,
} from '../../utils';
import { colors } from '../../utils/color';
import { copyAssets } from '../../utils/copy-assets';
import { assertIsError } from '../../utils/error';
import { i18nInlineEmittedFiles } from '../../utils/i18n-inlining';
import { I18nOptions } from '../../utils/i18n-webpack';
import { ensureOutputPaths } from '../../utils/output-paths';
import { Spinner } from '../../utils/spinner';
import {
BrowserWebpackConfigOptions,
generateI18nBrowserWebpackConfigFromContext,
} from '../../utils/webpack-browser-config';
import { Schema as ServerBuilderOptions } from './schema';
/**
* @experimental Direct usage of this type is considered experimental.
*/
export type ServerBuilderOutput = BuilderOutput & {
baseOutputPath: string;
outputPath: string;
outputs: {
locale?: string;
path: string;
}[];
};
export type { ServerBuilderOptions };
/**
* @experimental Direct usage of this function is considered experimental.
*/
export function execute(
options: ServerBuilderOptions,
context: BuilderContext,
transforms: {
webpackConfiguration?: ExecutionTransformer<webpack.Configuration>;
} = {},
): Observable<ServerBuilderOutput> {
const root = context.workspaceRoot;
// Check Angular version.
assertCompatibleAngularVersion(root);
const baseOutputPath = path.resolve(root, options.outputPath);
let outputPaths: undefined | Map<string, string>;
return from(initialize(options, context, transforms.webpackConfiguration)).pipe(
concatMap(({ config, i18n, projectRoot, projectSourceRoot }) => {
return runWebpack(config, context, {
webpackFactory: require('webpack') as typeof webpack,
logging: (stats, config) => {
if (options.verbose && config.stats !== false) {
const statsOptions = config.stats === true ? undefined : config.stats;
context.logger.info(stats.toString(statsOptions));
}
},
}).pipe(
concatMap(async (output) => {
const { emittedFiles = [], outputPath, webpackStats, success } = output;
if (!webpackStats) {
throw new Error('Webpack stats build result is required.');
}
if (!success) {
if (statsHasWarnings(webpackStats)) {
context.logger.warn(statsWarningsToString(webpackStats, { colors: true }));
}
if (statsHasErrors(webpackStats)) {
context.logger.error(statsErrorsToString(webpackStats, { colors: true }));
}
return output;
}
const spinner = new Spinner();
spinner.enabled = options.progress !== false;
outputPaths = ensureOutputPaths(baseOutputPath, i18n);
// Copy assets
if (!options.watch && options.assets?.length) {
spinner.start('Copying assets...');
try {
await copyAssets(
normalizeAssetPatterns(
options.assets,
context.workspaceRoot,
projectRoot,
projectSourceRoot,
),
Array.from(outputPaths.values()),
context.workspaceRoot,
);
spinner.succeed('Copying assets complete.');
} catch (err) {
spinner.fail(colors.redBright('Copying of assets failed.'));
assertIsError(err);
return {
...output,
success: false,
error: 'Unable to copy assets: ' + err.message,
};
}
}
if (i18n.shouldInline) {
const success = await i18nInlineEmittedFiles(
context,
emittedFiles,
i18n,
baseOutputPath,
Array.from(outputPaths.values()),
[],
outputPath,
options.i18nMissingTranslation,
);
if (!success) {
return {
...output,
success: false,
};
}
}
webpackStatsLogger(context.logger, webpackStats, config);
return output;
}),
);
}),
concatMap(async (output) => {
if (!output.success) {
return output as ServerBuilderOutput;
}
return {
...output,
baseOutputPath,
outputs: (outputPaths &&
[...outputPaths.entries()].map(([locale, path]) => ({
locale,
path,
}))) || {
path: baseOutputPath,
},
} as ServerBuilderOutput;
}),
);
}
export default createBuilder<ServerBuilderOptions, ServerBuilderOutput>(execute);
async function initialize(
options: ServerBuilderOptions,
context: BuilderContext,
webpackConfigurationTransform?: ExecutionTransformer<webpack.Configuration>,
): Promise<{
config: webpack.Configuration;
i18n: I18nOptions;
projectRoot: string;
projectSourceRoot?: string;
}> {
// Purge old build disk cache.
await purgeStaleBuildCache(context);
await checkTsConfigForPreserveWhitespacesSetting(context, options.tsConfig);
const browserslist = (await import('browserslist')).default;
const originalOutputPath = options.outputPath;
// Assets are processed directly by the builder except when watching
const adjustedOptions = options.watch ? options : { ...options, assets: [] };
const { config, projectRoot, projectSourceRoot, i18n } =
await generateI18nBrowserWebpackConfigFromContext(
{
...adjustedOptions,
aot: true,
platform: 'server',
} as NormalizedBrowserBuilderSchema,
context,
(wco) => {
// We use the platform to determine the JavaScript syntax output.
wco.buildOptions.supportedBrowsers ??= [];
wco.buildOptions.supportedBrowsers.push(...browserslist('maintained node versions'));
return [
getPlatformServerExportsConfig(wco),
getCommonConfig(wco),
getStylesConfig(wco),
{
plugins: [
new webpack.DefinePlugin({
'ngServerMode': true,
}),
],
},
];
},
);
if (options.deleteOutputPath) {
await deleteOutputDir(context.workspaceRoot, originalOutputPath);
}
const transformedConfig = (await webpackConfigurationTransform?.(config)) ?? config;
return { config: transformedConfig, i18n, projectRoot, projectSourceRoot };
}
async function checkTsConfigForPreserveWhitespacesSetting(
context: BuilderContext,
tsConfigPath: string,
): Promise<void> {
// We don't use the `readTsConfig` method on purpose here.
// To only catch cases were `preserveWhitespaces` is set directly in the `tsconfig.server.json`,
// which in the majority of cases will cause a mistmatch between client and server builds.
// Technically we should check if `tsconfig.server.json` and `tsconfig.app.json` values match.
// But:
// 1. It is not guaranteed that `tsconfig.app.json` is used to build the client side of this app.
// 2. There is no easy way to access the build build config from the server builder.
// 4. This will no longer be an issue with a single compilation model were the same tsconfig is used for both browser and server builds.
const content = await readFile(path.join(context.workspaceRoot, tsConfigPath), 'utf-8');
const { parse } = await import('jsonc-parser');
const tsConfig = parse(content, [], { allowTrailingComma: true });
if (tsConfig.angularCompilerOptions?.preserveWhitespaces !== undefined) {
context.logger.warn(
`"preserveWhitespaces" was set in "${tsConfigPath}". ` +
'Make sure that this setting is set consistently in both "tsconfig.server.json" for your server side ' +
'and "tsconfig.app.json" for your client side. A mismatched value will cause hydration to break.\n' +
'For more information see: https://angular.dev/guide/hydration#preserve-whitespaces-configuration',
);
}
}
/**
* Add `@angular/platform-server` exports.
* This is needed so that DI tokens can be referenced and set at runtime outside of the bundle.
*/
function getPlatformServerExportsConfig(wco: BrowserWebpackConfigOptions): Partial<Configuration> {
// Add `@angular/platform-server` exports.
// This is needed so that DI tokens can be referenced and set at runtime outside of the bundle.
// Only add `@angular/platform-server` exports when it is installed.
// In some cases this builder is used when `@angular/platform-server` is not installed.
// Example: when using `@nguniversal/common/clover` which does not need `@angular/platform-server`.
return isPackageInstalled(wco.root, '@angular/platform-server')
? {
module: {
rules: [
{
loader: require.resolve('./platform-server-exports-loader'),
include: [path.resolve(wco.root, wco.buildOptions.main)],
options: {
angularSSRInstalled: isPackageInstalled(wco.root, '@angular/ssr'),
},
},
],
},
}
: {};
}