Skip to content

Commit 16ee948

Browse files
author
Angular Builds
committed
2fc8076 refactor(@angular-devkit/build-angular): directly configure internal babel plugins for application builder
1 parent c6ccf6d commit 16ee948

File tree

5 files changed

+120
-32
lines changed

5 files changed

+120
-32
lines changed

package.json

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
{
22
"name": "@angular-devkit/build-angular",
3-
"version": "18.0.0-next.0+sha-950a445",
3+
"version": "18.0.0-next.0+sha-2fc8076",
44
"description": "Angular Webpack Build Facade",
55
"main": "src/index.js",
66
"typings": "src/index.d.ts",
77
"builders": "builders.json",
88
"dependencies": {
99
"@ampproject/remapping": "2.3.0",
10-
"@angular-devkit/architect": "github:angular/angular-devkit-architect-builds#950a445",
11-
"@angular-devkit/build-webpack": "github:angular/angular-devkit-build-webpack-builds#950a445",
12-
"@angular-devkit/core": "github:angular/angular-devkit-core-builds#950a445",
10+
"@angular-devkit/architect": "github:angular/angular-devkit-architect-builds#2fc8076",
11+
"@angular-devkit/build-webpack": "github:angular/angular-devkit-build-webpack-builds#2fc8076",
12+
"@angular-devkit/core": "github:angular/angular-devkit-core-builds#2fc8076",
1313
"@babel/core": "7.24.3",
1414
"@babel/generator": "7.24.1",
1515
"@babel/helper-annotate-as-pure": "7.22.5",
@@ -20,7 +20,7 @@
2020
"@babel/preset-env": "7.24.3",
2121
"@babel/runtime": "7.24.1",
2222
"@discoveryjs/json-ext": "0.5.7",
23-
"@ngtools/webpack": "github:angular/ngtools-webpack-builds#950a445",
23+
"@ngtools/webpack": "github:angular/ngtools-webpack-builds#2fc8076",
2424
"@vitejs/plugin-basic-ssl": "1.1.0",
2525
"ansi-colors": "4.1.3",
2626
"autoprefixer": "10.4.19",

src/tools/babel/plugins/index.d.ts

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
/**
2+
* @license
3+
* Copyright Google LLC All Rights Reserved.
4+
*
5+
* Use of this source code is governed by an MIT-style license that can be
6+
* found in the LICENSE file at https://angular.io/license
7+
*/
8+
export { default as adjustStaticMembers } from './adjust-static-class-members';
9+
export { default as adjustTypeScriptEnums } from './adjust-typescript-enums';
10+
export { default as elideAngularMetadata } from './elide-angular-metadata';
11+
export { default as markTopLevelPure } from './pure-toplevel-functions';

src/tools/babel/plugins/index.js

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
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+
var __importDefault = (this && this.__importDefault) || function (mod) {
10+
return (mod && mod.__esModule) ? mod : { "default": mod };
11+
};
12+
Object.defineProperty(exports, "__esModule", { value: true });
13+
exports.markTopLevelPure = exports.elideAngularMetadata = exports.adjustTypeScriptEnums = exports.adjustStaticMembers = void 0;
14+
var adjust_static_class_members_1 = require("./adjust-static-class-members");
15+
Object.defineProperty(exports, "adjustStaticMembers", { enumerable: true, get: function () { return __importDefault(adjust_static_class_members_1).default; } });
16+
var adjust_typescript_enums_1 = require("./adjust-typescript-enums");
17+
Object.defineProperty(exports, "adjustTypeScriptEnums", { enumerable: true, get: function () { return __importDefault(adjust_typescript_enums_1).default; } });
18+
var elide_angular_metadata_1 = require("./elide-angular-metadata");
19+
Object.defineProperty(exports, "elideAngularMetadata", { enumerable: true, get: function () { return __importDefault(elide_angular_metadata_1).default; } });
20+
var pure_toplevel_functions_1 = require("./pure-toplevel-functions");
21+
Object.defineProperty(exports, "markTopLevelPure", { enumerable: true, get: function () { return __importDefault(pure_toplevel_functions_1).default; } });

src/tools/esbuild/javascript-transformer-worker.js

+82-26
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,9 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
3434
};
3535
Object.defineProperty(exports, "__esModule", { value: true });
3636
const core_1 = require("@babel/core");
37+
const node_fs_1 = __importDefault(require("node:fs"));
38+
const node_path_1 = __importDefault(require("node:path"));
3739
const piscina_1 = __importDefault(require("piscina"));
38-
const application_1 = __importStar(require("../../tools/babel/presets/application"));
3940
const load_esm_1 = require("../../utils/load-esm");
4041
const textDecoder = new TextDecoder();
4142
const textEncoder = new TextEncoder();
@@ -47,22 +48,41 @@ async function transformJavaScript(request) {
4748
return piscina_1.default.move(textEncoder.encode(transformedData));
4849
}
4950
exports.default = transformJavaScript;
51+
/**
52+
* Cached instance of the compiler-cli linker's createEs2015LinkerPlugin function.
53+
*/
5054
let linkerPluginCreator;
55+
/**
56+
* Cached instance of the compiler-cli linker's needsLinking function.
57+
*/
58+
let needsLinking;
5159
async function transformWithBabel(filename, data, options) {
52-
const shouldLink = !options.skipLinker && (await (0, application_1.requiresLinking)(filename, data));
60+
const shouldLink = !options.skipLinker && (await requiresLinking(filename, data));
5361
const useInputSourcemap = options.sourcemap &&
5462
(!!options.thirdPartySourcemaps || !/[\\/]node_modules[\\/]/.test(filename));
63+
const plugins = [];
64+
// Lazy load the linker plugin only when linking is required
65+
if (shouldLink) {
66+
const linkerPlugin = await createLinkerPlugin(options);
67+
plugins.push(linkerPlugin);
68+
}
69+
if (options.advancedOptimizations) {
70+
const sideEffectFree = options.sideEffects === false;
71+
const safeAngularPackage = sideEffectFree && /[\\/]node_modules[\\/]@angular[\\/]/.test(filename);
72+
const { adjustStaticMembers, adjustTypeScriptEnums, elideAngularMetadata, markTopLevelPure } = await Promise.resolve().then(() => __importStar(require('../babel/plugins')));
73+
if (safeAngularPackage) {
74+
plugins.push(markTopLevelPure);
75+
}
76+
plugins.push(elideAngularMetadata, adjustTypeScriptEnums, [
77+
adjustStaticMembers,
78+
{ wrapDecorators: sideEffectFree },
79+
]);
80+
}
5581
// If no additional transformations are needed, return the data directly
56-
if (!options.advancedOptimizations && !shouldLink) {
82+
if (plugins.length === 0) {
5783
// Strip sourcemaps if they should not be used
5884
return useInputSourcemap ? data : data.replace(/^\/\/# sourceMappingURL=[^\r\n]*/gm, '');
5985
}
60-
const sideEffectFree = options.sideEffects === false;
61-
const safeAngularPackage = sideEffectFree && /[\\/]node_modules[\\/]@angular[\\/]/.test(filename);
62-
// Lazy load the linker plugin only when linking is required
63-
if (shouldLink) {
64-
linkerPluginCreator ??= (await (0, load_esm_1.loadEsmModule)('@angular/compiler-cli/linker/babel')).createEs2015LinkerPlugin;
65-
}
6686
const result = await (0, core_1.transformAsync)(data, {
6787
filename,
6888
inputSourceMap: (useInputSourcemap ? undefined : false),
@@ -71,23 +91,7 @@ async function transformWithBabel(filename, data, options) {
7191
configFile: false,
7292
babelrc: false,
7393
browserslistConfigFile: false,
74-
plugins: [],
75-
presets: [
76-
[
77-
application_1.default,
78-
{
79-
angularLinker: linkerPluginCreator && {
80-
shouldLink,
81-
jitMode: options.jit,
82-
linkerPluginCreator,
83-
},
84-
optimize: options.advancedOptimizations && {
85-
pureTopLevel: safeAngularPackage,
86-
wrapDecorators: sideEffectFree,
87-
},
88-
},
89-
],
90-
],
94+
plugins,
9195
});
9296
const outputCode = result?.code ?? data;
9397
// Strip sourcemaps if they should not be used.
@@ -96,3 +100,55 @@ async function transformWithBabel(filename, data, options) {
96100
? outputCode
97101
: outputCode.replace(/^\/\/# sourceMappingURL=[^\r\n]*/gm, '');
98102
}
103+
async function requiresLinking(path, source) {
104+
// @angular/core and @angular/compiler will cause false positives
105+
// Also, TypeScript files do not require linking
106+
if (/[\\/]@angular[\\/](?:compiler|core)|\.tsx?$/.test(path)) {
107+
return false;
108+
}
109+
if (!needsLinking) {
110+
// Load ESM `@angular/compiler-cli/linker` using the TypeScript dynamic import workaround.
111+
// Once TypeScript provides support for keeping the dynamic import this workaround can be
112+
// changed to a direct dynamic import.
113+
const linkerModule = await (0, load_esm_1.loadEsmModule)('@angular/compiler-cli/linker');
114+
needsLinking = linkerModule.needsLinking;
115+
}
116+
return needsLinking(path, source);
117+
}
118+
async function createLinkerPlugin(options) {
119+
linkerPluginCreator ??= (await (0, load_esm_1.loadEsmModule)('@angular/compiler-cli/linker/babel')).createEs2015LinkerPlugin;
120+
const linkerPlugin = linkerPluginCreator({
121+
linkerJitMode: options.jit,
122+
// This is a workaround until https://github.com/angular/angular/issues/42769 is fixed.
123+
sourceMapping: false,
124+
logger: {
125+
level: 1, // Info level
126+
debug(...args) {
127+
// eslint-disable-next-line no-console
128+
console.debug(args);
129+
},
130+
info(...args) {
131+
// eslint-disable-next-line no-console
132+
console.info(args);
133+
},
134+
warn(...args) {
135+
// eslint-disable-next-line no-console
136+
console.warn(args);
137+
},
138+
error(...args) {
139+
// eslint-disable-next-line no-console
140+
console.error(args);
141+
},
142+
},
143+
fileSystem: {
144+
resolve: node_path_1.default.resolve,
145+
exists: node_fs_1.default.existsSync,
146+
dirname: node_path_1.default.dirname,
147+
relative: node_path_1.default.relative,
148+
readFile: node_fs_1.default.readFileSync,
149+
// Node.JS types don't overlap the Compiler types.
150+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
151+
},
152+
});
153+
return linkerPlugin;
154+
}

uniqueId

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
Fri Mar 22 2024 17:27:22 GMT+0000 (Coordinated Universal Time)
1+
Fri Mar 22 2024 19:25:44 GMT+0000 (Coordinated Universal Time)

0 commit comments

Comments
 (0)