Skip to content

Commit

Permalink
Support asset condition (#10)
Browse files Browse the repository at this point in the history
  • Loading branch information
kasperisager authored Oct 3, 2024
1 parent 40a0d99 commit 93d8332
Show file tree
Hide file tree
Showing 2 changed files with 109 additions and 1 deletion.
2 changes: 1 addition & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -510,7 +510,7 @@ const Module = module.exports = exports = class Module {
} = opts

const [resolution = null] = resolve(specifier, parentURL, {
conditions,
conditions: ['asset', ...conditions],
imports,
resolutions
}, readPackage)
Expand Down
108 changes: 108 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1820,6 +1820,114 @@ test('conditional imports in package.json, import', (t) => {
Module.load(new URL(root + '/foo.mjs'), { protocol })
})

test('conditional imports in package.json, asset', (t) => {
t.teardown(onteardown)

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

read (url) {
if (url.href === root + '/package.json') {
return '{ "imports": { "bar": { "asset": "./bar.txt" } } }'
}

if (url.href === root + '/foo.cjs') {
return 'module.exports = require.asset(\'bar\')'
}

if (url.href === root + '/bar.txt') {
return 'hello world'
}

t.fail()
}
})

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

test('conditional imports in package.json, asset and default', (t) => {
t.teardown(onteardown)

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

read (url) {
if (url.href === root + '/package.json') {
return '{ "imports": { "bar": { "asset": "./bar.txt", "default": "./bar.js" } } }'
}

if (url.href === root + '/foo.cjs') {
return 'module.exports = [require.asset(\'bar\'), require(\'bar\')]'
}

if (url.href === root + '/bar.txt') {
return 'hello world'
}

if (url.href === root + '/bar.js') {
return 'module.exports = 42'
}

t.fail()
}
})

t.alike(Module.load(new URL(root + '/foo.cjs'), { protocol }).exports, [isWindows ? 'c:\\bar.txt' : '/bar.txt', 42])
})

test('conditional imports in package.json, asset and require without default', (t) => {
t.teardown(onteardown)

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

read (url) {
if (url.href === root + '/package.json') {
return '{ "imports": { "bar": { "asset": "./bar.txt" } } }'
}

if (url.href === root + '/foo.cjs') {
return 'module.exports = require(\'bar\')'
}

if (url.href === root + '/bar.txt') {
return 'hello world'
}

if (url.href === root + '/bar.js') {
return 'module.exports = 42'
}

t.fail()
}
})

try {
Module.load(new URL(root + '/foo.cjs'), { protocol })
t.fail()
} catch (err) {
t.comment(err.message)
}
})

test('imports in node_modules', (t) => {
t.teardown(onteardown)

Expand Down

0 comments on commit 93d8332

Please sign in to comment.