@@ -16,6 +16,7 @@ import type * as NodeType from './NodeType';
16
16
import { NodeBase } from './shared/Node' ;
17
17
18
18
const FILE_PREFIX = 'ROLLUP_FILE_URL_' ;
19
+ const FILE_OBJ_PREFIX = 'ROLLUP_FILE_URL_OBJ_' ;
19
20
const IMPORT = 'import' ;
20
21
21
22
export default class MetaProperty extends NodeBase {
@@ -32,8 +33,12 @@ export default class MetaProperty extends NodeBase {
32
33
meta : { name } ,
33
34
metaProperty
34
35
} = this ;
35
- if ( name === IMPORT && metaProperty ?. startsWith ( FILE_PREFIX ) ) {
36
- return outputPluginDriver . getFileName ( metaProperty . slice ( FILE_PREFIX . length ) ) ;
36
+ if ( name === IMPORT ) {
37
+ if ( metaProperty ?. startsWith ( FILE_OBJ_PREFIX ) ) {
38
+ return outputPluginDriver . getFileName ( metaProperty . slice ( FILE_OBJ_PREFIX . length ) ) ;
39
+ } else if ( metaProperty ?. startsWith ( FILE_PREFIX ) ) {
40
+ return outputPluginDriver . getFileName ( metaProperty . slice ( FILE_PREFIX . length ) ) ;
41
+ }
37
42
}
38
43
return null ;
39
44
}
@@ -59,7 +64,9 @@ export default class MetaProperty extends NodeBase {
59
64
parent instanceof MemberExpression && typeof parent . propertyKey === 'string'
60
65
? parent . propertyKey
61
66
: null ) ;
62
- if ( metaProperty ?. startsWith ( FILE_PREFIX ) ) {
67
+ if ( metaProperty ?. startsWith ( FILE_OBJ_PREFIX ) ) {
68
+ this . referenceId = metaProperty . slice ( FILE_OBJ_PREFIX . length ) ;
69
+ } else if ( metaProperty ?. startsWith ( FILE_PREFIX ) ) {
63
70
this . referenceId = metaProperty . slice ( FILE_PREFIX . length ) ;
64
71
}
65
72
}
@@ -87,10 +94,11 @@ export default class MetaProperty extends NodeBase {
87
94
if ( referenceId ) {
88
95
const fileName = pluginDriver . getFileName ( referenceId ) ;
89
96
const relativePath = normalize ( relative ( dirname ( chunkId ) , fileName ) ) ;
97
+ const isUrlObject = ! ! metaProperty ?. startsWith ( FILE_OBJ_PREFIX ) ;
90
98
const replacement =
91
99
pluginDriver . hookFirstSync ( 'resolveFileUrl' , [
92
100
{ chunkId, fileName, format, moduleId, referenceId, relativePath }
93
- ] ) || relativeUrlMechanisms [ format ] ( relativePath ) ;
101
+ ] ) || relativeUrlMechanisms [ format ] ( relativePath , isUrlObject ) ;
94
102
95
103
code . overwrite (
96
104
( parent as MemberExpression ) . start ,
@@ -126,7 +134,9 @@ export default class MetaProperty extends NodeBase {
126
134
) : void {
127
135
this . preliminaryChunkId = preliminaryChunkId ;
128
136
const accessedGlobals = (
129
- this . metaProperty ?. startsWith ( FILE_PREFIX ) ? accessedFileUrlGlobals : accessedMetaUrlGlobals
137
+ this . metaProperty ?. startsWith ( FILE_PREFIX ) || this . metaProperty ?. startsWith ( FILE_OBJ_PREFIX )
138
+ ? accessedFileUrlGlobals
139
+ : accessedMetaUrlGlobals
130
140
) [ format ] ;
131
141
if ( accessedGlobals . length > 0 ) {
132
142
this . scope . addAccessedGlobals ( accessedGlobals , accessedGlobalsByScope ) ;
@@ -154,13 +164,15 @@ const accessedFileUrlGlobals = {
154
164
umd : [ 'document' , 'require' , 'URL' ]
155
165
} ;
156
166
157
- const getResolveUrl = ( path : string , URL = 'URL' ) => `new ${ URL } (${ path } ).href` ;
167
+ const getResolveUrl = ( path : string , asObject : boolean , URL = 'URL' ) =>
168
+ `new ${ URL } (${ path } )${ asObject ? '' : '.href' } ` ;
158
169
159
- const getRelativeUrlFromDocument = ( relativePath : string , umd = false ) =>
170
+ const getRelativeUrlFromDocument = ( relativePath : string , asObject : boolean , umd = false ) =>
160
171
getResolveUrl (
161
172
`'${ escapeId ( relativePath ) } ', ${
162
173
umd ? `typeof document === 'undefined' ? location.href : ` : ''
163
- } document.currentScript && document.currentScript.tagName.toUpperCase() === 'SCRIPT' && document.currentScript.src || document.baseURI`
174
+ } document.currentScript && document.currentScript.tagName.toUpperCase() === 'SCRIPT' && document.currentScript.src || document.baseURI`,
175
+ asObject
164
176
) ;
165
177
166
178
const getGenericImportMetaMechanism =
@@ -174,10 +186,11 @@ const getGenericImportMetaMechanism =
174
186
: 'undefined' ;
175
187
} ;
176
188
177
- const getFileUrlFromFullPath = ( path : string ) => `require('u' + 'rl').pathToFileURL(${ path } ).href` ;
189
+ const getFileUrlFromFullPath = ( path : string , asObject : boolean ) =>
190
+ `require('u' + 'rl').pathToFileURL(${ path } )${ asObject ? '' : '.href' } ` ;
178
191
179
- const getFileUrlFromRelativePath = ( path : string ) =>
180
- getFileUrlFromFullPath ( `__dirname + '/${ escapeId ( path ) } '` ) ;
192
+ const getFileUrlFromRelativePath = ( path : string , asObject : boolean ) =>
193
+ getFileUrlFromFullPath ( `__dirname + '/${ escapeId ( path ) } '` , asObject ) ;
181
194
182
195
const getUrlFromDocument = ( chunkId : string , umd = false ) =>
183
196
`${
@@ -186,42 +199,39 @@ const getUrlFromDocument = (chunkId: string, umd = false) =>
186
199
chunkId
187
200
) } ', document.baseURI).href)`;
188
201
189
- const relativeUrlMechanisms : Record < InternalModuleFormat , ( relativePath : string ) => string > = {
190
- amd : relativePath => {
202
+ const relativeUrlMechanisms : Record <
203
+ InternalModuleFormat ,
204
+ ( relativePath : string , asObject : boolean ) => string
205
+ > = {
206
+ amd : ( relativePath , asObject : boolean ) => {
191
207
if ( relativePath [ 0 ] !== '.' ) relativePath = './' + relativePath ;
192
- return getResolveUrl ( `require.toUrl('${ escapeId ( relativePath ) } '), document.baseURI` ) ;
208
+ return getResolveUrl ( `require.toUrl('${ escapeId ( relativePath ) } '), document.baseURI` , asObject ) ;
193
209
} ,
194
- cjs : relativePath =>
195
- `(typeof document === 'undefined' ? ${ getFileUrlFromRelativePath (
196
- relativePath
197
- ) } : ${ getRelativeUrlFromDocument ( relativePath ) } )`,
198
- es : relativePath => getResolveUrl ( `'${ escapeId ( relativePath ) } ', import.meta.url` ) ,
199
- iife : relativePath => getRelativeUrlFromDocument ( relativePath ) ,
200
- system : relativePath => getResolveUrl ( `'${ escapeId ( relativePath ) } ', module.meta.url` ) ,
201
- umd : relativePath =>
202
- `(typeof document === 'undefined' && typeof location === 'undefined' ? ${ getFileUrlFromRelativePath (
203
- relativePath
204
- ) } : ${ getRelativeUrlFromDocument ( relativePath , true ) } )`
210
+ cjs : ( relativePath , asObject : boolean ) =>
211
+ `(typeof document === 'undefined' ? ${ getFileUrlFromRelativePath ( relativePath , asObject ) } : ${ getRelativeUrlFromDocument ( relativePath , asObject ) } )` ,
212
+ es : ( relativePath , asObject : boolean ) =>
213
+ getResolveUrl ( `'${ escapeId ( relativePath ) } ', import.meta.url` , asObject ) ,
214
+ iife : ( relativePath , asObject : boolean ) => getRelativeUrlFromDocument ( relativePath , asObject ) ,
215
+ system : ( relativePath , asObject : boolean ) =>
216
+ getResolveUrl ( `'${ escapeId ( relativePath ) } ', module.meta.url` , asObject ) ,
217
+ umd : ( relativePath , asObject : boolean ) =>
218
+ `(typeof document === 'undefined' && typeof location === 'undefined' ? ${ getFileUrlFromRelativePath ( relativePath , asObject ) } : ${ getRelativeUrlFromDocument ( relativePath , asObject , true ) } )`
205
219
} ;
206
220
207
221
const importMetaMechanisms : Record <
208
222
string ,
209
223
( property : string | null , options : { chunkId : string ; snippets : GenerateCodeSnippets } ) => string
210
224
> = {
211
- amd : getGenericImportMetaMechanism ( ( ) => getResolveUrl ( `module.uri, document.baseURI` ) ) ,
225
+ amd : getGenericImportMetaMechanism ( ( ) => getResolveUrl ( `module.uri, document.baseURI` , false ) ) ,
212
226
cjs : getGenericImportMetaMechanism (
213
227
chunkId =>
214
- `(typeof document === 'undefined' ? ${ getFileUrlFromFullPath (
215
- '__filename'
216
- ) } : ${ getUrlFromDocument ( chunkId ) } )`
228
+ `(typeof document === 'undefined' ? ${ getFileUrlFromFullPath ( '__filename' , false ) } : ${ getUrlFromDocument ( chunkId ) } )`
217
229
) ,
218
230
iife : getGenericImportMetaMechanism ( chunkId => getUrlFromDocument ( chunkId ) ) ,
219
231
system : ( property , { snippets : { getPropertyAccess } } ) =>
220
232
property === null ? `module.meta` : `module.meta${ getPropertyAccess ( property ) } ` ,
221
233
umd : getGenericImportMetaMechanism (
222
234
chunkId =>
223
- `(typeof document === 'undefined' && typeof location === 'undefined' ? ${ getFileUrlFromFullPath (
224
- '__filename'
225
- ) } : ${ getUrlFromDocument ( chunkId , true ) } )`
235
+ `(typeof document === 'undefined' && typeof location === 'undefined' ? ${ getFileUrlFromFullPath ( '__filename' , false ) } : ${ getUrlFromDocument ( chunkId , true ) } )`
226
236
)
227
237
} ;
0 commit comments