33const {
44 ArrayPrototypeMap,
55 ArrayPrototypePush,
6- Boolean,
76 FunctionPrototypeCall,
87 JSONParse,
98 ObjectKeys,
@@ -49,6 +48,7 @@ let debug = require('internal/util/debuglog').debuglog('esm', (fn) => {
4948} ) ;
5049const { emitExperimentalWarning, kEmptyObject, setOwnProperty, isWindows } = require ( 'internal/util' ) ;
5150const {
51+ ERR_INVALID_RETURN_PROPERTY_VALUE ,
5252 ERR_UNKNOWN_BUILTIN_MODULE ,
5353} = require ( 'internal/errors' ) . codes ;
5454const { maybeCacheSourceMap } = require ( 'internal/source_map/source_map_cache' ) ;
@@ -225,6 +225,46 @@ function createCJSModuleWrap(url, source, isMain, loadCJS = loadCJSModule) {
225225 } , module ) ;
226226}
227227
228+ /**
229+ * Creates a ModuleWrap object for a CommonJS module without source texts.
230+ * @param {string } url - The URL of the module.
231+ * @param {boolean } isMain - Whether the module is the main module.
232+ * @returns {ModuleWrap } The ModuleWrap object for the CommonJS module.
233+ */
234+ function createCJSNoSourceModuleWrap ( url , isMain ) {
235+ debug ( `Translating CJSModule without source ${ url } ` ) ;
236+
237+ const filename = urlToFilename ( url ) ;
238+
239+ const module = cjsEmplaceModuleCacheEntry ( filename ) ;
240+ cjsCache . set ( url , module ) ;
241+
242+ if ( isMain ) {
243+ setOwnProperty ( process , 'mainModule' , module ) ;
244+ }
245+
246+ // Addon export names are not known until the addon is loaded.
247+ const exportNames = [ 'default' , 'module.exports' ] ;
248+ return new ModuleWrap ( url , undefined , exportNames , function ( ) {
249+ debug ( `Loading CJSModule ${ url } ` ) ;
250+
251+ if ( ! module . loaded ) {
252+ wrapModuleLoad ( filename , null , isMain ) ;
253+ }
254+
255+ let exports ;
256+ if ( module [ kModuleExport ] !== undefined ) {
257+ exports = module [ kModuleExport ] ;
258+ module [ kModuleExport ] = undefined ;
259+ } else {
260+ ( { exports } = module ) ;
261+ }
262+
263+ this . setExport ( 'default' , exports ) ;
264+ this . setExport ( 'module.exports' , exports ) ;
265+ } , module ) ;
266+ }
267+
228268translators . set ( 'commonjs-sync' , function requireCommonJS ( url , source , isMain ) {
229269 initCJSParseSync ( ) ;
230270
@@ -276,26 +316,37 @@ translators.set('commonjs', function commonjsStrategy(url, source, isMain) {
276316 return createCJSModuleWrap ( url , source , isMain , cjsLoader ) ;
277317} ) ;
278318
319+ /**
320+ * Get or create an entry in the CJS module cache for the given filename.
321+ * @param {string } filename CJS module filename
322+ * @returns {CJSModule } the cached CJS module entry
323+ */
324+ function cjsEmplaceModuleCacheEntry ( filename , exportNames ) {
325+ // TODO: Do we want to keep hitting the user mutable CJS loader here?
326+ let module = CJSModule . _cache [ filename ] ;
327+ if ( module ) {
328+ return module ;
329+ }
330+
331+ module = new CJSModule ( filename ) ;
332+ module . filename = filename ;
333+ module . paths = CJSModule . _nodeModulePaths ( module . path ) ;
334+ module [ kIsCachedByESMLoader ] = true ;
335+ CJSModule . _cache [ filename ] = module ;
336+
337+ return module ;
338+ }
339+
279340/**
280341 * Pre-parses a CommonJS module's exports and re-exports.
281342 * @param {string } filename - The filename of the module.
282343 * @param {string } [source] - The source code of the module.
283344 */
284345function cjsPreparseModuleExports ( filename , source ) {
285- // TODO: Do we want to keep hitting the user mutable CJS loader here?
286- let module = CJSModule . _cache [ filename ] ;
287- if ( module && module [ kModuleExportNames ] !== undefined ) {
346+ const module = cjsEmplaceModuleCacheEntry ( filename ) ;
347+ if ( module [ kModuleExportNames ] !== undefined ) {
288348 return { module, exportNames : module [ kModuleExportNames ] } ;
289349 }
290- const loaded = Boolean ( module ) ;
291- if ( ! loaded ) {
292- module = new CJSModule ( filename ) ;
293- module . filename = filename ;
294- module . paths = CJSModule . _nodeModulePaths ( module . path ) ;
295- module [ kIsCachedByESMLoader ] = true ;
296- module [ kModuleSource ] = source ;
297- CJSModule . _cache [ filename ] = module ;
298- }
299350
300351 let exports , reexports ;
301352 try {
@@ -308,11 +359,10 @@ function cjsPreparseModuleExports(filename, source) {
308359 const exportNames = new SafeSet ( new SafeArrayIterator ( exports ) ) ;
309360
310361 // Set first for cycles.
362+ module [ kModuleSource ] = source ;
311363 module [ kModuleExportNames ] = exportNames ;
312364
313365 if ( reexports . length ) {
314- module . filename = filename ;
315- module . paths = CJSModule . _nodeModulePaths ( module . path ) ;
316366 for ( let i = 0 ; i < reexports . length ; i ++ ) {
317367 const reexport = reexports [ i ] ;
318368 let resolved ;
@@ -459,6 +509,25 @@ translators.set('wasm', async function(url, source) {
459509 } ) . module ;
460510} ) ;
461511
512+ // Strategy for loading a addon
513+ translators . set ( 'addon' , function ( url , source , isMain ) {
514+ emitExperimentalWarning ( 'Importing addons' ) ;
515+
516+ // The addon must be loaded from file system with dlopen. Assert
517+ // the source is null.
518+ if ( source !== null ) {
519+ throw new ERR_INVALID_RETURN_PROPERTY_VALUE (
520+ 'null' ,
521+ 'load' ,
522+ 'source' ,
523+ source ) ;
524+ }
525+
526+ debug ( `Translating addon ${ url } ` ) ;
527+
528+ return createCJSNoSourceModuleWrap ( url , isMain ) ;
529+ } ) ;
530+
462531// Strategy for loading a commonjs TypeScript module
463532translators . set ( 'commonjs-typescript' , function ( url , source ) {
464533 emitExperimentalWarning ( 'Type Stripping' ) ;
0 commit comments