From 669c8dd7d11c1ec011f93d0246e110c6f8198a19 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kasper=20Isager=20Dalsgar=C3=B0?= Date: Thu, 3 Oct 2024 14:31:02 +0200 Subject: [PATCH] Test `asset` condition --- test.js | 67 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 67 insertions(+) diff --git a/test.js b/test.js index ef8bb9d..f879466 100644 --- a/test.js +++ b/test.js @@ -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)