@@ -9,6 +9,7 @@ export const plugin = (options: { inputs: BundlerInput[] }): Plugin => {
99 name : 'instrument-bundler-plugin' ,
1010 setup ( build ) {
1111 const namespaces = { bundle : 'bundle' } ;
12+ const legacyScripts : string [ ] = [ ] ;
1213 build . onResolve ( { filter : / .* / } , ( args ) => {
1314 // css @import statement
1415 if ( args . kind === 'import-rule' ) {
@@ -29,6 +30,35 @@ export const plugin = (options: { inputs: BundlerInput[] }): Plugin => {
2930 path : args . path
3031 } ;
3132 } ) ;
33+ build . onLoad ( { filter : / .+ \? r a w $ / , namespace : namespaces . bundle } , ( args ) => {
34+ const input = resolveInput ( / ( .+ ) \? r a w $ / . exec ( args . path ) ! [ 1 ] ! , options . inputs ) ;
35+ if ( ! input ) {
36+ return {
37+ errors : [
38+ {
39+ location : { file : args . path } ,
40+ text : `Failed to resolve '${ args . path } ' from input filenames: ${ options . inputs . map ( ( file ) => `'${ file . name } '` ) . join ( ', ' ) } `
41+ }
42+ ]
43+ } ;
44+ }
45+ return { contents : input ?. content , loader : 'text' } ;
46+ } ) ;
47+ build . onLoad ( { filter : / .+ \? l e g a c y $ / , namespace : namespaces . bundle } , ( args ) => {
48+ const input = resolveInput ( / ( .+ ) \? l e g a c y $ / . exec ( args . path ) ! [ 1 ] ! , options . inputs ) ;
49+ if ( ! input ) {
50+ return {
51+ errors : [
52+ {
53+ location : { file : args . path } ,
54+ text : `Failed to resolve '${ args . path } ' from input filenames: ${ options . inputs . map ( ( file ) => `'${ file . name } '` ) . join ( ', ' ) } `
55+ }
56+ ]
57+ } ;
58+ }
59+ legacyScripts . push ( input . content as string ) ;
60+ return { contents : input . content , loader : 'empty' } ;
61+ } ) ;
3262 build . onLoad ( { filter : / ^ \/ r u n t i m e \/ v 1 \/ .* .c s s $ / , namespace : namespaces . bundle } , ( args ) => {
3363 return { contents : `@import "${ args . path } ";` , loader : 'css' } ;
3464 } ) ;
@@ -53,6 +83,9 @@ export const plugin = (options: { inputs: BundlerInput[] }): Plugin => {
5383 }
5484 return { contents, loader } ;
5585 } ) ;
86+ build . onEnd ( ( result ) => {
87+ result . legacyScripts = legacyScripts ;
88+ } ) ;
5689 }
5790 } ;
5891} ;
0 commit comments