-
Notifications
You must be signed in to change notification settings - Fork 12k
/
index.ts
327 lines (287 loc) · 9.9 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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
/**
* @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, createBuilder, targetFromTargetString } from '@angular-devkit/architect';
import { BuildResult, runWebpack } from '@angular-devkit/build-webpack';
import { JsonObject } from '@angular-devkit/core';
import type { ɵParsedMessage as LocalizeMessage } from '@angular/localize';
import type { Diagnostics } from '@angular/localize/src/tools/src/diagnostics';
import * as fs from 'fs';
import * as path from 'path';
import * as webpack from 'webpack';
import { Schema as BrowserBuilderOptions, OutputHashing } from '../browser/schema';
import { ExecutionTransformer } from '../transforms';
import { createI18nOptions } from '../utils/i18n-options';
import { assertCompatibleAngularVersion } from '../utils/version';
import { generateBrowserWebpackConfigFromContext } from '../utils/webpack-browser-config';
import {
getBrowserConfig,
getCommonConfig,
getStatsConfig,
getTypeScriptConfig,
getWorkerConfig,
} from '../webpack/configs';
import { createWebpackLoggingCallback } from '../webpack/utils/stats';
import { Format, Schema } from './schema';
export type ExtractI18nBuilderOptions = Schema & JsonObject;
function getI18nOutfile(format: string | undefined) {
switch (format) {
case 'xmb':
return 'messages.xmb';
case 'xlf':
case 'xlif':
case 'xliff':
case 'xlf2':
case 'xliff2':
return 'messages.xlf';
case 'json':
case 'legacy-migrate':
return 'messages.json';
case 'arb':
return 'messages.arb';
default:
throw new Error(`Unsupported format "${format}"`);
}
}
async function getSerializer(
format: Format,
sourceLocale: string,
basePath: string,
useLegacyIds: boolean,
diagnostics: Diagnostics,
) {
switch (format) {
case Format.Xmb:
const { XmbTranslationSerializer } = await import(
'@angular/localize/src/tools/src/extract/translation_files/xmb_translation_serializer'
);
// eslint-disable-next-line @typescript-eslint/no-explicit-any
return new XmbTranslationSerializer(basePath as any, useLegacyIds);
case Format.Xlf:
case Format.Xlif:
case Format.Xliff:
const { Xliff1TranslationSerializer } = await import(
'@angular/localize/src/tools/src/extract/translation_files/xliff1_translation_serializer'
);
// eslint-disable-next-line @typescript-eslint/no-explicit-any
return new Xliff1TranslationSerializer(sourceLocale, basePath as any, useLegacyIds, {});
case Format.Xlf2:
case Format.Xliff2:
const { Xliff2TranslationSerializer } = await import(
'@angular/localize/src/tools/src/extract/translation_files/xliff2_translation_serializer'
);
// eslint-disable-next-line @typescript-eslint/no-explicit-any
return new Xliff2TranslationSerializer(sourceLocale, basePath as any, useLegacyIds, {});
case Format.Json:
const { SimpleJsonTranslationSerializer } = await import(
'@angular/localize/src/tools/src/extract/translation_files/json_translation_serializer'
);
return new SimpleJsonTranslationSerializer(sourceLocale);
case Format.LegacyMigrate:
const { LegacyMessageIdMigrationSerializer } = await import(
'@angular/localize/src/tools/src/extract/translation_files/legacy_message_id_migration_serializer'
);
return new LegacyMessageIdMigrationSerializer(diagnostics);
case Format.Arb:
const { ArbTranslationSerializer } = await import(
'@angular/localize/src/tools/src/extract/translation_files/arb_translation_serializer'
);
const fileSystem = {
relative(from: string, to: string): string {
return path.relative(from, to);
},
};
// eslint-disable-next-line @typescript-eslint/no-explicit-any
return new ArbTranslationSerializer(sourceLocale, basePath as any, fileSystem as any);
}
}
function normalizeFormatOption(options: ExtractI18nBuilderOptions): Format {
let format = options.format;
switch (format) {
case Format.Xlf:
case Format.Xlif:
case Format.Xliff:
format = Format.Xlf;
break;
case Format.Xlf2:
case Format.Xliff2:
format = Format.Xlf2;
break;
}
// Default format is xliff1
return format ?? Format.Xlf;
}
class NoEmitPlugin {
apply(compiler: webpack.Compiler): void {
compiler.hooks.shouldEmit.tap('angular-no-emit', () => false);
}
}
/**
* @experimental Direct usage of this function is considered experimental.
*/
export async function execute(
options: ExtractI18nBuilderOptions,
context: BuilderContext,
transforms?: {
webpackConfiguration?: ExecutionTransformer<webpack.Configuration>;
},
): Promise<BuildResult> {
// Check Angular version.
assertCompatibleAngularVersion(context.workspaceRoot, context.logger);
const browserTarget = targetFromTargetString(options.browserTarget);
const browserOptions = await context.validateOptions<JsonObject & BrowserBuilderOptions>(
await context.getTargetOptions(browserTarget),
await context.getBuilderNameForTarget(browserTarget),
);
const format = normalizeFormatOption(options);
// We need to determine the outFile name so that AngularCompiler can retrieve it.
let outFile = options.outFile || getI18nOutfile(format);
if (options.outputPath) {
// AngularCompilerPlugin doesn't support genDir so we have to adjust outFile instead.
outFile = path.join(options.outputPath, outFile);
}
outFile = path.resolve(context.workspaceRoot, outFile);
if (!context.target || !context.target.project) {
throw new Error('The builder requires a target.');
}
const metadata = await context.getProjectMetadata(context.target);
const i18n = createI18nOptions(metadata);
let useLegacyIds = true;
const ivyMessages: LocalizeMessage[] = [];
const { config, projectRoot } = await generateBrowserWebpackConfigFromContext(
{
...browserOptions,
optimization: false,
sourceMap: {
scripts: true,
styles: false,
vendor: true,
},
buildOptimizer: false,
aot: true,
progress: options.progress,
budgets: [],
assets: [],
scripts: [],
styles: [],
deleteOutputPath: false,
extractLicenses: false,
subresourceIntegrity: false,
outputHashing: OutputHashing.None,
namedChunks: true,
},
context,
(wco) => {
if (wco.tsConfig.options.enableIvy === false) {
context.logger.warn(
'Ivy extraction enabled but application is not Ivy enabled. Extraction may fail.',
);
}
// Default value for legacy message ids is currently true
useLegacyIds = wco.tsConfig.options.enableI18nLegacyMessageIdFormat ?? true;
const partials = [
{ plugins: [new NoEmitPlugin()] },
getCommonConfig(wco),
getBrowserConfig(wco),
getTypeScriptConfig(wco),
getWorkerConfig(wco),
getStatsConfig(wco),
];
// Add Ivy application file extractor support
partials.unshift({
module: {
rules: [
{
test: /\.[t|j]s$/,
loader: require.resolve('./ivy-extract-loader'),
options: {
messageHandler: (messages: LocalizeMessage[]) => ivyMessages.push(...messages),
},
},
],
},
});
// Replace all stylesheets with an empty default export
partials.push({
plugins: [
new webpack.NormalModuleReplacementPlugin(
/\.(css|scss|sass|styl|less)$/,
path.join(__dirname, 'empty-export-default.js'),
),
new webpack.NormalModuleReplacementPlugin(
/^angular-resource:\/\//,
path.join(__dirname, 'empty-export-default.js'),
),
],
});
return partials;
},
);
try {
require.resolve('@angular/localize');
} catch {
return {
success: false,
error: `Ivy extraction requires the '@angular/localize' package.`,
outputPath: outFile,
};
}
const webpackResult = await runWebpack(
(await transforms?.webpackConfiguration?.(config)) || config,
context,
{
logging: createWebpackLoggingCallback(false, context.logger),
webpackFactory: webpack,
},
).toPromise();
// Set the outputPath to the extraction output location for downstream consumers
webpackResult.outputPath = outFile;
// Complete if Webpack build failed
if (!webpackResult.success) {
return webpackResult;
}
const basePath = config.context || projectRoot;
const { checkDuplicateMessages } = await import(
'@angular/localize/src/tools/src/extract/duplicates'
);
// The filesystem is used to create a relative path for each file
// from the basePath. This relative path is then used in the error message.
const checkFileSystem = {
relative(from: string, to: string): string {
return path.relative(from, to);
},
};
const diagnostics = checkDuplicateMessages(
// eslint-disable-next-line @typescript-eslint/no-explicit-any
checkFileSystem as any,
ivyMessages,
'warning',
// eslint-disable-next-line @typescript-eslint/no-explicit-any
basePath as any,
);
if (diagnostics.messages.length > 0) {
context.logger.warn(diagnostics.formatDiagnostics(''));
}
// Serialize all extracted messages
const serializer = await getSerializer(
format,
i18n.sourceLocale,
basePath,
useLegacyIds,
diagnostics,
);
const content = serializer.serialize(ivyMessages);
// Ensure directory exists
const outputPath = path.dirname(outFile);
if (!fs.existsSync(outputPath)) {
fs.mkdirSync(outputPath, { recursive: true });
}
// Write translation file
fs.writeFileSync(outFile, content);
return webpackResult;
}
export default createBuilder<JsonObject & ExtractI18nBuilderOptions>(execute);