@@ -36,11 +36,15 @@ export const mirror = (): Plugin | undefined => {
3636 if (
3737 chunk . type == 'chunk' &&
3838 chunk . fileName . endsWith ( `.js` ) &&
39- chunk . code . includes ( '/assets/' )
39+ ( chunk . code . includes ( '/assets/' ) || chunk . code . match ( / \b i m p o r t \s \( / ) )
4040 ) {
4141 const ast = this . parse ( chunk . code ) ;
42+ const importNodes : ImportExpression [ ] = [ ] ;
4243 const literalNodes : Literal [ ] = [ ] ;
4344 simple ( ast , {
45+ ImportExpression ( node ) {
46+ importNodes . push ( node ) ;
47+ } ,
4448 Literal ( node ) {
4549 if (
4650 typeof node . value === 'string' &&
@@ -50,17 +54,27 @@ export const mirror = (): Plugin | undefined => {
5054 }
5155 } ,
5256 } ) ;
53- if ( literalNodes . length ) {
54- const ms = new MagicString ( chunk . code ) ;
55- literalNodes . forEach ( ( n ) => {
56- ms . update (
57- n . start ,
58- n . end ,
59- JSON . stringify ( mirrorBaseUrl + String ( n . value ) ) ,
60- ) ;
61- } ) ;
62- chunk . code = ms . toString ( ) ;
63- }
57+ const ms = new MagicString ( chunk . code ) ;
58+ importNodes . forEach ( ( node ) => {
59+ const start = node . source . start ;
60+ const end = node . source . end ;
61+ const code = chunk . code . slice ( start , end ) ;
62+ ms . overwrite (
63+ start ,
64+ end ,
65+ `((u)=>{if(u.startsWith('/')){return${ JSON . stringify (
66+ mirrorBaseUrl ,
67+ ) } +u}return u})(${ code } )`,
68+ ) ;
69+ } ) ;
70+ literalNodes . forEach ( ( n ) => {
71+ ms . update (
72+ n . start ,
73+ n . end ,
74+ JSON . stringify ( mirrorBaseUrl + String ( n . value ) ) ,
75+ ) ;
76+ } ) ;
77+ chunk . code = ms . toString ( ) ;
6478 }
6579 } ) ;
6680 } ,
0 commit comments