1
1
'use strict'
2
2
3
- const getEsmExports = require ( './get-esm-exports.js' )
4
- const { parse : parseCjs } = require ( 'cjs-module-lexer' )
5
- const { readFileSync } = require ( 'fs' )
6
- const { builtinModules } = require ( 'module' )
7
- const { fileURLToPath, pathToFileURL } = require ( 'url' )
8
- const { dirname } = require ( 'path' )
3
+ import getEsmExports from './get-esm-exports.mjs'
4
+ import { parse as parseCjs , init as parserInit } from 'cjs-module-lexer'
5
+ import { readFileSync } from 'fs'
6
+ import { builtinModules , createRequire } from 'module'
7
+ import { fileURLToPath , pathToFileURL } from 'url'
8
+ import { dirname } from 'path'
9
+
10
+ await parserInit ( )
9
11
10
12
function addDefault ( arr ) {
11
13
return new Set ( [ 'default' , ...arr ] )
@@ -14,9 +16,15 @@ function addDefault (arr) {
14
16
// Cached exports for Node built-in modules
15
17
const BUILT_INS = new Map ( )
16
18
19
+ let require
20
+
17
21
function getExportsForNodeBuiltIn ( name ) {
18
22
let exports = BUILT_INS . get ( )
19
23
24
+ if ( ! require ) {
25
+ require = createRequire ( import . meta. url )
26
+ }
27
+
20
28
if ( ! exports ) {
21
29
exports = new Set ( addDefault ( Object . keys ( require ( name ) ) ) )
22
30
BUILT_INS . set ( name , exports )
@@ -47,6 +55,9 @@ async function getCjsExports (url, context, parentLoad, source) {
47
55
re = './'
48
56
}
49
57
// Resolve the re-exported module relative to the current module.
58
+ if ( ! require ) {
59
+ require = createRequire ( import . meta. url )
60
+ }
50
61
const newUrl = pathToFileURL ( require . resolve ( re , { paths : [ dirname ( fileURLToPath ( url ) ) ] } ) ) . href
51
62
52
63
if ( newUrl . endsWith ( '.node' ) || newUrl . endsWith ( '.json' ) ) {
@@ -81,7 +92,7 @@ async function getCjsExports (url, context, parentLoad, source) {
81
92
* Please see {@link getEsmExports} for caveats on special identifiers that may
82
93
* be included in the result set.
83
94
*/
84
- async function getExports ( url , context , parentLoad ) {
95
+ export default async function getExports ( url , context , parentLoad ) {
85
96
// `parentLoad` gives us the possibility of getting the source
86
97
// from an upstream loader. This doesn't always work though,
87
98
// so later on we fall back to reading it from disk.
@@ -125,5 +136,3 @@ async function getExports (url, context, parentLoad) {
125
136
throw err
126
137
}
127
138
}
128
-
129
- module . exports = getExports
0 commit comments