Skip to content

Commit

Permalink
Test asset condition
Browse files Browse the repository at this point in the history
  • Loading branch information
kasperisager committed Oct 3, 2024
1 parent 688cfb2 commit 669c8dd
Showing 1 changed file with 67 additions and 0 deletions.
67 changes: 67 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1820,6 +1820,73 @@ 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('imports in node_modules', (t) => {
t.teardown(onteardown)

Expand Down

0 comments on commit 669c8dd

Please sign in to comment.