-
Notifications
You must be signed in to change notification settings - Fork 12k
/
Copy pathwebpack-loader.ts
168 lines (151 loc) · 5.58 KB
/
webpack-loader.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
/**
* @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 { needsLinking } from '@angular/compiler-cli/linker';
import { custom } from 'babel-loader';
import { ScriptTarget } from 'typescript';
import { ApplicationPresetOptions } from './presets/application';
interface AngularCustomOptions extends Pick<ApplicationPresetOptions, 'angularLinker' | 'i18n'> {
forceAsyncTransformation: boolean;
forceES5: boolean;
optimize?: {
pureTopLevel: boolean;
wrapDecorators: boolean;
};
}
function requiresLinking(path: string, source: string): boolean {
// @angular/core and @angular/compiler will cause false positives
// Also, TypeScript files do not require linking
if (/[\\\/]@angular[\\\/](?:compiler|core)|\.tsx?$/.test(path)) {
return false;
}
return needsLinking(path, source);
}
export default custom<AngularCustomOptions>(() => {
const baseOptions = Object.freeze({
babelrc: false,
configFile: false,
compact: false,
cacheCompression: false,
sourceType: 'unambiguous',
inputSourceMap: false,
});
return {
async customOptions({ i18n, scriptTarget, aot, optimize, ...rawOptions }, { source }) {
// Must process file if plugins are added
let shouldProcess = Array.isArray(rawOptions.plugins) && rawOptions.plugins.length > 0;
const customOptions: AngularCustomOptions = {
forceAsyncTransformation: false,
forceES5: false,
angularLinker: undefined,
i18n: undefined,
};
// Analyze file for linking
if (await requiresLinking(this.resourcePath, source)) {
customOptions.angularLinker = {
shouldLink: true,
jitMode: aot !== true,
};
shouldProcess = true;
}
// Analyze for ES target processing
const esTarget = scriptTarget as ScriptTarget | undefined;
if (esTarget !== undefined) {
if (esTarget < ScriptTarget.ES2015) {
// TypeScript files will have already been downlevelled
customOptions.forceES5 = !/\.tsx?$/.test(this.resourcePath);
} else if (esTarget >= ScriptTarget.ES2017) {
customOptions.forceAsyncTransformation =
!/[\\\/][_f]?esm2015[\\\/]/.test(this.resourcePath) && source.includes('async');
}
shouldProcess ||= customOptions.forceAsyncTransformation || customOptions.forceES5;
}
// Analyze for i18n inlining
if (
i18n &&
!/[\\\/]@angular[\\\/](?:compiler|localize)/.test(this.resourcePath) &&
source.includes('$localize')
) {
customOptions.i18n = i18n as ApplicationPresetOptions['i18n'];
shouldProcess = true;
}
if (optimize) {
customOptions.optimize = {
// Angular packages provide additional tested side effects guarantees and can use
// otherwise unsafe optimizations.
pureTopLevel: /[\\\/]node_modules[\\\/]@angular[\\\/]/.test(this.resourcePath),
// JavaScript modules that are marked as side effect free are considered to have
// no decorators that contain non-local effects.
wrapDecorators: !!this._module?.factoryMeta?.sideEffectFree,
};
shouldProcess = true;
}
// Add provided loader options to default base options
const loaderOptions: Record<string, unknown> = {
...baseOptions,
...rawOptions,
cacheIdentifier: JSON.stringify({
buildAngular: require('../../package.json').version,
customOptions,
baseOptions,
rawOptions,
}),
};
// Skip babel processing if no actions are needed
if (!shouldProcess) {
// Force the current file to be ignored
loaderOptions.ignore = [() => true];
}
return { custom: customOptions, loader: loaderOptions };
},
config(configuration, { customOptions }) {
const plugins = configuration.options.plugins ?? [];
if (customOptions.optimize) {
if (customOptions.optimize.pureTopLevel) {
plugins.push(require('./plugins/pure-toplevel-functions').default);
}
plugins.push(
require('./plugins/elide-angular-metadata').default,
require('./plugins/adjust-typescript-enums').default,
[
require('./plugins/adjust-static-class-members').default,
{ wrapDecorators: customOptions.optimize.wrapDecorators },
],
);
}
return {
...configuration.options,
// Workaround for https://github.com/babel/babel-loader/pull/896 is available
// Delete once the above PR is released
// eslint-disable-next-line @typescript-eslint/no-explicit-any
inputSourceMap: configuration.options.inputSourceMap || (false as any), // Typings are not correct
plugins,
presets: [
...(configuration.options.presets || []),
[
require('./presets/application').default,
{
...customOptions,
diagnosticReporter: (type, message) => {
switch (type) {
case 'error':
this.emitError(message);
break;
case 'info':
// Webpack does not currently have an informational diagnostic
case 'warning':
this.emitWarning(message);
break;
}
},
} as ApplicationPresetOptions,
],
],
};
},
};
});