@@ -12,7 +12,8 @@ import assert from 'node:assert';
12
12
import { readFile } from 'node:fs/promises' ;
13
13
import path from 'node:path' ;
14
14
import { assertIsError } from '../../utils/error' ;
15
- import { NormalizedBrowserOptions } from './options' ;
15
+ import { LoadResultCache , createCachedLoad } from './load-result-cache' ;
16
+ import type { NormalizedBrowserOptions } from './options' ;
16
17
import { createSourcemapIngorelistPlugin } from './sourcemap-ignorelist-plugin' ;
17
18
18
19
/**
@@ -24,6 +25,7 @@ import { createSourcemapIngorelistPlugin } from './sourcemap-ignorelist-plugin';
24
25
export function createGlobalScriptsBundleOptions (
25
26
options : NormalizedBrowserOptions ,
26
27
initial : boolean ,
28
+ loadCache ?: LoadResultCache ,
27
29
) : BuildOptions | undefined {
28
30
const {
29
31
globalScripts,
@@ -91,51 +93,60 @@ export function createGlobalScriptsBundleOptions(
91
93
external : true ,
92
94
} ;
93
95
} ) ;
94
- build . onLoad ( { filter : / ./ , namespace } , async ( args ) => {
95
- const files = globalScripts . find ( ( { name } ) => name === args . path . slice ( 0 , - 3 ) ) ?. files ;
96
- assert ( files , `Invalid operation: global scripts name not found [${ args . path } ]` ) ;
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 } ]` ) ;
97
103
98
- // Global scripts are concatenated using magic-string instead of bundled via esbuild.
99
- const bundleContent = new Bundle ( ) ;
100
- for ( const filename of files ) {
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
- }
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
+ } ) ;
110
124
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
- } ) ;
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 = [ ] ) ) ;
116
129
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 = [ ] ) ) ;
130
+ return {
131
+ errors : resolveResult . errors ,
132
+ warnings : resolveResult . warnings ,
133
+ } ;
134
+ }
121
135
122
- return {
123
- errors : resolveResult . errors ,
124
- warnings : resolveResult . warnings ,
125
- } ;
136
+ watchFiles . push ( path . relative ( resolveResult . path , workspaceRoot ) ) ;
137
+ fileContent = await readFile ( resolveResult . path , 'utf-8' ) ;
126
138
}
127
139
128
- fileContent = await readFile ( resolveResult . path , 'utf-8' ) ;
140
+ bundleContent . addSource ( new MagicString ( fileContent , { filename } ) ) ;
129
141
}
130
142
131
- bundleContent . addSource ( new MagicString ( fileContent , { filename } ) ) ;
132
- }
133
-
134
- return {
135
- contents : bundleContent . toString ( ) ,
136
- loader : 'js' ,
137
- } ;
138
- } ) ;
143
+ return {
144
+ contents : bundleContent . toString ( ) ,
145
+ loader : 'js' ,
146
+ watchFiles,
147
+ } ;
148
+ } ) ,
149
+ ) ;
139
150
} ,
140
151
} ,
141
152
] ,
0 commit comments