-
Notifications
You must be signed in to change notification settings - Fork 12k
/
Copy pathindex.ts
142 lines (127 loc) · 4.25 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
/**
* @license
* Copyright Google Inc. 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 { WebpackLoggingCallback, runWebpack } from '@angular-devkit/build-webpack';
import { JsonObject } from '@angular-devkit/core';
import * as path from 'path';
import * as webpack from 'webpack';
import {
getAotConfig,
getCommonConfig,
getStatsConfig,
getStylesConfig,
} from '../angular-cli-files/models/webpack-configs';
import { statsErrorsToString, statsWarningsToString } from '../angular-cli-files/utilities/stats';
import { Schema as BrowserBuilderOptions } from '../browser/schema';
import { createI18nOptions } from '../utils/i18n-options';
import { assertCompatibleAngularVersion } from '../utils/version';
import { generateBrowserWebpackConfigFromContext } from '../utils/webpack-browser-config';
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';
default:
throw new Error(`Unsupported format "${format}"`);
}
}
class InMemoryOutputPlugin {
apply(compiler: webpack.Compiler): void {
// tslint:disable-next-line:no-any
compiler.outputFileSystem = new (webpack as any).MemoryOutputFileSystem();
}
}
export async function execute(options: ExtractI18nBuilderOptions, context: BuilderContext) {
// 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),
);
if (options.i18nFormat !== Format.Xlf) {
options.format = options.i18nFormat;
}
switch (options.format) {
case Format.Xlf:
case Format.Xlif:
case Format.Xliff:
options.format = Format.Xlf;
break;
case Format.Xlf2:
case Format.Xliff2:
options.format = Format.Xlf2;
break;
}
// We need to determine the outFile name so that AngularCompiler can retrieve it.
let outFile = options.outFile || getI18nOutfile(options.format);
if (options.outputPath) {
// AngularCompilerPlugin doesn't support genDir so we have to adjust outFile instead.
outFile = path.join(options.outputPath, outFile);
}
const projectName = context.target && context.target.project;
if (!projectName) {
throw new Error('The builder requires a target.');
}
// target is verified in the above call
// tslint:disable-next-line: no-non-null-assertion
const metadata = await context.getProjectMetadata(context.target!);
const i18n = createI18nOptions(metadata);
const { config } = await generateBrowserWebpackConfigFromContext(
{
...browserOptions,
optimization: {
scripts: false,
styles: false,
},
buildOptimizer: false,
i18nLocale: options.i18nLocale || i18n.sourceLocale,
i18nFormat: options.format,
i18nFile: outFile,
aot: true,
progress: options.progress,
assets: [],
scripts: [],
styles: [],
deleteOutputPath: false,
},
context,
wco => [
{ plugins: [new InMemoryOutputPlugin()] },
getCommonConfig(wco),
getAotConfig(wco, true),
getStylesConfig(wco),
getStatsConfig(wco),
],
);
const logging: WebpackLoggingCallback = (stats, config) => {
const json = stats.toJson({ errors: true, warnings: true });
if (stats.hasWarnings()) {
context.logger.warn(statsWarningsToString(json, config.stats));
}
if (stats.hasErrors()) {
context.logger.error(statsErrorsToString(json, config.stats));
}
};
return runWebpack(config, context, {
logging,
webpackFactory: await import('webpack'),
}).toPromise();
}
export default createBuilder<JsonObject & ExtractI18nBuilderOptions>(execute);