@@ -50,123 +50,121 @@ export function assetImportMetaUrlPlugin(config: ResolvedConfig): Plugin {
50
50
} ,
51
51
52
52
transform : {
53
+ filter : {
54
+ id : {
55
+ exclude : [ preloadHelperId , CLIENT_ENTRY ] ,
56
+ } ,
57
+ code : / n e w \s + U R L .+ i m p o r t \. m e t a \. u r l / ,
58
+ } ,
53
59
async handler ( code , id ) {
54
- if (
55
- id !== preloadHelperId &&
56
- id !== CLIENT_ENTRY &&
57
- code . includes ( 'new URL' ) &&
58
- code . includes ( `import.meta.url` )
59
- ) {
60
- let s : MagicString | undefined
61
- const assetImportMetaUrlRE =
62
- / \b n e w \s + U R L \s * \( \s * ( ' [ ^ ' ] + ' | " [ ^ " ] + " | ` [ ^ ` ] + ` ) \s * , \s * i m p o r t \. m e t a \. u r l \s * (?: , \s * ) ? \) / dg
63
- const cleanString = stripLiteral ( code )
64
-
65
- let match : RegExpExecArray | null
66
- while ( ( match = assetImportMetaUrlRE . exec ( cleanString ) ) ) {
67
- const [ [ startIndex , endIndex ] , [ urlStart , urlEnd ] ] = match . indices !
68
- if ( hasViteIgnoreRE . test ( code . slice ( startIndex , urlStart ) ) ) continue
60
+ let s : MagicString | undefined
61
+ const assetImportMetaUrlRE =
62
+ / \b n e w \s + U R L \s * \( \s * ( ' [ ^ ' ] + ' | " [ ^ " ] + " | ` [ ^ ` ] + ` ) \s * , \s * i m p o r t \. m e t a \. u r l \s * (?: , \s * ) ? \) / dg
63
+ const cleanString = stripLiteral ( code )
69
64
70
- const rawUrl = code . slice ( urlStart , urlEnd )
65
+ let match : RegExpExecArray | null
66
+ while ( ( match = assetImportMetaUrlRE . exec ( cleanString ) ) ) {
67
+ const [ [ startIndex , endIndex ] , [ urlStart , urlEnd ] ] = match . indices !
68
+ if ( hasViteIgnoreRE . test ( code . slice ( startIndex , urlStart ) ) ) continue
71
69
72
- if ( ! s ) s = new MagicString ( code )
70
+ const rawUrl = code . slice ( urlStart , urlEnd )
73
71
74
- // potential dynamic template string
75
- if ( rawUrl [ 0 ] === '`' && rawUrl . includes ( '${' ) ) {
76
- const queryDelimiterIndex = getQueryDelimiterIndex ( rawUrl )
77
- const hasQueryDelimiter = queryDelimiterIndex !== - 1
78
- const pureUrl = hasQueryDelimiter
79
- ? rawUrl . slice ( 0 , queryDelimiterIndex ) + '`'
80
- : rawUrl
81
- const queryString = hasQueryDelimiter
82
- ? rawUrl . slice ( queryDelimiterIndex , - 1 )
83
- : ''
84
- const ast = this . parse ( pureUrl )
85
- const templateLiteral = ( ast as any ) . body [ 0 ] . expression
86
- if ( templateLiteral . expressions . length ) {
87
- const pattern = buildGlobPattern ( templateLiteral )
88
- if ( pattern . startsWith ( '*' ) ) {
89
- // don't transform for patterns like this
90
- // because users won't intend to do that in most cases
91
- continue
92
- }
72
+ if ( ! s ) s = new MagicString ( code )
93
73
94
- const globOptions = {
95
- eager : true ,
96
- import : 'default' ,
97
- // A hack to allow 'as' & 'query' exist at the same time
98
- query : injectQuery ( queryString , 'url' ) ,
99
- }
100
- s . update (
101
- startIndex ,
102
- endIndex ,
103
- `new URL((import.meta.glob(${ JSON . stringify (
104
- pattern ,
105
- ) } , ${ JSON . stringify (
106
- globOptions ,
107
- ) } ))[${ pureUrl } ], import.meta.url)`,
108
- )
74
+ // potential dynamic template string
75
+ if ( rawUrl [ 0 ] === '`' && rawUrl . includes ( '${' ) ) {
76
+ const queryDelimiterIndex = getQueryDelimiterIndex ( rawUrl )
77
+ const hasQueryDelimiter = queryDelimiterIndex !== - 1
78
+ const pureUrl = hasQueryDelimiter
79
+ ? rawUrl . slice ( 0 , queryDelimiterIndex ) + '`'
80
+ : rawUrl
81
+ const queryString = hasQueryDelimiter
82
+ ? rawUrl . slice ( queryDelimiterIndex , - 1 )
83
+ : ''
84
+ const ast = this . parse ( pureUrl )
85
+ const templateLiteral = ( ast as any ) . body [ 0 ] . expression
86
+ if ( templateLiteral . expressions . length ) {
87
+ const pattern = buildGlobPattern ( templateLiteral )
88
+ if ( pattern . startsWith ( '*' ) ) {
89
+ // don't transform for patterns like this
90
+ // because users won't intend to do that in most cases
109
91
continue
110
92
}
111
- }
112
93
113
- const url = rawUrl . slice ( 1 , - 1 )
114
- if ( isDataUrl ( url ) ) {
94
+ const globOptions = {
95
+ eager : true ,
96
+ import : 'default' ,
97
+ // A hack to allow 'as' & 'query' exist at the same time
98
+ query : injectQuery ( queryString , 'url' ) ,
99
+ }
100
+ s . update (
101
+ startIndex ,
102
+ endIndex ,
103
+ `new URL((import.meta.glob(${ JSON . stringify (
104
+ pattern ,
105
+ ) } , ${ JSON . stringify (
106
+ globOptions ,
107
+ ) } ))[${ pureUrl } ], import.meta.url)`,
108
+ )
115
109
continue
116
110
}
117
- let file : string | undefined
118
- if ( url [ 0 ] === '.' ) {
119
- file = slash ( path . resolve ( path . dirname ( id ) , url ) )
120
- file = tryFsResolve ( file , fsResolveOptions ) ?? file
121
- } else {
122
- assetResolver ??= createBackCompatIdResolver ( config , {
123
- extensions : [ ] ,
124
- mainFields : [ ] ,
125
- tryIndex : false ,
126
- preferRelative : true ,
127
- } )
128
- file = await assetResolver ( this . environment , url , id )
129
- file ??=
130
- url [ 0 ] === '/'
131
- ? slash ( path . join ( publicDir , url ) )
132
- : slash ( path . resolve ( path . dirname ( id ) , url ) )
133
- }
111
+ }
134
112
135
- // Get final asset URL. If the file does not exist,
136
- // we fall back to the initial URL and let it resolve in runtime
137
- let builtUrl : string | undefined
138
- if ( file ) {
139
- try {
140
- if ( publicDir && isParentDirectory ( publicDir , file ) ) {
141
- const publicPath = '/' + path . posix . relative ( publicDir , file )
142
- builtUrl = await fileToUrl ( this , publicPath )
143
- } else {
144
- builtUrl = await fileToUrl ( this , file )
145
- }
146
- } catch {
147
- // do nothing, we'll log a warning after this
113
+ const url = rawUrl . slice ( 1 , - 1 )
114
+ if ( isDataUrl ( url ) ) {
115
+ continue
116
+ }
117
+ let file : string | undefined
118
+ if ( url [ 0 ] === '.' ) {
119
+ file = slash ( path . resolve ( path . dirname ( id ) , url ) )
120
+ file = tryFsResolve ( file , fsResolveOptions ) ?? file
121
+ } else {
122
+ assetResolver ??= createBackCompatIdResolver ( config , {
123
+ extensions : [ ] ,
124
+ mainFields : [ ] ,
125
+ tryIndex : false ,
126
+ preferRelative : true ,
127
+ } )
128
+ file = await assetResolver ( this . environment , url , id )
129
+ file ??=
130
+ url [ 0 ] === '/'
131
+ ? slash ( path . join ( publicDir , url ) )
132
+ : slash ( path . resolve ( path . dirname ( id ) , url ) )
133
+ }
134
+
135
+ // Get final asset URL. If the file does not exist,
136
+ // we fall back to the initial URL and let it resolve in runtime
137
+ let builtUrl : string | undefined
138
+ if ( file ) {
139
+ try {
140
+ if ( publicDir && isParentDirectory ( publicDir , file ) ) {
141
+ const publicPath = '/' + path . posix . relative ( publicDir , file )
142
+ builtUrl = await fileToUrl ( this , publicPath )
143
+ } else {
144
+ builtUrl = await fileToUrl ( this , file )
148
145
}
146
+ } catch {
147
+ // do nothing, we'll log a warning after this
149
148
}
150
- if ( ! builtUrl ) {
151
- const rawExp = code . slice ( startIndex , endIndex )
152
- config . logger . warnOnce (
153
- `\n${ rawExp } doesn't exist at build time, it will remain unchanged to be resolved at runtime. ` +
154
- `If this is intended, you can use the /* @vite-ignore */ comment to suppress this warning.` ,
155
- )
156
- builtUrl = url
157
- }
158
- s . update (
159
- startIndex ,
160
- endIndex ,
161
- // NOTE: add `'' +` to opt-out rolldown's transform: https://github.com/rolldown/rolldown/issues/2745
162
- `new URL(${ JSON . stringify ( builtUrl ) } , '' + import.meta.url)` ,
163
- )
164
149
}
165
- if ( s ) {
166
- return transformStableResult ( s , id , config )
150
+ if ( ! builtUrl ) {
151
+ const rawExp = code . slice ( startIndex , endIndex )
152
+ config . logger . warnOnce (
153
+ `\n${ rawExp } doesn't exist at build time, it will remain unchanged to be resolved at runtime. ` +
154
+ `If this is intended, you can use the /* @vite-ignore */ comment to suppress this warning.` ,
155
+ )
156
+ builtUrl = url
167
157
}
158
+ s . update (
159
+ startIndex ,
160
+ endIndex ,
161
+ // NOTE: add `'' +` to opt-out rolldown's transform: https://github.com/rolldown/rolldown/issues/2745
162
+ `new URL(${ JSON . stringify ( builtUrl ) } , '' + import.meta.url)` ,
163
+ )
164
+ }
165
+ if ( s ) {
166
+ return transformStableResult ( s , id , config )
168
167
}
169
- return null
170
168
} ,
171
169
} ,
172
170
}
0 commit comments