Skip to content

Commit

Permalink
Support caller provided builtins
Browse files Browse the repository at this point in the history
  • Loading branch information
kasperisager committed Nov 17, 2023
1 parent 9a37a91 commit d7aa004
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 19 deletions.
47 changes: 28 additions & 19 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -239,9 +239,10 @@ const Module = module.exports = exports = class Module {
}

let {
imports = null,
protocol = this._protocolFor(specifier, this._protocols['file:']),
referrer = null,
protocol = this._protocolFor(specifier, this._protocols['file:']),
imports = null,
builtins = null,
dynamic = false,
main = referrer ? referrer._main : null,
type = 0,
Expand Down Expand Up @@ -270,22 +271,26 @@ const Module = module.exports = exports = class Module {

const module = this._cache[specifier] = new this(specifier)

module._defaultType = defaultType
module._main = main || module
module._info = this._loadPackageManifest(path.dirname(module._filename), protocol)
if (builtins && specifier in builtins) {
module._exports = builtins[specifier]
} else {
module._defaultType = defaultType
module._main = main || module
module._info = this._loadPackageManifest(path.dirname(module._filename), protocol)

let extension = this._extensionFor(type) || path.extname(specifier)
let extension = this._extensionFor(type) || path.extname(specifier)

if (extension in this._extensions === false) {
if (defaultType) extension = this._extensionFor(defaultType) || '.js'
else extension = '.js'
}
if (extension in this._extensions === false) {
if (defaultType) extension = this._extensionFor(defaultType) || '.js'
else extension = '.js'
}

if (extension === '.bundle' && path.extname(specifier) !== extension) {
throw errors.INVALID_BUNDLE_EXTENSION(`Invalid extension for bundle '${specifier}'`)
}
if (extension === '.bundle' && path.extname(specifier) !== extension) {
throw errors.INVALID_BUNDLE_EXTENSION(`Invalid extension for bundle '${specifier}'`)
}

this._extensions[extension].call(this, module, source, referrer, protocol, imports)
this._extensions[extension].call(this, module, source, referrer, protocol, imports)
}

return this._transform(module, referrer, dynamic)
}
Expand Down Expand Up @@ -320,9 +325,10 @@ const Module = module.exports = exports = class Module {
}

let {
imports = null,
protocol = this._protocols['file:'],
referrer = null,
protocol = this._protocols['file:'],
imports = null,
builtins = null,
conditions = ['import', 'require', 'bare', 'node']
} = opts

Expand All @@ -344,7 +350,7 @@ const Module = module.exports = exports = class Module {
})
}

const [resolved = null] = this._resolve(specifier, dirname, protocol, imports, conditions)
const [resolved = null] = this._resolve(specifier, dirname, protocol, imports, builtins, conditions)

if (resolved === null) {
let msg = `Cannot find module '${specifier}'`
Expand All @@ -357,7 +363,7 @@ const Module = module.exports = exports = class Module {
return protocol.postresolve(resolved, dirname)
}

static * _resolve (specifier, dirname, protocol, imports, conditions) {
static * _resolve (specifier, dirname, protocol, imports, builtins, conditions) {
const info = this._loadPackageManifest(dirname, protocol)

specifier = this._mapConditionalSpecifier(
Expand All @@ -372,6 +378,8 @@ const Module = module.exports = exports = class Module {

yield * protocol.resolve(specifier, dirname, imports)

if (builtins && specifier in builtins) yield specifier

if (path.isAbsolute(specifier)) {
yield * this._resolveFile(specifier, protocol)
yield * this._resolveDirectory(specifier, protocol, conditions)
Expand Down Expand Up @@ -783,7 +791,8 @@ Module._protocols['file:'] = new Protocol({
},

postresolve (specifier) {
return binding.realpath(specifier)
if (path.isAbsolute(specifier)) return binding.realpath(specifier)
return specifier
},

exists (filename) {
Expand Down
11 changes: 11 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1361,6 +1361,17 @@ test('imports in node_modules', (t) => {
Module.load('/node_modules/foo/foo.js', { protocol })
})

test('builtins', (t) => {
t.teardown(onteardown)

const builtins = {
foo: 42
}

t.is(Module.resolve('foo', { builtins }), 'foo')
t.is(Module.load('foo', { builtins }).exports, 42)
})

test('load file that cannot be read', async (t) => {
t.teardown(onteardown)

Expand Down

0 comments on commit d7aa004

Please sign in to comment.