@@ -253,7 +253,7 @@ export async function transformJSCode(source : string, moduleSourceType : boolea
253253 comments : devMode ,
254254 retainLines : devMode ,
255255 //envName: devMode ? 'development' : 'production', see 'process.env.BABEL_ENV': JSON.stringify(mode),
256-
256+ filename : filename . toString ( ) ,
257257 //minified,
258258 sourceType : moduleSourceType ? 'module' : 'script' ,
259259 } ) ;
@@ -324,7 +324,7 @@ export async function loadModuleInternal(pathCx : PathContext, options : Options
324324 * Create a cjs module
325325 * @internal
326326 */
327- export function defaultCreateCJSModule ( refPath : AbstractPath , source : string , options : Options ) : Module {
327+ export async function defaultCreateCJSModule ( refPath : AbstractPath , source : string , options : Options ) : Promise < Module > {
328328
329329 const { moduleCache, pathResolve, getResource } = options ;
330330
@@ -348,8 +348,31 @@ export function defaultCreateCJSModule(refPath : AbstractPath, source : string,
348348
349349 // see https://github.com/nodejs/node/blob/a46b21f556a83e43965897088778ddc7d46019ae/lib/internal/modules/cjs/loader.js#L195-L198
350350 // 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 )
353376
354377 return module ;
355378}
@@ -379,7 +402,7 @@ export async function createJSModule(source : string, moduleSourceType : boolean
379402 } ) ;
380403
381404 await loadDeps ( filename , depsList , options ) ;
382- return createCJSModule ( filename , transformedSource , options ) . exports ;
405+ return ( await createCJSModule ( filename , transformedSource , options ) ) . exports ;
383406}
384407
385408
0 commit comments