Skip to content

Commit eee0eff

Browse files
alan-agius4clydin
authored andcommitted
refactor: avoid loading babel-loader when running esbuild
This commit refactor the code to avoid loading babel-loader when not needed.
1 parent ba76862 commit eee0eff

File tree

3 files changed

+32
-28
lines changed

3 files changed

+32
-28
lines changed

packages/angular_devkit/build_angular/src/tools/babel/presets/application.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,12 @@ import { strict as assert } from 'assert';
1717
import browserslist from 'browserslist';
1818
import * as fs from 'fs';
1919
import * as path from 'path';
20+
import { loadEsmModule } from '../../../utils/load-esm';
21+
22+
/**
23+
* Cached instance of the compiler-cli linker's needsLinking function.
24+
*/
25+
let needsLinking: typeof import('@angular/compiler-cli/linker').needsLinking | undefined;
2026

2127
/**
2228
* List of browsers which are affected by a WebKit bug where class field
@@ -276,3 +282,23 @@ export default function (api: unknown, options: ApplicationPresetOptions) {
276282

277283
return { presets, plugins };
278284
}
285+
286+
export async function requiresLinking(path: string, source: string): Promise<boolean> {
287+
// @angular/core and @angular/compiler will cause false positives
288+
// Also, TypeScript files do not require linking
289+
if (/[\\/]@angular[\\/](?:compiler|core)|\.tsx?$/.test(path)) {
290+
return false;
291+
}
292+
293+
if (!needsLinking) {
294+
// Load ESM `@angular/compiler-cli/linker` using the TypeScript dynamic import workaround.
295+
// Once TypeScript provides support for keeping the dynamic import this workaround can be
296+
// changed to a direct dynamic import.
297+
const linkerModule = await loadEsmModule<typeof import('@angular/compiler-cli/linker')>(
298+
'@angular/compiler-cli/linker',
299+
);
300+
needsLinking = linkerModule.needsLinking;
301+
}
302+
303+
return needsLinking(path, source);
304+
}

packages/angular_devkit/build_angular/src/tools/babel/webpack-loader.ts

Lines changed: 5 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,11 @@
99
import { custom } from 'babel-loader';
1010
import { loadEsmModule } from '../../utils/load-esm';
1111
import { VERSION } from '../../utils/package-version';
12-
import { ApplicationPresetOptions, I18nPluginCreators } from './presets/application';
12+
import {
13+
ApplicationPresetOptions,
14+
I18nPluginCreators,
15+
requiresLinking,
16+
} from './presets/application';
1317

1418
interface AngularCustomOptions extends Omit<ApplicationPresetOptions, 'instrumentCode'> {
1519
instrumentCode?: {
@@ -21,11 +25,6 @@ interface AngularCustomOptions extends Omit<ApplicationPresetOptions, 'instrumen
2125

2226
export type AngularBabelLoaderOptions = AngularCustomOptions & Record<string, unknown>;
2327

24-
/**
25-
* Cached instance of the compiler-cli linker's needsLinking function.
26-
*/
27-
let needsLinking: typeof import('@angular/compiler-cli/linker').needsLinking | undefined;
28-
2928
/**
3029
* Cached instance of the compiler-cli linker's Babel plugin factory function.
3130
*/
@@ -38,26 +37,6 @@ let linkerPluginCreator:
3837
*/
3938
let i18nPluginCreators: I18nPluginCreators | undefined;
4039

41-
export async function requiresLinking(path: string, source: string): Promise<boolean> {
42-
// @angular/core and @angular/compiler will cause false positives
43-
// Also, TypeScript files do not require linking
44-
if (/[\\/]@angular[\\/](?:compiler|core)|\.tsx?$/.test(path)) {
45-
return false;
46-
}
47-
48-
if (!needsLinking) {
49-
// Load ESM `@angular/compiler-cli/linker` using the TypeScript dynamic import workaround.
50-
// Once TypeScript provides support for keeping the dynamic import this workaround can be
51-
// changed to a direct dynamic import.
52-
const linkerModule = await loadEsmModule<typeof import('@angular/compiler-cli/linker')>(
53-
'@angular/compiler-cli/linker',
54-
);
55-
needsLinking = linkerModule.needsLinking;
56-
}
57-
58-
return needsLinking(path, source);
59-
}
60-
6140
// eslint-disable-next-line max-lines-per-function
6241
export default custom<ApplicationPresetOptions>(() => {
6342
const baseOptions = Object.freeze({

packages/angular_devkit/build_angular/src/tools/esbuild/javascript-transformer-worker.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,7 @@
88

99
import { transformAsync } from '@babel/core';
1010
import { readFile } from 'node:fs/promises';
11-
import angularApplicationPreset from '../../tools/babel/presets/application';
12-
import { requiresLinking } from '../../tools/babel/webpack-loader';
11+
import angularApplicationPreset, { requiresLinking } from '../../tools/babel/presets/application';
1312
import { loadEsmModule } from '../../utils/load-esm';
1413

1514
interface JavaScriptTransformRequest {

0 commit comments

Comments
 (0)