@@ -16,6 +16,7 @@ import type * as NodeType from './NodeType';
1616import { NodeBase } from './shared/Node' ;
1717
1818const FILE_PREFIX = 'ROLLUP_FILE_URL_' ;
19+ const FILE_OBJ_PREFIX = 'ROLLUP_FILE_URL_OBJ_' ;
1920const IMPORT = 'import' ;
2021
2122export default class MetaProperty extends NodeBase {
@@ -32,8 +33,12 @@ export default class MetaProperty extends NodeBase {
3233 meta : { name } ,
3334 metaProperty
3435 } = 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+ }
3742 }
3843 return null ;
3944 }
@@ -59,7 +64,9 @@ export default class MetaProperty extends NodeBase {
5964 parent instanceof MemberExpression && typeof parent . propertyKey === 'string'
6065 ? parent . propertyKey
6166 : 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 ) ) {
6370 this . referenceId = metaProperty . slice ( FILE_PREFIX . length ) ;
6471 }
6572 }
@@ -87,10 +94,11 @@ export default class MetaProperty extends NodeBase {
8794 if ( referenceId ) {
8895 const fileName = pluginDriver . getFileName ( referenceId ) ;
8996 const relativePath = normalize ( relative ( dirname ( chunkId ) , fileName ) ) ;
97+ const isUrlObject = ! ! metaProperty ?. startsWith ( FILE_OBJ_PREFIX ) ;
9098 const replacement =
9199 pluginDriver . hookFirstSync ( 'resolveFileUrl' , [
92100 { chunkId, fileName, format, moduleId, referenceId, relativePath }
93- ] ) || relativeUrlMechanisms [ format ] ( relativePath ) ;
101+ ] ) || relativeUrlMechanisms [ format ] ( relativePath , isUrlObject ) ;
94102
95103 code . overwrite (
96104 ( parent as MemberExpression ) . start ,
@@ -126,7 +134,9 @@ export default class MetaProperty extends NodeBase {
126134 ) : void {
127135 this . preliminaryChunkId = preliminaryChunkId ;
128136 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
130140 ) [ format ] ;
131141 if ( accessedGlobals . length > 0 ) {
132142 this . scope . addAccessedGlobals ( accessedGlobals , accessedGlobalsByScope ) ;
@@ -154,13 +164,15 @@ const accessedFileUrlGlobals = {
154164 umd : [ 'document' , 'require' , 'URL' ]
155165} ;
156166
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' } ` ;
158169
159- const getRelativeUrlFromDocument = ( relativePath : string , umd = false ) =>
170+ const getRelativeUrlFromDocument = ( relativePath : string , asObject : boolean , umd = false ) =>
160171 getResolveUrl (
161172 `'${ escapeId ( relativePath ) } ', ${
162173 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
164176 ) ;
165177
166178const getGenericImportMetaMechanism =
@@ -174,10 +186,11 @@ const getGenericImportMetaMechanism =
174186 : 'undefined' ;
175187 } ;
176188
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' } ` ;
178191
179- const getFileUrlFromRelativePath = ( path : string ) =>
180- getFileUrlFromFullPath ( `__dirname + '/${ escapeId ( path ) } '` ) ;
192+ const getFileUrlFromRelativePath = ( path : string , asObject : boolean ) =>
193+ getFileUrlFromFullPath ( `__dirname + '/${ escapeId ( path ) } '` , asObject ) ;
181194
182195const getUrlFromDocument = ( chunkId : string , umd = false ) =>
183196 `${
@@ -186,42 +199,39 @@ const getUrlFromDocument = (chunkId: string, umd = false) =>
186199 chunkId
187200 ) } ', document.baseURI).href)`;
188201
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 ) => {
191207 if ( relativePath [ 0 ] !== '.' ) relativePath = './' + relativePath ;
192- return getResolveUrl ( `require.toUrl('${ escapeId ( relativePath ) } '), document.baseURI` ) ;
208+ return getResolveUrl ( `require.toUrl('${ escapeId ( relativePath ) } '), document.baseURI` , asObject ) ;
193209 } ,
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 ) } )`
205219} ;
206220
207221const importMetaMechanisms : Record <
208222 string ,
209223 ( property : string | null , options : { chunkId : string ; snippets : GenerateCodeSnippets } ) => string
210224> = {
211- amd : getGenericImportMetaMechanism ( ( ) => getResolveUrl ( `module.uri, document.baseURI` ) ) ,
225+ amd : getGenericImportMetaMechanism ( ( ) => getResolveUrl ( `module.uri, document.baseURI` , false ) ) ,
212226 cjs : getGenericImportMetaMechanism (
213227 chunkId =>
214- `(typeof document === 'undefined' ? ${ getFileUrlFromFullPath (
215- '__filename'
216- ) } : ${ getUrlFromDocument ( chunkId ) } )`
228+ `(typeof document === 'undefined' ? ${ getFileUrlFromFullPath ( '__filename' , false ) } : ${ getUrlFromDocument ( chunkId ) } )`
217229 ) ,
218230 iife : getGenericImportMetaMechanism ( chunkId => getUrlFromDocument ( chunkId ) ) ,
219231 system : ( property , { snippets : { getPropertyAccess } } ) =>
220232 property === null ? `module.meta` : `module.meta${ getPropertyAccess ( property ) } ` ,
221233 umd : getGenericImportMetaMechanism (
222234 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 ) } )`
226236 )
227237} ;
0 commit comments