1
1
#!/usr/bin/env node
2
2
3
- import { createEsbuildAngularOptimizePlugin } from '@angular/build-tooling/shared-scripts/angular-optimization/esbuild-plugin.mjs ' ;
3
+ import babel from '@babel/core ' ;
4
4
import child_process from 'child_process' ;
5
5
import esbuild from 'esbuild' ;
6
6
import fs from 'fs' ;
7
7
import glob from 'glob' ;
8
8
import module from 'module' ;
9
+ import path from 'path' ;
9
10
import { dirname , join , relative } from 'path' ;
10
11
import * as sass from 'sass' ;
11
12
import url from 'url' ;
@@ -48,15 +49,10 @@ async function main() {
48
49
await compileProjectWithNgtsc ( ) ;
49
50
50
51
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 ) ;
60
56
61
57
const result = await esbuild . build ( {
62
58
bundle : true ,
@@ -66,7 +62,9 @@ async function main() {
66
62
target : 'es2015' ,
67
63
outfile : outFile ,
68
64
treeShaking : false ,
69
- plugins : [ esbuildResolvePlugin , esbuildLinkerPlugin ] ,
65
+ logLevel : 'info' ,
66
+ tsconfig : esbuildTsconfig ,
67
+ plugins : [ createLinkerEsbuildPlugin ( ) ] ,
70
68
stdin : { contents : specEntryPointFile , resolveDir : projectDir } ,
71
69
} ) ;
72
70
@@ -168,48 +166,26 @@ function renderSassFile(inputFile) {
168
166
} ) ;
169
167
}
170
168
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 ( ) {
176
171
return {
177
- name : 'ng-resolve -esbuild' ,
172
+ name : 'ng-link -esbuild' ,
178
173
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 } ;
196
184
} ) ;
197
185
} ,
198
186
} ;
199
187
}
200
188
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
-
213
189
main ( ) . catch ( e => {
214
190
console . error ( e ) ;
215
191
process . exitCode = 1 ;
0 commit comments