-
Notifications
You must be signed in to change notification settings - Fork 12k
/
application.ts
204 lines (177 loc) · 5.79 KB
/
application.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
/**
* @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 * as fs from 'fs';
import * as path from 'path';
export type DiagnosticReporter = (type: 'error' | 'warning' | 'info', message: string) => void;
export interface ApplicationPresetOptions {
i18n?: {
locale: string;
missingTranslationBehavior?: 'error' | 'warning' | 'ignore';
translation?: unknown;
};
angularLinker?: {
shouldLink: boolean;
jitMode: boolean;
};
forceES5?: boolean;
forceAsyncTransformation?: boolean;
diagnosticReporter?: DiagnosticReporter;
}
type I18nDiagnostics = import('@angular/localize/src/tools/src/diagnostics').Diagnostics;
function createI18nDiagnostics(reporter: DiagnosticReporter | undefined): I18nDiagnostics {
// Babel currently is synchronous so import cannot be used
const diagnostics: I18nDiagnostics =
new (require('@angular/localize/src/tools/src/diagnostics').Diagnostics)();
if (!reporter) {
return diagnostics;
}
const baseAdd = diagnostics.add;
diagnostics.add = function (type, message, ...args) {
if (type !== 'ignore') {
baseAdd.call(diagnostics, type, message, ...args);
reporter(type, message);
}
};
const baseError = diagnostics.error;
diagnostics.error = function (message, ...args) {
baseError.call(diagnostics, message, ...args);
reporter('error', message);
};
const baseWarn = diagnostics.warn;
diagnostics.warn = function (message, ...args) {
baseWarn.call(diagnostics, message, ...args);
reporter('warning', message);
};
const baseMerge = diagnostics.merge;
diagnostics.merge = function (other, ...args) {
baseMerge.call(diagnostics, other, ...args);
for (const diagnostic of other.messages) {
reporter(diagnostic.type, diagnostic.message);
}
};
return diagnostics;
}
function createI18nPlugins(
locale: string,
translation: unknown | undefined,
missingTranslationBehavior: 'error' | 'warning' | 'ignore',
diagnosticReporter: DiagnosticReporter | undefined,
) {
const diagnostics = createI18nDiagnostics(diagnosticReporter);
const plugins = [];
if (translation) {
const {
makeEs2015TranslatePlugin,
} = require('@angular/localize/src/tools/src/translate/source_files/es2015_translate_plugin');
plugins.push(
makeEs2015TranslatePlugin(diagnostics, translation, {
missingTranslation: missingTranslationBehavior,
}),
);
const {
makeEs5TranslatePlugin,
} = require('@angular/localize/src/tools/src/translate/source_files/es5_translate_plugin');
plugins.push(
makeEs5TranslatePlugin(diagnostics, translation, {
missingTranslation: missingTranslationBehavior,
}),
);
}
const {
makeLocalePlugin,
} = require('@angular/localize/src/tools/src/translate/source_files/locale_plugin');
plugins.push(makeLocalePlugin(locale));
return plugins;
}
function createNgtscLogger(
reporter: DiagnosticReporter | undefined,
): import('@angular/compiler-cli/src/ngtsc/logging').Logger {
return {
level: 1, // Info level
debug(...args: string[]) {},
info(...args: string[]) {
reporter?.('info', args.join());
},
warn(...args: string[]) {
reporter?.('warning', args.join());
},
error(...args: string[]) {
reporter?.('error', args.join());
},
};
}
export default function (api: unknown, options: ApplicationPresetOptions) {
const presets = [];
const plugins = [];
let needRuntimeTransform = false;
if (options.angularLinker?.shouldLink) {
// Babel currently is synchronous so import cannot be used
const { createEs2015LinkerPlugin } =
require('@angular/compiler-cli/linker/babel') as typeof import('@angular/compiler-cli/linker/babel');
plugins.push(
createEs2015LinkerPlugin({
linkerJitMode: options.angularLinker.jitMode,
// This is a workaround until https://github.com/angular/angular/issues/42769 is fixed.
sourceMapping: false,
logger: createNgtscLogger(options.diagnosticReporter),
fileSystem: {
resolve: path.resolve,
exists: fs.existsSync,
dirname: path.dirname,
relative: path.relative,
readFile: fs.readFileSync,
// Node.JS types don't overlap the Compiler types.
// eslint-disable-next-line @typescript-eslint/no-explicit-any
} as any,
}),
);
}
if (options.forceES5) {
presets.push([
require('@babel/preset-env').default,
{
bugfixes: true,
modules: false,
// Comparable behavior to tsconfig target of ES5
targets: { ie: 9 },
exclude: ['transform-typeof-symbol'],
},
]);
needRuntimeTransform = true;
}
if (options.i18n) {
const { locale, missingTranslationBehavior, translation } = options.i18n;
const i18nPlugins = createI18nPlugins(
locale,
translation,
missingTranslationBehavior || 'ignore',
options.diagnosticReporter,
);
plugins.push(...i18nPlugins);
}
if (options.forceAsyncTransformation) {
// Always transform async/await to support Zone.js
plugins.push(
require('@babel/plugin-transform-async-to-generator').default,
require('@babel/plugin-proposal-async-generator-functions').default,
);
needRuntimeTransform = true;
}
if (needRuntimeTransform) {
// Babel equivalent to TypeScript's `importHelpers` option
plugins.push([
require('@babel/plugin-transform-runtime').default,
{
useESModules: true,
version: require('@babel/runtime/package.json').version,
absoluteRuntime: path.dirname(require.resolve('@babel/runtime/package.json')),
},
]);
}
return { presets, plugins };
}