@@ -10,6 +10,9 @@ import {
1010 ensureStatementsHoisted ,
1111 wrapInterop ,
1212 getModuleName ,
13+ withExtension ,
14+ resolveRelativeImportPaths ,
15+ amdImportId ,
1316} from "@babel/helper-module-transforms" ;
1417import type { PluginOptions } from "@babel/helper-module-transforms" ;
1518import { types as t , template , type NodePath } from "@babel/core" ;
@@ -45,6 +48,8 @@ export interface Options extends PluginOptions {
4548 exactGlobals ?: boolean ;
4649 globals ?: Record < string , string > ;
4750 importInterop ?: RewriteModuleStatementsAndPrepareHeaderOptions [ "importInterop" ] ;
51+ /** Extension applied to relative CommonJS `require()` paths, e.g. ".js". */
52+ importFileExt ?: string ;
4853 /** @deprecated Use the `constantReexports` and `enumerableModuleMeta` assumptions instead. */
4954 loose ?: boolean ;
5055 noInterop ?: boolean ;
@@ -70,6 +75,7 @@ export default declare((api, options: Options) => {
7075 strictMode,
7176 noInterop,
7277 importInterop,
78+ importFileExt,
7379 } = options ;
7480
7581 const constantReexports =
@@ -96,7 +102,8 @@ export default declare((api, options: Options) => {
96102 let initAssignments = [ ] ;
97103
98104 if ( exactGlobals ) {
99- const globalName = browserGlobals [ moduleNameOrBasename ] ;
105+ const globalName =
106+ browserGlobals [ filename ] || browserGlobals [ moduleNameOrBasename ] ;
100107
101108 if ( globalName ) {
102109 initAssignments = [ ] ;
@@ -136,10 +143,12 @@ export default declare((api, options: Options) => {
136143 browserGlobals : Record < string , string > ,
137144 exactGlobals : boolean | undefined ,
138145 source : string ,
146+ filename : string | undefined ,
139147 ) {
140148 let memberExpression : t . MemberExpression ;
141149 if ( exactGlobals ) {
142- const globalRef = browserGlobals [ source ] ;
150+ const globalRef =
151+ ( filename && browserGlobals [ filename ] ) || browserGlobals [ source ] ;
143152 if ( globalRef ) {
144153 memberExpression = globalRef
145154 . split ( "." )
@@ -173,9 +182,10 @@ export default declare((api, options: Options) => {
173182 exit ( path ) {
174183 if ( ! isModule ( path ) ) return ;
175184
185+ const fileOpts = this . file . opts ;
176186 const browserGlobals = globals || { } ;
177187
178- const moduleName = getModuleName ( this . file . opts , options ) ;
188+ const moduleName = getModuleName ( fileOpts , options ) ;
179189 let moduleNameLiteral : t . StringLiteral | undefined ;
180190 if ( moduleName ) moduleNameLiteral = t . stringLiteral ( moduleName ) ;
181191
@@ -208,14 +218,25 @@ export default declare((api, options: Options) => {
208218 }
209219
210220 for ( const [ source , metadata ] of meta . source ) {
211- amdArgs . push ( t . stringLiteral ( source ) ) ;
221+ const {
222+ filename : sourceFilename ,
223+ filenameRelative : sourceRelative ,
224+ } = resolveRelativeImportPaths ( source , fileOpts ) ;
225+ amdArgs . push (
226+ t . stringLiteral ( amdImportId ( source , fileOpts , options ) ) ,
227+ ) ;
212228 commonjsArgs . push (
213229 t . callExpression ( t . identifier ( "require" ) , [
214- t . stringLiteral ( source ) ,
230+ t . stringLiteral ( withExtension ( source , importFileExt ) ) ,
215231 ] ) ,
216232 ) ;
217233 browserArgs . push (
218- buildBrowserArg ( browserGlobals , exactGlobals , source ) ,
234+ buildBrowserArg (
235+ browserGlobals ,
236+ exactGlobals ,
237+ source ,
238+ sourceRelative || sourceFilename ,
239+ ) ,
219240 ) ;
220241 importNames . push ( t . identifier ( metadata . name ) ) ;
221242
@@ -267,7 +288,10 @@ export default declare((api, options: Options) => {
267288 GLOBAL_TO_ASSIGN : buildBrowserInit (
268289 browserGlobals ,
269290 exactGlobals ,
270- this . filename || "unknown" ,
291+ fileOpts . filenameRelative ||
292+ fileOpts . filename ||
293+ this . filename ||
294+ "unknown" ,
271295 moduleNameLiteral ,
272296 ) ,
273297 } ) as t . Statement ,
0 commit comments