1 | "use strict";
|
---|
2 | /**
|
---|
3 | * @license
|
---|
4 | * Copyright Google LLC All Rights Reserved.
|
---|
5 | *
|
---|
6 | * Use of this source code is governed by an MIT-style license that can be
|
---|
7 | * found in the LICENSE file at https://angular.io/license
|
---|
8 | */
|
---|
9 | Object.defineProperty(exports, "__esModule", { value: true });
|
---|
10 | const linker_1 = require("@angular/compiler-cli/linker");
|
---|
11 | const babel_loader_1 = require("babel-loader");
|
---|
12 | const typescript_1 = require("typescript");
|
---|
13 | function requiresLinking(path, source) {
|
---|
14 | // @angular/core and @angular/compiler will cause false positives
|
---|
15 | // Also, TypeScript files do not require linking
|
---|
16 | if (/[\\\/]@angular[\\\/](?:compiler|core)|\.tsx?$/.test(path)) {
|
---|
17 | return false;
|
---|
18 | }
|
---|
19 | return linker_1.needsLinking(path, source);
|
---|
20 | }
|
---|
21 | exports.default = babel_loader_1.custom(() => {
|
---|
22 | const baseOptions = Object.freeze({
|
---|
23 | babelrc: false,
|
---|
24 | configFile: false,
|
---|
25 | compact: false,
|
---|
26 | cacheCompression: false,
|
---|
27 | sourceType: 'unambiguous',
|
---|
28 | inputSourceMap: false,
|
---|
29 | });
|
---|
30 | return {
|
---|
31 | async customOptions({ i18n, scriptTarget, aot, optimize, ...rawOptions }, { source }) {
|
---|
32 | var _a, _b;
|
---|
33 | // Must process file if plugins are added
|
---|
34 | let shouldProcess = Array.isArray(rawOptions.plugins) && rawOptions.plugins.length > 0;
|
---|
35 | const customOptions = {
|
---|
36 | forceAsyncTransformation: false,
|
---|
37 | forceES5: false,
|
---|
38 | angularLinker: undefined,
|
---|
39 | i18n: undefined,
|
---|
40 | };
|
---|
41 | // Analyze file for linking
|
---|
42 | if (await requiresLinking(this.resourcePath, source)) {
|
---|
43 | customOptions.angularLinker = {
|
---|
44 | shouldLink: true,
|
---|
45 | jitMode: aot !== true,
|
---|
46 | };
|
---|
47 | shouldProcess = true;
|
---|
48 | }
|
---|
49 | // Analyze for ES target processing
|
---|
50 | const esTarget = scriptTarget;
|
---|
51 | if (esTarget !== undefined) {
|
---|
52 | if (esTarget < typescript_1.ScriptTarget.ES2015) {
|
---|
53 | // TypeScript files will have already been downlevelled
|
---|
54 | customOptions.forceES5 = !/\.tsx?$/.test(this.resourcePath);
|
---|
55 | }
|
---|
56 | else if (esTarget >= typescript_1.ScriptTarget.ES2017 || /\.[cm]?js$/.test(this.resourcePath)) {
|
---|
57 | // Application code (TS files) will only contain native async if target is ES2017+.
|
---|
58 | // However, third-party libraries can regardless of the target option.
|
---|
59 | // APF packages with code in [f]esm2015 directories is downlevelled to ES2015 and
|
---|
60 | // will not have native async.
|
---|
61 | customOptions.forceAsyncTransformation =
|
---|
62 | !/[\\\/][_f]?esm2015[\\\/]/.test(this.resourcePath) && source.includes('async');
|
---|
63 | }
|
---|
64 | shouldProcess || (shouldProcess = customOptions.forceAsyncTransformation || customOptions.forceES5);
|
---|
65 | }
|
---|
66 | // Analyze for i18n inlining
|
---|
67 | if (i18n &&
|
---|
68 | !/[\\\/]@angular[\\\/](?:compiler|localize)/.test(this.resourcePath) &&
|
---|
69 | source.includes('$localize')) {
|
---|
70 | customOptions.i18n = i18n;
|
---|
71 | shouldProcess = true;
|
---|
72 | }
|
---|
73 | if (optimize) {
|
---|
74 | const angularPackage = /[\\\/]node_modules[\\\/]@angular[\\\/]/.test(this.resourcePath);
|
---|
75 | customOptions.optimize = {
|
---|
76 | // Angular packages provide additional tested side effects guarantees and can use
|
---|
77 | // otherwise unsafe optimizations.
|
---|
78 | looseEnums: angularPackage,
|
---|
79 | pureTopLevel: angularPackage,
|
---|
80 | // JavaScript modules that are marked as side effect free are considered to have
|
---|
81 | // no decorators that contain non-local effects.
|
---|
82 | wrapDecorators: !!((_b = (_a = this._module) === null || _a === void 0 ? void 0 : _a.factoryMeta) === null || _b === void 0 ? void 0 : _b.sideEffectFree),
|
---|
83 | };
|
---|
84 | shouldProcess = true;
|
---|
85 | }
|
---|
86 | // Add provided loader options to default base options
|
---|
87 | const loaderOptions = {
|
---|
88 | ...baseOptions,
|
---|
89 | ...rawOptions,
|
---|
90 | cacheIdentifier: JSON.stringify({
|
---|
91 | buildAngular: require('../../package.json').version,
|
---|
92 | customOptions,
|
---|
93 | baseOptions,
|
---|
94 | rawOptions,
|
---|
95 | }),
|
---|
96 | };
|
---|
97 | // Skip babel processing if no actions are needed
|
---|
98 | if (!shouldProcess) {
|
---|
99 | // Force the current file to be ignored
|
---|
100 | loaderOptions.ignore = [() => true];
|
---|
101 | }
|
---|
102 | return { custom: customOptions, loader: loaderOptions };
|
---|
103 | },
|
---|
104 | config(configuration, { customOptions }) {
|
---|
105 | var _a;
|
---|
106 | const plugins = (_a = configuration.options.plugins) !== null && _a !== void 0 ? _a : [];
|
---|
107 | if (customOptions.optimize) {
|
---|
108 | if (customOptions.optimize.pureTopLevel) {
|
---|
109 | plugins.push(require('./plugins/pure-toplevel-functions').default);
|
---|
110 | }
|
---|
111 | plugins.push(require('./plugins/elide-angular-metadata').default, [
|
---|
112 | require('./plugins/adjust-typescript-enums').default,
|
---|
113 | { loose: customOptions.optimize.looseEnums },
|
---|
114 | ], [
|
---|
115 | require('./plugins/adjust-static-class-members').default,
|
---|
116 | { wrapDecorators: customOptions.optimize.wrapDecorators },
|
---|
117 | ]);
|
---|
118 | }
|
---|
119 | return {
|
---|
120 | ...configuration.options,
|
---|
121 | // Workaround for https://github.com/babel/babel-loader/pull/896 is available
|
---|
122 | // Delete once the above PR is released
|
---|
123 | // eslint-disable-next-line @typescript-eslint/no-explicit-any
|
---|
124 | inputSourceMap: configuration.options.inputSourceMap || false,
|
---|
125 | plugins,
|
---|
126 | presets: [
|
---|
127 | ...(configuration.options.presets || []),
|
---|
128 | [
|
---|
129 | require('./presets/application').default,
|
---|
130 | {
|
---|
131 | ...customOptions,
|
---|
132 | diagnosticReporter: (type, message) => {
|
---|
133 | switch (type) {
|
---|
134 | case 'error':
|
---|
135 | this.emitError(message);
|
---|
136 | break;
|
---|
137 | case 'info':
|
---|
138 | // Webpack does not currently have an informational diagnostic
|
---|
139 | case 'warning':
|
---|
140 | this.emitWarning(message);
|
---|
141 | break;
|
---|
142 | }
|
---|
143 | },
|
---|
144 | },
|
---|
145 | ],
|
---|
146 | ],
|
---|
147 | };
|
---|
148 | },
|
---|
149 | };
|
---|
150 | });
|
---|