@@ -78,13 +78,15 @@ function importMetaAssets({ include, exclude, warnOnError, transform } = {}) {
7878 return null ;
7979 }
8080
81+ let newCode = code ;
82+
8183 // Part 1: resolve dynamic template literal expressions
82- const parsed = this . parse ( code ) ;
84+ const ast1 = this . parse ( newCode ) ;
85+ let ms1 ;
8386
8487 let dynamicURLIndex = - 1 ;
85- let ms ;
8688
87- await asyncWalk ( parsed , {
89+ await asyncWalk ( ast1 , {
8890 enter : async node => {
8991 const importMetaUrlType = getImportMetaUrlType ( node ) ;
9092
@@ -95,7 +97,10 @@ function importMetaAssets({ include, exclude, warnOnError, transform } = {}) {
9597
9698 try {
9799 // see if this is a Template Literal with expressions inside, and generate a glob expression
98- const glob = dynamicURLToGlob ( node . arguments [ 0 ] , code . substring ( node . start , node . end ) ) ;
100+ const glob = dynamicURLToGlob (
101+ node . arguments [ 0 ] ,
102+ newCode . substring ( node . start , node . end ) ,
103+ ) ;
99104
100105 if ( ! glob ) {
101106 // this was not a variable dynamic url
@@ -110,10 +115,10 @@ function importMetaAssets({ include, exclude, warnOnError, transform } = {}) {
110115 . map ( r => ( r . startsWith ( './' ) || r . startsWith ( '../' ) ? r : `./${ r } ` ) ) ;
111116
112117 // create magic string if it wasn't created already
113- ms = ms || new MagicString ( code ) ;
118+ ms1 = ms1 || new MagicString ( newCode ) ;
114119 // unpack variable dynamic url into a function with url statements per file, rollup
115120 // will turn these into chunks automatically
116- ms . prepend (
121+ ms1 . prepend (
117122 `function __variableDynamicURLRuntime${ dynamicURLIndex } __(path) {
118123 switch (path) {
119124${ paths . map ( p => ` case '${ p } ': return new URL('${ p } ', import.meta.url);` ) . join ( '\n' ) }
@@ -126,7 +131,7 @@ ${` default: return new Promise(function(resolve, reject) {
126131 ) ;
127132 // call the runtime function instead of doing a dynamic url, the url specifier will
128133 // be evaluated at runtime and the correct url will be returned by the injected function
129- ms . overwrite (
134+ ms1 . overwrite (
130135 node . start ,
131136 node . start + 7 ,
132137 `__variableDynamicURLRuntime${ dynamicURLIndex } __` ,
@@ -141,17 +146,15 @@ ${` default: return new Promise(function(resolve, reject) {
141146 } ,
142147 } ) ;
143148
144- let newCode = code ;
145- if ( ms && dynamicURLIndex !== - 1 ) {
146- newCode = ms . toString ( ) ;
149+ if ( ms1 ) {
150+ newCode = ms1 . toString ( ) ;
147151 }
148152
149153 // Part 2: emit asset files
150- const ast = this . parse ( newCode ) ;
151- const magicString = new MagicString ( newCode ) ;
152- let modifiedCode = false ;
154+ const ast2 = this . parse ( newCode ) ;
155+ let ms2 ;
153156
154- await asyncWalk ( ast , {
157+ await asyncWalk ( ast2 , {
155158 enter : async node => {
156159 const importMetaUrlType = getImportMetaUrlType ( node ) ;
157160 if ( ! importMetaUrlType ) {
@@ -179,12 +182,12 @@ ${` default: return new Promise(function(resolve, reject) {
179182 originalFileName : absoluteAssetPath ,
180183 source : transformedAssetContents ,
181184 } ) ;
182- magicString . overwrite (
185+ ms2 = ms2 || new MagicString ( newCode ) ;
186+ ms2 . overwrite (
183187 node . arguments [ 0 ] . start ,
184188 node . arguments [ 1 ] . end ,
185189 `import.meta.ROLLUP_FILE_URL_${ ref } ` ,
186190 ) ;
187- modifiedCode = true ;
188191 } catch ( error ) {
189192 // Do not process directories, just skip
190193 if ( error . code !== 'EISDIR' ) {
@@ -199,9 +202,29 @@ ${` default: return new Promise(function(resolve, reject) {
199202 } ,
200203 } ) ;
201204
205+ if ( ms2 ) {
206+ newCode = ms2 . toString ( ) ;
207+ }
208+
209+ if ( ! ms1 && ! ms2 ) {
210+ return null ;
211+ }
212+
213+ let map ;
214+ if ( ms1 && ms2 ) {
215+ const map1 = ms1 . generateMap ( { hires : true , source : id } ) ;
216+ const map2 = ms2 . generateMap ( { hires : true , source : id } ) ;
217+ const remapping = ( await import ( '@jridgewell/remapping' ) ) . default ;
218+ map = remapping ( [ map2 , map1 ] , ( ) => null ) ;
219+ } else if ( ms2 ) {
220+ map = ms2 . generateMap ( { hires : true } ) ;
221+ } else if ( ms1 ) {
222+ map = ms1 . generateMap ( { hires : true } ) ;
223+ }
224+
202225 return {
203- code : magicString . toString ( ) ,
204- map : modifiedCode ? magicString . generateMap ( { hires : true , includeContent : true } ) : null ,
226+ code : newCode ,
227+ map,
205228 } ;
206229 } ,
207230 } ;
0 commit comments