Skip to content

Commit

Permalink
fix: package.json exports condition could be only for d.ts types import
Browse files Browse the repository at this point in the history
  • Loading branch information
3cp committed Dec 24, 2024
1 parent 913e333 commit 7e8041b
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 1 deletion.
4 changes: 3 additions & 1 deletion lib/package-reader.js
Original file line number Diff line number Diff line change
Expand Up @@ -411,7 +411,9 @@ function pickCondition(obj) {
if (condition && condition in obj) return pickCondition(obj[condition]);
}

throw new Error("Unexpected exports condition: " + JSON.stringify(obj));
// condition could be only for types, like "./action": { "types": "./types/index.d.ts" }
// the "./action" is only for import type in TypeScript.
return null;
}

function _exportsMain(exports) {
Expand Down
46 changes: 46 additions & 0 deletions test/package-reader.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -1839,3 +1839,49 @@ test('packageReader reads exports subpaths in package.json', async t => {
t.equal(err.message, "Resource foo/e is not allowed to be imported (foo package.json exports definition {\"./b\":\"./be.js\",\"./c\":\"./lib/c.js\",\"./d/*\":\"./lib/d/*.js\",\"./e\":null}).");
}
});

test('packageReader reads exports subpaths in package.json', async t => {
const r = await getReader('foo', {
'node_modules/foo/package.json': `{
"name": "foo",
"exports": {
".": {
"types": "./types/index.d.ts",
"worker": "./src/index-server.js",
"browser": "./src/index-client.js",
"default": "./src/index-server.js"
},
"./package.json": "./package.json",
"./action": {
"types": "./types/index.d.ts"
},
"./compiler": {
"types": "./types/index.d.ts",
"require": "./src/compiler/index.js",
"default": "./src/compiler/index.js"
}
}
}`,
'node_modules/foo/src/index-client.js': 'lorem',
'node_modules/foo/src/compiler/index.js': 'a',
});


const unit = await r.readMain();
t.equal(r.name, 'foo');
t.equal(r.mainPath, 'src/index-client.js');
t.deepEqual(r.exportsReplacement, {
'./action': null,
'./compiler': './src/compiler/index.js'
});

t.deepEqual(unit, {
path: 'node_modules/foo/src/index-client.js',
contents: 'lorem',
moduleId: 'foo/src/index-client.js',
packageName: 'foo',
packageMainPath: 'src/index-client.js',
alias: 'foo',
sourceMap: undefined
});
});

0 comments on commit 7e8041b

Please sign in to comment.