Skip to content

Commit 141d74d

Browse files
clydinangular-robot[bot]
authored andcommitted
fix(@angular-devkit/build-angular): attempt relative global script read first in esbuild builder
When using a global script (`scripts` option) with the esbuild-based browser application builder, an attempt to read the script as a relative path from the workspace root will be performed first. This avoids the need to perform a potentially expensive module resolution attempt for files that are directly available and also ensures the relative paths are given priority over any potential modules with the same name. This matches prior behavior that also preferred relative paths.
1 parent ac95732 commit 141d74d

File tree

1 file changed

+28
-13
lines changed

1 file changed

+28
-13
lines changed

packages/angular_devkit/build_angular/src/builders/browser-esbuild/global-scripts.ts

Lines changed: 28 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ import type { BuildOptions } from 'esbuild';
1010
import MagicString, { Bundle } from 'magic-string';
1111
import assert from 'node:assert';
1212
import { readFile } from 'node:fs/promises';
13+
import path from 'node:path';
14+
import { assertIsError } from '../../utils/error';
1315
import { NormalizedBrowserOptions } from './options';
1416
import { createSourcemapIngorelistPlugin } from './sourcemap-ignorelist-plugin';
1517

@@ -96,23 +98,36 @@ export function createGlobalScriptsBundleOptions(
9698
// Global scripts are concatenated using magic-string instead of bundled via esbuild.
9799
const bundleContent = new Bundle();
98100
for (const filename of files) {
99-
const resolveResult = await build.resolve(filename, {
100-
kind: 'entry-point',
101-
resolveDir: workspaceRoot,
102-
});
101+
let fileContent;
102+
try {
103+
// Attempt to read as a relative path from the workspace root
104+
fileContent = await readFile(path.join(workspaceRoot, filename), 'utf-8');
105+
} catch (e) {
106+
assertIsError(e);
107+
if (e.code !== 'ENOENT') {
108+
throw e;
109+
}
103110

104-
if (resolveResult.errors.length) {
105-
// Remove resolution failure notes about marking as external since it doesn't apply
106-
// to global scripts.
107-
resolveResult.errors.forEach((error) => (error.notes = []));
111+
// If not found attempt to resolve as a module specifier
112+
const resolveResult = await build.resolve(filename, {
113+
kind: 'entry-point',
114+
resolveDir: workspaceRoot,
115+
});
108116

109-
return {
110-
errors: resolveResult.errors,
111-
warnings: resolveResult.warnings,
112-
};
117+
if (resolveResult.errors.length) {
118+
// Remove resolution failure notes about marking as external since it doesn't apply
119+
// to global scripts.
120+
resolveResult.errors.forEach((error) => (error.notes = []));
121+
122+
return {
123+
errors: resolveResult.errors,
124+
warnings: resolveResult.warnings,
125+
};
126+
}
127+
128+
fileContent = await readFile(resolveResult.path, 'utf-8');
113129
}
114130

115-
const fileContent = await readFile(resolveResult.path, 'utf-8');
116131
bundleContent.addSource(new MagicString(fileContent, { filename }));
117132
}
118133

0 commit comments

Comments
 (0)