Skip to content

Commit

Permalink
Only set synthetic export names that were found during parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
kasperisager committed Mar 13, 2024
1 parent 3b662ba commit cda2b5a
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 70 deletions.
11 changes: 6 additions & 5 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ const Module = module.exports = exports = class Module {
this._protocol = null
this._bundle = null
this._function = null
this._names = null
this._handle = null

Module._modules.add(this)
Expand Down Expand Up @@ -179,7 +180,9 @@ const Module = module.exports = exports = class Module {
}
}

this._handle = binding.createSyntheticModule(this._url.href, names, Module._handle)
this._names = names

this._handle = binding.createSyntheticModule(this._url.href, this._names, Module._handle)
}

_evaluate (eagerRun = false) {
Expand Down Expand Up @@ -280,10 +283,8 @@ const Module = module.exports = exports = class Module {

module._evaluate()

binding.setExport(module._handle, 'default', module._exports)

for (const [key, value] of Object.entries(module._exports)) {
binding.setExport(module._handle, key, value)
for (const name of module._names) {
binding.setExport(module._handle, name, name === 'default' ? module._exports : module._exports[name])
}
}

Expand Down
88 changes: 23 additions & 65 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,10 +83,6 @@ test('load .js', (t) => {
t.teardown(onteardown)

const protocol = new Module.Protocol({
exists () {
return false
},

read (url) {
if (url.href === root + '/index.js') {
return 'module.exports = 42'
Expand Down Expand Up @@ -151,10 +147,6 @@ test('load .cjs', (t) => {
t.teardown(onteardown)

const protocol = new Module.Protocol({
exists () {
return false
},

read (url) {
if (url.href === root + '/index.cjs') {
return 'module.exports = 42'
Expand Down Expand Up @@ -226,10 +218,6 @@ test('load .cjs with top-level await', async (t) => {
t.teardown(onteardown)

const protocol = new Module.Protocol({
exists () {
return false
},

read (url) {
if (url.href === root + '/index.cjs') {
return 'await 42'
Expand Down Expand Up @@ -294,10 +282,6 @@ test('load .mjs', (t) => {
t.teardown(onteardown)

const protocol = new Module.Protocol({
exists () {
return false
},

read (url) {
if (url.href === root + '/index.mjs') {
return 'export default 42'
Expand Down Expand Up @@ -534,10 +518,6 @@ test('load .mjs with missing import', async (t) => {
t.teardown(onteardown)

const protocol = new Module.Protocol({
exists () {
return false
},

read (url) {
if (url.href === root + '/index.mjs') {
return 'import foo from \'./foo\''
Expand Down Expand Up @@ -610,10 +590,6 @@ test('load .mjs with top-level await', (t) => {
t.teardown(onteardown)

const protocol = new Module.Protocol({
exists () {
return false
},

read (url) {
if (url.href === root + '/index.mjs') {
return 'await 42'
Expand Down Expand Up @@ -707,8 +683,8 @@ test('load .json', (t) => {
t.teardown(onteardown)

const protocol = new Module.Protocol({
exists () {
return false
exists (url) {
return url.href === root + '/native.bare'
},

read (url) {
Expand All @@ -723,50 +699,56 @@ test('load .json', (t) => {
t.is(Module.load(new URL(root + '/index.json'), { protocol }).exports, 42)
})

test('load .bare', async (t) => {
test('load .cjs with .bare import', async (t) => {
t.teardown(onteardown)

const protocol = new Module.Protocol({
exists (url) {
return (
url.href === root + '/index.js' ||
url.href === root + '/native.bare'
)
return url.href.endsWith('.bare')
},

read (url) {
if (url.href === root + '/index.js') {
if (url.href === root + '/index.cjs') {
return 'require(\'/native.bare\')'
}

t.fail()
}
})

await t.exception(() => Module.load(new URL(root + '/index.js'), { protocol }))
const resolutions = {
[root + '/index.cjs']: {
'/native.bare': __dirname + '/prebuilds/' + Bare.Addon.host + '/bare-module.bare'
}
}

Module.load(new URL(root + '/index.cjs'), { protocol, resolutions })
})

test('load .node', async (t) => {
test('load .mjs with .bare import', async (t) => {
t.teardown(onteardown)

const protocol = new Module.Protocol({
exists (url) {
return (
url.href === root + '/index.js' ||
url.href === root + '/native.node'
)
return url.href.endsWith('.bare')
},

read (url) {
if (url.href === root + '/index.js') {
return 'require(\'/native.node\')'
if (url.href === root + '/index.mjs') {
return 'import \'/native.bare\''
}

t.fail()
}
})

await t.exception(() => Module.load(new URL(root + '/index.js'), { protocol }))
const resolutions = {
[root + '/index.mjs']: {
'/native.bare': __dirname + '/prebuilds/' + Bare.Addon.host + '/bare-module.bare'
}
}

Module.load(new URL(root + '/index.mjs'), { protocol, resolutions })
})

test('load .bundle', (t) => {
Expand Down Expand Up @@ -837,10 +819,6 @@ test.skip('load specific module within .bundle', (t) => {
.toBuffer()

const protocol = new Module.Protocol({
exists () {
return false
},

read (url) {
if (url.href === root + '/app.bundle') {
return bundle
Expand All @@ -865,10 +843,6 @@ test.skip('load specific module within nested .bundle', (t) => {
.toBuffer()

const protocol = new Module.Protocol({
exists () {
return false
},

read (url) {
if (url.href === root + '/foo.bundle') {
return bundleB
Expand Down Expand Up @@ -947,10 +921,6 @@ test.skip('resolve specific module within .bundle', (t) => {
.toBuffer()

const protocol = new Module.Protocol({
exists () {
return false
},

read (url) {
if (url.href === root + '/app.bundle') {
return bundle
Expand All @@ -975,10 +945,6 @@ test.skip('resolve specific module within nested .bundle', (t) => {
.toBuffer()

const protocol = new Module.Protocol({
exists () {
return false
},

read (url) {
if (url.href === root + '/foo.bundle') {
return bundleB
Expand All @@ -995,10 +961,6 @@ test('load unknown extension', (t) => {
t.teardown(onteardown)

const protocol = new Module.Protocol({
exists () {
return false
},

read (url) {
if (url.href === root + '/index.foo') {
return 'module.exports = 42'
Expand All @@ -1015,10 +977,6 @@ test('load unknown extension with default type', (t) => {
t.teardown(onteardown)

const protocol = new Module.Protocol({
exists () {
return false
},

read (url) {
if (url.href === root + '/index.foo') {
return '42'
Expand Down

0 comments on commit cda2b5a

Please sign in to comment.