@@ -15,6 +15,7 @@ import { assertIsError } from '../../utils/error';
1515import { LoadResultCache , createCachedLoad } from './load-result-cache' ;
1616import type { NormalizedBrowserOptions } from './options' ;
1717import { createSourcemapIngorelistPlugin } from './sourcemap-ignorelist-plugin' ;
18+ import { createVirtualModulePlugin } from './virtual-module-plugin' ;
1819
1920/**
2021 * Create an esbuild 'build' options object for all global scripts defined in the user provied
@@ -71,84 +72,62 @@ export function createGlobalScriptsBundleOptions(
7172 preserveSymlinks,
7273 plugins : [
7374 createSourcemapIngorelistPlugin ( ) ,
74- {
75- name : 'angular-global-scripts' ,
76- setup ( build ) {
77- build . onResolve ( { filter : / ^ a n g u l a r : s c r i p t \/ g l o b a l : / } , ( args ) => {
78- if ( args . kind !== 'entry-point' ) {
79- return null ;
80- }
81-
82- return {
83- // Add the `js` extension here so that esbuild generates an output file with the extension
84- path : args . path . slice ( namespace . length + 1 ) + '.js' ,
85- namespace,
86- } ;
87- } ) ;
88- // All references within a global script should be considered external. This maintains the runtime
89- // behavior of the script as if it were added directly to a script element for referenced imports.
90- build . onResolve ( { filter : / ./ , namespace } , ( { path } ) => {
91- return {
92- path,
93- external : true ,
94- } ;
95- } ) ;
96- build . onLoad (
97- { filter : / ./ , namespace } ,
98- createCachedLoad ( loadCache , async ( args ) => {
99- const files = globalScripts . find (
100- ( { name } ) => name === args . path . slice ( 0 , - 3 ) ,
101- ) ?. files ;
102- assert ( files , `Invalid operation: global scripts name not found [${ args . path } ]` ) ;
75+ createVirtualModulePlugin ( {
76+ namespace,
77+ external : true ,
78+ // Add the `js` extension here so that esbuild generates an output file with the extension
79+ transformPath : ( path ) => path . slice ( namespace . length + 1 ) + '.js' ,
80+ loadContent : ( args , build ) =>
81+ createCachedLoad ( loadCache , async ( args ) => {
82+ const files = globalScripts . find ( ( { name } ) => name === args . path . slice ( 0 , - 3 ) ) ?. files ;
83+ assert ( files , `Invalid operation: global scripts name not found [${ args . path } ]` ) ;
10384
104- // Global scripts are concatenated using magic-string instead of bundled via esbuild.
105- const bundleContent = new Bundle ( ) ;
106- const watchFiles = [ ] ;
107- for ( const filename of files ) {
108- let fileContent ;
109- try {
110- // Attempt to read as a relative path from the workspace root
111- fileContent = await readFile ( path . join ( workspaceRoot , filename ) , 'utf-8' ) ;
112- watchFiles . push ( filename ) ;
113- } catch ( e ) {
114- assertIsError ( e ) ;
115- if ( e . code !== 'ENOENT' ) {
116- throw e ;
117- }
118-
119- // If not found attempt to resolve as a module specifier
120- const resolveResult = await build . resolve ( filename , {
121- kind : 'entry-point' ,
122- resolveDir : workspaceRoot ,
123- } ) ;
85+ // Global scripts are concatenated using magic-string instead of bundled via esbuild.
86+ const bundleContent = new Bundle ( ) ;
87+ const watchFiles = [ ] ;
88+ for ( const filename of files ) {
89+ let fileContent ;
90+ try {
91+ // Attempt to read as a relative path from the workspace root
92+ fileContent = await readFile ( path . join ( workspaceRoot , filename ) , 'utf-8' ) ;
93+ watchFiles . push ( filename ) ;
94+ } catch ( e ) {
95+ assertIsError ( e ) ;
96+ if ( e . code !== 'ENOENT' ) {
97+ throw e ;
98+ }
12499
125- if ( resolveResult . errors . length ) {
126- // Remove resolution failure notes about marking as external since it doesn't apply
127- // to global scripts.
128- resolveResult . errors . forEach ( ( error ) => ( error . notes = [ ] ) ) ;
100+ // If not found attempt to resolve as a module specifier
101+ const resolveResult = await build . resolve ( filename , {
102+ kind : 'entry-point' ,
103+ resolveDir : workspaceRoot ,
104+ } ) ;
129105
130- return {
131- errors : resolveResult . errors ,
132- warnings : resolveResult . warnings ,
133- } ;
134- }
106+ if ( resolveResult . errors . length ) {
107+ // Remove resolution failure notes about marking as external since it doesn't apply
108+ // to global scripts.
109+ resolveResult . errors . forEach ( ( error ) => ( error . notes = [ ] ) ) ;
135110
136- watchFiles . push ( path . relative ( resolveResult . path , workspaceRoot ) ) ;
137- fileContent = await readFile ( resolveResult . path , 'utf-8' ) ;
111+ return {
112+ errors : resolveResult . errors ,
113+ warnings : resolveResult . warnings ,
114+ } ;
138115 }
139116
140- bundleContent . addSource ( new MagicString ( fileContent , { filename } ) ) ;
117+ watchFiles . push ( path . relative ( resolveResult . path , workspaceRoot ) ) ;
118+ fileContent = await readFile ( resolveResult . path , 'utf-8' ) ;
141119 }
142120
143- return {
144- contents : bundleContent . toString ( ) ,
145- loader : 'js' ,
146- watchFiles,
147- } ;
148- } ) ,
149- ) ;
150- } ,
151- } ,
121+ bundleContent . addSource ( new MagicString ( fileContent , { filename } ) ) ;
122+ }
123+
124+ return {
125+ contents : bundleContent . toString ( ) ,
126+ loader : 'js' ,
127+ watchFiles,
128+ } ;
129+ } ) . call ( build , args ) ,
130+ } ) ,
152131 ] ,
153132 } ;
154133}
0 commit comments