Skip to content

Commit 07fac5b

Browse files
committed
build: simplify legacy bundle creation and fix build-tooling dependency
The legacy bundle creation relies on `@angular/build-tooling`. As ideally we don't have any of such scripts, that rely on build tooling outside of Bazel, we simply add our own simple linker ESBuild plugin, while we save on complexity by using our existing tsconfig for path mappings. if we find more scripts needing build tooling scripts outside of Bazel that aren't legacy tooling, we should come up with a better solution.
1 parent ddbd4a5 commit 07fac5b

File tree

1 file changed

+22
-46
lines changed

1 file changed

+22
-46
lines changed

scripts/create-legacy-tests-bundle.mjs

+22-46
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
#!/usr/bin/env node
22

3-
import {createEsbuildAngularOptimizePlugin} from '@angular/build-tooling/shared-scripts/angular-optimization/esbuild-plugin.mjs';
3+
import babel from '@babel/core';
44
import child_process from 'child_process';
55
import esbuild from 'esbuild';
66
import fs from 'fs';
77
import glob from 'glob';
88
import module from 'module';
9+
import path from 'path';
910
import {dirname, join, relative} from 'path';
1011
import * as sass from 'sass';
1112
import url from 'url';
@@ -48,15 +49,10 @@ async function main() {
4849
await compileProjectWithNgtsc();
4950

5051
const specEntryPointFile = await createEntryPointSpecFile();
51-
const esbuildLinkerPlugin = await createEsbuildAngularOptimizePlugin({
52-
enableLinker: {
53-
filterPaths: /fesm2022/,
54-
linkerOptions: {
55-
linkerJitMode: true,
56-
},
57-
},
58-
});
59-
const esbuildResolvePlugin = await createResolveEsbuildPlugin();
52+
53+
// Copy tsconfig so that ESBuild can leverage its path mappings.
54+
const esbuildTsconfig = path.join(legacyOutputDir, 'tsconfig-esbuild.json');
55+
await fs.promises.cp(path.join(packagesDir, 'bazel-tsconfig-build.json'), esbuildTsconfig);
6056

6157
const result = await esbuild.build({
6258
bundle: true,
@@ -66,7 +62,9 @@ async function main() {
6662
target: 'es2015',
6763
outfile: outFile,
6864
treeShaking: false,
69-
plugins: [esbuildResolvePlugin, esbuildLinkerPlugin],
65+
logLevel: 'info',
66+
tsconfig: esbuildTsconfig,
67+
plugins: [createLinkerEsbuildPlugin()],
7068
stdin: {contents: specEntryPointFile, resolveDir: projectDir},
7169
});
7270

@@ -168,48 +166,26 @@ function renderSassFile(inputFile) {
168166
});
169167
}
170168

171-
/**
172-
* Creates an ESBuild plugin which maps `@angular/<..>` module names to their
173-
* locally-built output (for the packages which are built as part of this repo).
174-
*/
175-
async function createResolveEsbuildPlugin() {
169+
/** Plugin that links node modules using the Angular compiler CLI. */
170+
function createLinkerEsbuildPlugin() {
176171
return {
177-
name: 'ng-resolve-esbuild',
172+
name: 'ng-link-esbuild',
178173
setup: build => {
179-
build.onResolve({filter: /@angular\//}, async args => {
180-
const pkgName = args.path.slice('@angular/'.length);
181-
let resolvedPath = join(legacyOutputDir, pkgName);
182-
let stats = await statGraceful(resolvedPath);
183-
184-
// If the resolved path points to a directory, resolve the contained `index.js` file
185-
if (stats && stats.isDirectory()) {
186-
resolvedPath = join(resolvedPath, 'index.js');
187-
stats = await statGraceful(resolvedPath);
188-
}
189-
// If the resolved path does not exist, check with an explicit JS extension.
190-
else if (stats === null) {
191-
resolvedPath += '.js';
192-
stats = await statGraceful(resolvedPath);
193-
}
194-
195-
return stats !== null ? {path: resolvedPath} : undefined;
174+
build.onLoad({filter: /fesm2022/}, async args => {
175+
const filePath = args.path;
176+
const content = await fs.promises.readFile(filePath, 'utf8');
177+
const {code} = await babel.transformAsync(content, {
178+
filename: filePath,
179+
compact: false,
180+
plugins: [['@angular/compiler-cli/linker/babel', {linkerJitMode: true}]],
181+
});
182+
183+
return {contents: code};
196184
});
197185
},
198186
};
199187
}
200188

201-
/**
202-
* Retrieves the `fs.Stats` results for the given path gracefully.
203-
* If the file does not exist, returns `null`.
204-
*/
205-
async function statGraceful(path) {
206-
try {
207-
return await fs.promises.stat(path);
208-
} catch {
209-
return null;
210-
}
211-
}
212-
213189
main().catch(e => {
214190
console.error(e);
215191
process.exitCode = 1;

0 commit comments

Comments
 (0)