-
Notifications
You must be signed in to change notification settings - Fork 12k
/
Copy pathwebpack-loader.ts
140 lines (127 loc) · 4.61 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
/**
* @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 { custom } from 'babel-loader';
import { ScriptTarget } from 'typescript';
interface AngularCustomOptions {
forceAsyncTransformation: boolean;
forceES5: boolean;
shouldLink: boolean;
}
/**
* Cached linker check utility function
*
* If undefined, not yet been imported
* If null, attempted import failed and no linker support
* If function, import succeeded and linker supported
*/
let needsLinking: undefined | null | typeof import('@angular/compiler-cli/linker').needsLinking;
async function checkLinking(
path: string,
source: string,
): Promise<{ hasLinkerSupport?: boolean; requiresLinking: 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 { requiresLinking: false };
}
if (needsLinking !== null) {
try {
if (needsLinking === undefined) {
needsLinking = (await import('@angular/compiler-cli/linker')).needsLinking;
}
// If the linker entry point is present then there is linker support
return { hasLinkerSupport: true, requiresLinking: needsLinking(path, source) };
} catch {
needsLinking = null;
}
}
// Fallback for Angular versions less than 11.1.0 with no linker support.
// This information is used to issue errors if a partially compiled library is used when unsupported.
return {
hasLinkerSupport: false,
requiresLinking:
source.includes('ɵɵngDeclareDirective') || source.includes('ɵɵngDeclareComponent'),
};
}
export default custom<AngularCustomOptions>(() => {
const baseOptions = Object.freeze({
babelrc: false,
configFile: false,
compact: false,
cacheCompression: false,
sourceType: 'unambiguous',
});
return {
async customOptions({ scriptTarget, ...loaderOptions }, { source }) {
// Must process file if plugins are added
let shouldProcess = Array.isArray(loaderOptions.plugins) && loaderOptions.plugins.length > 0;
// Analyze file for linking
let shouldLink = false;
const { hasLinkerSupport, requiresLinking } = await checkLinking(this.resourcePath, source);
if (requiresLinking && !hasLinkerSupport) {
// Cannot link if there is no linker support
this.emitError(
'File requires the Angular linker. "@angular/compiler-cli" version 11.1.0 or greater is needed.',
);
} else {
shouldLink = requiresLinking;
}
shouldProcess ||= shouldLink;
// Analyze for ES target processing
let forceES5 = false;
let forceAsyncTransformation = false;
const esTarget = scriptTarget as ScriptTarget;
if (esTarget < ScriptTarget.ES2015) {
// TypeScript files will have already been downlevelled
forceES5 = !/\.tsx?$/.test(this.resourcePath);
} else if (esTarget >= ScriptTarget.ES2017) {
forceAsyncTransformation = source.includes('async');
}
shouldProcess ||= forceAsyncTransformation || forceES5;
// Add provided loader options to default base options
const options: Record<string, unknown> = {
...baseOptions,
...loaderOptions,
};
// Skip babel processing if no actions are needed
if (!shouldProcess) {
// Force the current file to be ignored
options.ignore = [() => true];
}
return { custom: { forceAsyncTransformation, forceES5, shouldLink }, loader: options };
},
config(configuration, { customOptions }) {
return {
...configuration.options,
presets: [
...(configuration.options.presets || []),
[
require('./presets/application').default,
{
angularLinker: customOptions.shouldLink,
forceES5: customOptions.forceES5,
forceAsyncTransformation: customOptions.forceAsyncTransformation,
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 import('./presets/application').ApplicationPresetOptions,
],
],
};
},
};
});