Skip to content

fix(NODE-5025): no type definitions for es module #563

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,14 @@
"main": "./lib/bson.cjs",
"module": "./lib/bson.mjs",
"exports": {
"import": "./lib/bson.mjs",
"require": "./lib/bson.cjs",
"import": {
"types": "./bson.d.ts",
"default": "./lib/bson.mjs"
},
"require": {
"types": "./bson.d.ts",
"default": "./lib/bson.cjs"
},
"react-native": "./lib/bson.cjs",
"browser": "./lib/bson.mjs"
},
Expand Down
41 changes: 41 additions & 0 deletions test/node/exports.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { expect } from 'chai';
import * as BSON from '../register-bson';
import { sorted, byStrings } from './tools/utils';
import { readFile } from 'fs/promises';
import { resolve } from 'path';

const EXPECTED_EXPORTS = [
// This is our added web indicator not a real export but a small exception for this test.
Expand Down Expand Up @@ -52,4 +54,43 @@ describe('bson entrypoint', () => {
expect(BSON.EJSON).to.be.frozen;
expect(Object.getPrototypeOf(BSON.EJSON)).to.be.null;
});

context('package.json entrypoint', () => {
let pkg: typeof import('../../package.json');
before(async () => {
pkg = await readFile(resolve(__dirname, '../../package.json'), {
encoding: 'utf8'
// JSON.parse will preserve key order
}).then(c => JSON.parse(c));
});

it('maintains the order of keys in exports conditions', async () => {
expect(pkg).property('exports').is.a('object');
expect(pkg).nested.property('exports.import').is.a('object');
expect(pkg).nested.property('exports.require').is.a('object');

expect(
Object.keys(pkg.exports),
'Order matters in the exports fields. import/require need to proceed the "bundler" targets (RN/browser) and react-native MUST proceed browser'
).to.deep.equal(['import', 'require', 'react-native', 'browser']);

// https://www.typescriptlang.org/docs/handbook/release-notes/typescript-4-7.html#packagejson-exports-imports-and-self-referencing
expect(
Object.keys(pkg.exports.import),
'TS docs say that `types` should ALWAYS proceed `default`'
).to.deep.equal(['types', 'default']);
expect(
Object.keys(pkg.exports.require),
'TS docs say that `types` should ALWAYS proceed `default`'
).to.deep.equal(['types', 'default']);

expect(Object.keys(pkg['compass:exports'])).to.deep.equal(['import', 'require']);
});

it('has the equivalent "bson.d.ts" value for all "types" specifiers', () => {
expect(pkg).property('types', 'bson.d.ts');
expect(pkg).nested.property('exports.import.types', './bson.d.ts');
expect(pkg).nested.property('exports.require.types', './bson.d.ts');
});
});
});