@@ -204,50 +204,56 @@ function shimDepsPlugin(deps: Record<string, ShimOptions[]>): Plugin {
204204
205205 return {
206206 name : 'shim-deps' ,
207- transform ( code , id ) {
208- for ( const file in deps ) {
209- if ( id . replace ( / \\ / g, '/' ) . endsWith ( file ) ) {
210- for ( const { src, replacement, pattern } of deps [ file ] ) {
211- const magicString = new MagicString ( code )
212-
213- if ( src ) {
214- const pos = code . indexOf ( src )
215- if ( pos < 0 ) {
216- this . error (
217- `Could not find expected src "${ src } " in file "${ file } "` ,
218- )
219- }
220- transformed [ file ] = true
221- magicString . overwrite ( pos , pos + src . length , replacement )
207+ transform : {
208+ filter : {
209+ id : new RegExp ( `(?:${ Object . keys ( deps ) . join ( '|' ) } )$` ) ,
210+ } ,
211+ handler ( code , id ) {
212+ const file = Object . keys ( deps ) . find ( ( file ) =>
213+ id . replace ( / \\ / g, '/' ) . endsWith ( file ) ,
214+ )
215+ if ( ! file ) return
216+
217+ for ( const { src, replacement, pattern } of deps [ file ] ) {
218+ const magicString = new MagicString ( code )
219+
220+ if ( src ) {
221+ const pos = code . indexOf ( src )
222+ if ( pos < 0 ) {
223+ this . error (
224+ `Could not find expected src "${ src } " in file "${ file } "` ,
225+ )
222226 }
227+ transformed [ file ] = true
228+ magicString . overwrite ( pos , pos + src . length , replacement )
229+ }
223230
224- if ( pattern ) {
225- let match
226- while ( ( match = pattern . exec ( code ) ) ) {
227- transformed [ file ] = true
228- const start = match . index
229- const end = start + match [ 0 ] . length
230- let _replacement = replacement
231- for ( let i = 1 ; i <= match . length ; i ++ ) {
232- _replacement = _replacement . replace ( `$${ i } ` , match [ i ] || '' )
233- }
234- magicString . overwrite ( start , end , _replacement )
235- }
236- if ( ! transformed [ file ] ) {
237- this . error (
238- `Could not find expected pattern "${ pattern } " in file "${ file } "` ,
239- )
231+ if ( pattern ) {
232+ let match
233+ while ( ( match = pattern . exec ( code ) ) ) {
234+ transformed [ file ] = true
235+ const start = match . index
236+ const end = start + match [ 0 ] . length
237+ let _replacement = replacement
238+ for ( let i = 1 ; i <= match . length ; i ++ ) {
239+ _replacement = _replacement . replace ( `$${ i } ` , match [ i ] || '' )
240240 }
241+ magicString . overwrite ( start , end , _replacement )
242+ }
243+ if ( ! transformed [ file ] ) {
244+ this . error (
245+ `Could not find expected pattern "${ pattern } " in file "${ file } "` ,
246+ )
241247 }
242-
243- code = magicString . toString ( )
244248 }
245249
246- console . log ( `shimmed: ${ file } ` )
247-
248- return code
250+ code = magicString . toString ( )
249251 }
250- }
252+
253+ console . log ( `shimmed: ${ file } ` )
254+
255+ return code
256+ } ,
251257 } ,
252258 buildEnd ( err ) {
253259 if ( ! err ) {
@@ -273,8 +279,10 @@ function buildTimeImportMetaUrlPlugin(): Plugin {
273279 return {
274280 name : 'import-meta-current-dirname' ,
275281 transform : {
282+ filter : {
283+ code : 'import.meta.url' ,
284+ } ,
276285 async handler ( code , id ) {
277- if ( ! code . includes ( 'import.meta.url' ) ) return
278286 const relativeId = path . relative ( __dirname , id ) . replaceAll ( '\\' , '/' )
279287 // only replace import.meta.url in src/
280288 if ( ! relativeId . startsWith ( 'src/' ) ) return
0 commit comments