Skip to content

Commit

Permalink
Allow modules in bundles to resolve modules outside of bundle (#11)
Browse files Browse the repository at this point in the history
  • Loading branch information
kasperisager authored Oct 9, 2024
1 parent bd42259 commit d56ce6a
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 9 deletions.
12 changes: 3 additions & 9 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -767,22 +767,16 @@ Module._extensions['.bundle'] = function (module, source, referrer) {
module._resolutions = bundle.resolutions

module._protocol = protocol.extend({
preresolve (context, specifier) {
return specifier
},

postresolve (context, url) {
return url
return bundle.exists(url.href) ? url : context.postresolve(url)
},

* resolve () {},

exists (context, url) {
return bundle.exists(url.href)
return bundle.exists(url.href) || context.exists(url)
},

read (context, url) {
return bundle.read(url.href)
return bundle.read(url.href) || context.read(url)
}
})

Expand Down
24 changes: 24 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2831,6 +2831,30 @@ test('load .bundle with asset import, resolutions map', (t) => {
t.is(Module.load(new URL(root + '/app.bundle'), bundle.toBuffer()).exports, isWindows ? 'c:\\app.bundle\\baz.txt' : '/app.bundle/baz.txt')
})

test('load .bundle with asset import, resolutions map pointing outside .bundle', (t) => {
t.teardown(onteardown)

const protocol = new Module.Protocol({
exists (url) {
return url.href === root + '/bar.txt'
}
})

const bundle = new Module.Bundle()
.write('/foo.js', 'module.exports = require.asset(\'./bar.txt\')', { main: true })
.write('/baz.txt', 'hello world', { asset: true })

bundle.resolutions = {
'/foo.js': {
'./bar.txt': {
asset: root + '/bar.txt'
}
}
}

t.is(Module.load(new URL(root + '/app.bundle'), bundle.toBuffer(), { protocol }).exports, isWindows ? 'c:\\bar.txt' : '/bar.txt')
})

function onteardown () {
// TODO Provide a public API for clearing the cache.
Module._cache = Object.create(null)
Expand Down

0 comments on commit d56ce6a

Please sign in to comment.