Skip to content

Commit 6ef65f2

Browse files
committed
refactor!(NODE-4701): remove default export and add named namespace export
1 parent 35a9234 commit 6ef65f2

File tree

13 files changed

+35
-70
lines changed

13 files changed

+35
-70
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ lib
2222
.nyc_output/
2323
coverage/
2424
*.d.ts
25+
!test/register-bson.d.ts
2526
*.tgz
2627
docs/public
2728

.nycrc.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,6 @@
1010
],
1111
"statements": 81,
1212
"branches": 73,
13-
"functions": 74,
13+
"functions": 71,
1414
"lines": 83
1515
}

api-extractor.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"$schema": "https://developer.microsoft.com/json-schemas/api-extractor/v7/api-extractor.schema.json",
3-
"mainEntryPointFilePath": "<projectFolder>/lib/bson.d.ts",
3+
"mainEntryPointFilePath": "<projectFolder>/lib/index.d.ts",
44
"apiReport": {
55
"enabled": false
66
},

bower.json

Lines changed: 0 additions & 26 deletions
This file was deleted.

docs/upgrade-to-v5.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,3 +165,8 @@ new Timestamp({ t: -2, i: 1 });
165165
new Timestamp({ t: 2, i: 0xFFFF_FFFF + 1 });
166166
// Will throw, both fields need to be less than or equal to the unsigned int32 max value
167167
```
168+
### The BSON default export has been removed.
169+
170+
* If you import BSON commonjs style `const BSON = require('bson')` then the `BSON.default` property is no longer present.
171+
* If you import BSON esmodule style `import BSON from 'bson'` then this code will crash upon loading. **TODO: This is not the case right now but it will be after NODE-4713.**
172+
* This error will throw: `SyntaxError: The requested module 'bson' does not provide an export named 'default'`.

etc/benchmarks/lib_runner.mjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ export async function getLibs() {
5959
const hash = stdout.trim();
6060
return {
6161
name: 'local',
62-
lib: await import('../../lib/bson.js'),
62+
lib: await import('../../lib/index.js'),
6363
version: hash
6464
};
6565
})(),

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,10 +80,10 @@
8080
"config": {
8181
"native": false
8282
},
83-
"main": "lib/bson.js",
83+
"main": "lib/index.js",
8484
"module": "dist/bson.esm.js",
8585
"browser": {
86-
"./lib/bson.js": "./dist/bson.browser.umd.js",
86+
"./lib/index.js": "./dist/bson.browser.umd.js",
8787
"./dist/bson.esm.js": "./dist/bson.browser.esm.js"
8888
},
8989
"engines": {

rollup.config.mjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ const tsConfig = {
3232
tsconfig: false,
3333
include: ['src/**/*']
3434
};
35-
const input = 'src/bson.ts';
35+
const input = 'src/index.ts';
3636

3737
const plugins = (options = { browser: false }) => {
3838
return [

src/bson.ts

Lines changed: 0 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -247,39 +247,3 @@ export function deserializeStream(
247247
// Return object containing end index of parsing and list of documents
248248
return index;
249249
}
250-
251-
/**
252-
* BSON default export
253-
* @deprecated Please use named exports
254-
* @privateRemarks
255-
* We want to someday deprecate the default export,
256-
* so none of the new TS types are being exported on the default
257-
* @public
258-
*/
259-
const BSON = {
260-
Binary,
261-
Code,
262-
DBRef,
263-
Decimal128,
264-
Double,
265-
Int32,
266-
Long,
267-
UUID,
268-
Map,
269-
MaxKey,
270-
MinKey,
271-
ObjectId,
272-
BSONRegExp,
273-
BSONSymbol,
274-
Timestamp,
275-
EJSON,
276-
setInternalBufferSize,
277-
serialize,
278-
serializeWithBufferAndIndex,
279-
deserialize,
280-
calculateObjectSize,
281-
deserializeStream,
282-
BSONError,
283-
BSONTypeError
284-
};
285-
export default BSON;

src/index.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import * as BSON from './bson';
2+
3+
// Export all named properties from BSON to support
4+
// import { ObjectId, serialize } from 'bson';
5+
// const { ObjectId, serialize } = require('bson');
6+
export * from './bson';
7+
8+
// Export BSON as a namespace to support:
9+
// import { BSON } from 'bson';
10+
// const { BSON } = require('bson');
11+
export { BSON };
12+
13+
// BSON does **NOT** have a default export
14+
15+
// The following will crash in es module environments
16+
// import BSON from 'bson';
17+
18+
// The following will work as expected, BSON as a namespace of all the APIs (BSON.ObjectId, BSON.serialize)
19+
// const BSON = require('bson');

0 commit comments

Comments
 (0)