@@ -253,7 +253,7 @@ export async function transformJSCode(source : string, moduleSourceType : boolea
253
253
comments : devMode ,
254
254
retainLines : devMode ,
255
255
//envName: devMode ? 'development' : 'production', see 'process.env.BABEL_ENV': JSON.stringify(mode),
256
-
256
+ filename : filename . toString ( ) ,
257
257
//minified,
258
258
sourceType : moduleSourceType ? 'module' : 'script' ,
259
259
} ) ;
@@ -324,7 +324,7 @@ export async function loadModuleInternal(pathCx : PathContext, options : Options
324
324
* Create a cjs module
325
325
* @internal
326
326
*/
327
- export function defaultCreateCJSModule ( refPath : AbstractPath , source : string , options : Options ) : Module {
327
+ export async function defaultCreateCJSModule ( refPath : AbstractPath , source : string , options : Options ) : Promise < Module > {
328
328
329
329
const { moduleCache, pathResolve, getResource } = options ;
330
330
@@ -348,8 +348,31 @@ export function defaultCreateCJSModule(refPath : AbstractPath, source : string,
348
348
349
349
// see https://github.com/nodejs/node/blob/a46b21f556a83e43965897088778ddc7d46019ae/lib/internal/modules/cjs/loader.js#L195-L198
350
350
// see https://github.com/nodejs/node/blob/a46b21f556a83e43965897088778ddc7d46019ae/lib/internal/modules/cjs/loader.js#L1102
351
- const moduleFunction = Function ( 'exports' , 'require' , 'module' , '__filename' , '__dirname' , '__vsfcl_import__' , source ) ;
352
- moduleFunction . call ( module . exports , module . exports , require , module , refPath , pathResolve ( { refPath, relPath : '.' } , options ) , importFunction ) ;
351
+ const wrappedSource = `(exports, require, module, __filename, __dirname, __vsfcl_import__) => { ${ source } \r\n }`
352
+
353
+ let ast = babel_parse ( wrappedSource , {
354
+ sourceType : 'module' ,
355
+ sourceFilename : refPath . toString ( ) ,
356
+ } ) ;
357
+
358
+ const transformedScript = await babel_transformFromAstAsync ( ast , wrappedSource , {
359
+ sourceMaps : "inline" ,
360
+ babelrc : false ,
361
+ configFile : false ,
362
+ highlightCode : false ,
363
+ filename : refPath . toString ( ) ,
364
+ sourceType : 'module' ,
365
+ } ) ;
366
+
367
+ if ( transformedScript === null || transformedScript . code == null ) { // == null or undefined
368
+
369
+ const msg = `unable to transform script "${ refPath . toString ( ) } "` ;
370
+ options . log ?.( 'error' , msg ) ;
371
+ throw new Error ( msg )
372
+ }
373
+
374
+
375
+ eval ( transformedScript . code ) ( module . exports , require , module , refPath , pathResolve ( { refPath, relPath : '.' } , options ) , importFunction )
353
376
354
377
return module ;
355
378
}
@@ -379,7 +402,7 @@ export async function createJSModule(source : string, moduleSourceType : boolean
379
402
} ) ;
380
403
381
404
await loadDeps ( filename , depsList , options ) ;
382
- return createCJSModule ( filename , transformedSource , options ) . exports ;
405
+ return ( await createCJSModule ( filename , transformedSource , options ) ) . exports ;
383
406
}
384
407
385
408
0 commit comments