Skip to content

Commit 50e90fc

Browse files
authored
fix(NODE-5025): no type definitions for es module (#563)
1 parent d5088af commit 50e90fc

File tree

2 files changed

+49
-2
lines changed

2 files changed

+49
-2
lines changed

Diff for: package.json

+8-2
Original file line numberDiff line numberDiff line change
@@ -77,8 +77,14 @@
7777
"main": "./lib/bson.cjs",
7878
"module": "./lib/bson.mjs",
7979
"exports": {
80-
"import": "./lib/bson.mjs",
81-
"require": "./lib/bson.cjs",
80+
"import": {
81+
"types": "./bson.d.ts",
82+
"default": "./lib/bson.mjs"
83+
},
84+
"require": {
85+
"types": "./bson.d.ts",
86+
"default": "./lib/bson.cjs"
87+
},
8288
"react-native": "./lib/bson.cjs",
8389
"browser": "./lib/bson.mjs"
8490
},

Diff for: test/node/exports.test.ts

+41
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import { expect } from 'chai';
22
import * as BSON from '../register-bson';
33
import { sorted, byStrings } from './tools/utils';
4+
import { readFile } from 'fs/promises';
5+
import { resolve } from 'path';
46

57
const EXPECTED_EXPORTS = [
68
// This is our added web indicator not a real export but a small exception for this test.
@@ -53,4 +55,43 @@ describe('bson entrypoint', () => {
5355
expect(BSON.EJSON).to.be.frozen;
5456
expect(Object.getPrototypeOf(BSON.EJSON)).to.be.null;
5557
});
58+
59+
context('package.json entrypoint', () => {
60+
let pkg: typeof import('../../package.json');
61+
before(async () => {
62+
pkg = await readFile(resolve(__dirname, '../../package.json'), {
63+
encoding: 'utf8'
64+
// JSON.parse will preserve key order
65+
}).then(c => JSON.parse(c));
66+
});
67+
68+
it('maintains the order of keys in exports conditions', async () => {
69+
expect(pkg).property('exports').is.a('object');
70+
expect(pkg).nested.property('exports.import').is.a('object');
71+
expect(pkg).nested.property('exports.require').is.a('object');
72+
73+
expect(
74+
Object.keys(pkg.exports),
75+
'Order matters in the exports fields. import/require need to proceed the "bundler" targets (RN/browser) and react-native MUST proceed browser'
76+
).to.deep.equal(['import', 'require', 'react-native', 'browser']);
77+
78+
// https://www.typescriptlang.org/docs/handbook/release-notes/typescript-4-7.html#packagejson-exports-imports-and-self-referencing
79+
expect(
80+
Object.keys(pkg.exports.import),
81+
'TS docs say that `types` should ALWAYS proceed `default`'
82+
).to.deep.equal(['types', 'default']);
83+
expect(
84+
Object.keys(pkg.exports.require),
85+
'TS docs say that `types` should ALWAYS proceed `default`'
86+
).to.deep.equal(['types', 'default']);
87+
88+
expect(Object.keys(pkg['compass:exports'])).to.deep.equal(['import', 'require']);
89+
});
90+
91+
it('has the equivalent "bson.d.ts" value for all "types" specifiers', () => {
92+
expect(pkg).property('types', 'bson.d.ts');
93+
expect(pkg).nested.property('exports.import.types', './bson.d.ts');
94+
expect(pkg).nested.property('exports.require.types', './bson.d.ts');
95+
});
96+
});
5697
});

0 commit comments

Comments
 (0)