-
Notifications
You must be signed in to change notification settings - Fork 12k
/
typescript.ts
265 lines (232 loc) · 7.58 KB
/
typescript.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
/**
* @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
*/
// tslint:disable
// TODO: cleanup this file, it's copied as is from Angular CLI.
import { CompilerOptions } from '@angular/compiler-cli';
import { buildOptimizerLoaderPath } from '@angular-devkit/build-optimizer';
import { getSystemPath } from '@angular-devkit/core';
import {
AngularCompilerPlugin,
AngularCompilerPluginOptions,
NgToolsLoader,
PLATFORM,
ivy,
} from '@ngtools/webpack';
import * as path from 'path';
import { RuleSetLoader } from 'webpack';
import { WebpackConfigOptions, BuildOptions } from '../../utils/build-options';
import { legacyIvyPluginEnabled } from '../../utils/environment-options';
function canUseIvyPlugin(wco: WebpackConfigOptions): boolean {
// Can only be used with Ivy
if (!wco.tsConfig.options.enableIvy) {
return false;
}
// Allow fallback to legacy build system via environment variable ('NG_BUILD_IVY_LEGACY=1')
if (legacyIvyPluginEnabled) {
wco.logger.warn(
'"NG_BUILD_IVY_LEGACY" environment variable detected. Using legacy Ivy build system.',
);
return false;
}
// Lazy modules option uses the deprecated string format for lazy routes
if (wco.buildOptions.lazyModules && wco.buildOptions.lazyModules.length > 0) {
return false;
}
// This pass relies on internals of the original plugin
if (wco.buildOptions.experimentalRollupPass) {
return false;
}
return true;
}
function createIvyPlugin(
wco: WebpackConfigOptions,
aot: boolean,
tsconfig: string,
): ivy.AngularWebpackPlugin {
const { buildOptions } = wco;
const optimize = buildOptions.optimization.scripts;
const compilerOptions: CompilerOptions = {
sourceMap: buildOptions.sourceMap.scripts,
};
if (buildOptions.preserveSymlinks !== undefined) {
compilerOptions.preserveSymlinks = buildOptions.preserveSymlinks;
}
const fileReplacements: Record<string, string> = {};
if (buildOptions.fileReplacements) {
for (const replacement of buildOptions.fileReplacements) {
fileReplacements[getSystemPath(replacement.replace)] = getSystemPath(replacement.with);
}
}
return new ivy.AngularWebpackPlugin({
tsconfig,
compilerOptions,
fileReplacements,
jitMode: !aot,
emitNgModuleScope: !optimize,
suppressZoneJsIncompatibilityWarning: true,
});
}
function _pluginOptionsOverrides(
buildOptions: BuildOptions,
pluginOptions: AngularCompilerPluginOptions
): AngularCompilerPluginOptions {
const compilerOptions = {
...(pluginOptions.compilerOptions || {})
}
const hostReplacementPaths: { [replace: string]: string } = {};
if (buildOptions.fileReplacements) {
for (const replacement of buildOptions.fileReplacements) {
hostReplacementPaths[replacement.replace] = replacement.with;
}
}
if (buildOptions.preserveSymlinks) {
compilerOptions.preserveSymlinks = true;
}
return {
...pluginOptions,
hostReplacementPaths,
compilerOptions
};
}
function _createAotPlugin(
wco: WebpackConfigOptions,
options: AngularCompilerPluginOptions,
i18nExtract = false,
) {
const { root, buildOptions } = wco;
const i18nInFile = buildOptions.i18nFile
? path.resolve(root, buildOptions.i18nFile)
: undefined;
const i18nFileAndFormat = i18nExtract
? {
i18nOutFile: buildOptions.i18nFile,
i18nOutFormat: buildOptions.i18nFormat,
} : {
i18nInFile: i18nInFile,
i18nInFormat: buildOptions.i18nFormat,
};
const compilerOptions = options.compilerOptions || {};
if (i18nExtract) {
// Extraction of i18n is still using the legacy VE pipeline
compilerOptions.enableIvy = false;
}
const additionalLazyModules: { [module: string]: string } = {};
if (buildOptions.lazyModules) {
for (const lazyModule of buildOptions.lazyModules) {
additionalLazyModules[lazyModule] = path.resolve(
root,
lazyModule,
);
}
}
let pluginOptions: AngularCompilerPluginOptions = {
mainPath: path.join(root, buildOptions.main),
...i18nFileAndFormat,
locale: buildOptions.i18nLocale,
platform: buildOptions.platform === 'server' ? PLATFORM.Server : PLATFORM.Browser,
missingTranslation: buildOptions.i18nMissingTranslation,
sourceMap: buildOptions.sourceMap.scripts,
additionalLazyModules,
nameLazyFiles: buildOptions.namedChunks,
forkTypeChecker: buildOptions.forkTypeChecker,
contextElementDependencyConstructor: require('webpack/lib/dependencies/ContextElementDependency'),
logger: wco.logger,
directTemplateLoading: true,
...options,
compilerOptions,
suppressZoneJsIncompatibilityWarning: true,
};
pluginOptions = _pluginOptionsOverrides(buildOptions, pluginOptions);
return new AngularCompilerPlugin(pluginOptions);
}
export function getNonAotConfig(wco: WebpackConfigOptions) {
const { tsConfigPath } = wco;
const useIvyOnlyPlugin = canUseIvyPlugin(wco);
return {
module: {
rules: [
{
test: useIvyOnlyPlugin ? /\.[jt]sx?$/ : /\.tsx?$/,
loader: useIvyOnlyPlugin
? ivy.AngularWebpackLoaderPath
: NgToolsLoader,
},
],
},
plugins: [
useIvyOnlyPlugin
? createIvyPlugin(wco, false, tsConfigPath)
: _createAotPlugin(wco, { tsConfigPath, skipCodeGeneration: true }),
],
};
}
export function getAotConfig(wco: WebpackConfigOptions, i18nExtract = false) {
const { tsConfigPath, buildOptions } = wco;
const optimize = buildOptions.optimization.scripts;
const useIvyOnlyPlugin = canUseIvyPlugin(wco) && !i18nExtract;
let buildOptimizerRules: RuleSetLoader[] = [];
if (buildOptions.buildOptimizer) {
buildOptimizerRules = [{
loader: buildOptimizerLoaderPath,
options: { sourceMap: buildOptions.sourceMap.scripts }
}];
}
return {
module: {
rules: [
{
test: useIvyOnlyPlugin ? /\.tsx?$/ : /(?:\.ngfactory\.js|\.ngstyle\.js|\.tsx?)$/,
use: [
...buildOptimizerRules,
useIvyOnlyPlugin ? ivy.AngularWebpackLoaderPath : NgToolsLoader,
],
},
// "allowJs" support with ivy plugin - ensures build optimizer is not run twice
...(useIvyOnlyPlugin
? [
{
test: /\.jsx?$/,
use: [ivy.AngularWebpackLoaderPath],
},
]
: []),
],
},
plugins: [
useIvyOnlyPlugin
? createIvyPlugin(wco, true, tsConfigPath)
: _createAotPlugin(
wco,
{ tsConfigPath, emitClassMetadata: !optimize, emitNgModuleScope: !optimize },
i18nExtract,
),
],
};
}
export function getTypescriptWorkerPlugin(wco: WebpackConfigOptions, workerTsConfigPath: string) {
if (canUseIvyPlugin(wco)) {
return createIvyPlugin(wco, false, workerTsConfigPath);
}
const { buildOptions } = wco;
let pluginOptions: AngularCompilerPluginOptions = {
skipCodeGeneration: true,
tsConfigPath: workerTsConfigPath,
mainPath: undefined,
platform: PLATFORM.Browser,
sourceMap: buildOptions.sourceMap.scripts,
forkTypeChecker: buildOptions.forkTypeChecker,
contextElementDependencyConstructor: require('webpack/lib/dependencies/ContextElementDependency'),
logger: wco.logger,
// Run no transformers.
platformTransformers: [],
// Don't attempt lazy route discovery.
discoverLazyRoutes: false,
};
pluginOptions = _pluginOptionsOverrides(buildOptions, pluginOptions);
return new AngularCompilerPlugin(pluginOptions);
}