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 ' ;
44import child_process from 'child_process' ;
55import esbuild from 'esbuild' ;
66import fs from 'fs' ;
77import glob from 'glob' ;
88import module from 'module' ;
9+ import path from 'path' ;
910import { dirname , join , relative } from 'path' ;
1011import * as sass from 'sass' ;
1112import 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 : / f e s m 2 0 2 2 / ,
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 : / @ a n g u l a r \/ / } , 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 : / f e s m 2 0 2 2 / } , 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-
213189main ( ) . catch ( e => {
214190 console . error ( e ) ;
215191 process . exitCode = 1 ;
0 commit comments