Skip to content

Commit 2a503d1

Browse files
authored
docs(NODE-4901): update guide about bundle changes (#544)
1 parent 28ce4d5 commit 2a503d1

File tree

2 files changed

+52
-92
lines changed

2 files changed

+52
-92
lines changed

README.md

+26-92
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
11
# BSON parser
22

3-
BSON is short for "Binary JSON," and is the binary-encoded serialization of JSON-like documents. You can learn more about it in [the specification](http://bsonspec.org).
4-
5-
This browser version of the BSON parser is compiled using [rollup](https://rollupjs.org/) and the current version is pre-compiled in the `dist` directory.
6-
7-
This is the default BSON parser, however, there is a C++ Node.js addon version as well that does not support the browser. It can be found at [mongod-js/bson-ext](https://github.com/mongodb-js/bson-ext).
3+
BSON is short for "Binary JSON," and is the binary-encoded serialization of JSON-like documents.
4+
You can learn more about it in [the specification](http://bsonspec.org).
85

96
### Table of Contents
107
- [Usage](#usage)
@@ -32,106 +29,43 @@ npm install
3229
npm run build
3330
```
3431

35-
### Node (no bundling)
36-
A simple example of how to use BSON in `Node.js`:
32+
### Node.js or Bundling Usage
3733

38-
```js
39-
const BSON = require('bson');
40-
const Long = BSON.Long;
34+
When using a bundler or Node.js you can import bson using the package name:
4135

42-
// Serialize a document
43-
const doc = { long: Long.fromNumber(100) };
44-
const data = BSON.serialize(doc);
45-
console.log('data:', data);
36+
```js
37+
import { BSON, EJSON, ObjectId } from 'bson';
38+
// or:
39+
// const { BSON, EJSON, ObjectId } = require('bson');
4640

47-
// Deserialize the resulting Buffer
48-
const doc_2 = BSON.deserialize(data);
49-
console.log('doc_2:', doc_2);
41+
const bytes = BSON.serialize({ _id: new ObjectId() });
42+
console.log(bytes);
43+
const doc = BSON.deserialize(bytes);
44+
console.log(EJSON.stringify(doc));
45+
// {"_id":{"$oid":"..."}}
5046
```
5147

52-
### Browser (no bundling)
48+
### Browser Usage
5349

54-
If you are not using a bundler like webpack, you can include `dist/bson.bundle.js` using a script tag. It includes polyfills for built-in node types like `Buffer`.
50+
If you are working directly in the browser without a bundler please use the `.mjs` bundle like so:
5551

5652
```html
57-
<script src="./dist/bson.bundle.js"></script>
58-
59-
<script>
60-
function start() {
61-
// Get the Long type
62-
const Long = BSON.Long;
63-
64-
// Serialize a document
65-
const doc = { long: Long.fromNumber(100) }
66-
const data = BSON.serialize(doc);
67-
console.log('data:', data);
68-
69-
// De serialize it again
70-
const doc_2 = BSON.deserialize(data);
71-
console.log('doc_2:', doc_2);
72-
}
53+
<script type="module">
54+
import { BSON, EJSON, ObjectId } from './lib/bson.mjs';
55+
56+
const bytes = BSON.serialize({ _id: new ObjectId() });
57+
console.log(bytes);
58+
const doc = BSON.deserialize(bytes);
59+
console.log(EJSON.stringify(doc));
60+
// {"_id":{"$oid":"..."}}
7361
</script>
7462
```
7563

76-
### Using webpack
77-
78-
If using webpack, you can use your normal import/require syntax of your project to pull in the `bson` library.
79-
80-
ES6 Example:
81-
82-
```js
83-
import { Long, serialize, deserialize } from 'bson';
84-
85-
// Serialize a document
86-
const doc = { long: Long.fromNumber(100) };
87-
const data = serialize(doc);
88-
console.log('data:', data);
89-
90-
// De serialize it again
91-
const doc_2 = deserialize(data);
92-
console.log('doc_2:', doc_2);
93-
```
94-
95-
ES5 Example:
96-
97-
```js
98-
const BSON = require('bson');
99-
const Long = BSON.Long;
100-
101-
// Serialize a document
102-
const doc = { long: Long.fromNumber(100) };
103-
const data = BSON.serialize(doc);
104-
console.log('data:', data);
105-
106-
// Deserialize the resulting Buffer
107-
const doc_2 = BSON.deserialize(data);
108-
console.log('doc_2:', doc_2);
109-
```
110-
111-
Depending on your settings, webpack will under the hood resolve to one of the following:
112-
113-
- `dist/bson.browser.esm.js` If your project is in the browser and using ES6 modules (Default for `webworker` and `web` targets)
114-
- `dist/bson.browser.umd.js` If your project is in the browser and not using ES6 modules
115-
- `dist/bson.esm.js` If your project is in Node.js and using ES6 modules (Default for `node` targets)
116-
- `lib/bson.js` (the normal include path) If your project is in Node.js and not using ES6 modules
117-
118-
For more information, see [this page on webpack's `resolve.mainFields`](https://webpack.js.org/configuration/resolve/#resolvemainfields) and [the `package.json` for this project](./package.json#L52)
119-
120-
### Usage with Angular
121-
122-
Starting with Angular 6, Angular CLI removed the shim for `global` and other node built-in variables (original comment [here](https://github.com/angular/angular-cli/issues/9827#issuecomment-386154063)). If you are using BSON with Angular, you may need to add the following shim to your `polyfills.ts` file:
123-
124-
```js
125-
// In polyfills.ts
126-
(window as any).global = window;
127-
```
128-
129-
- [Original Comment by Angular CLI](https://github.com/angular/angular-cli/issues/9827#issuecomment-386154063)
130-
- [Original Source for Solution](https://stackoverflow.com/a/50488337/4930088)
131-
13264
## Installation
13365

134-
`npm install bson`
66+
```sh
67+
npm install bson
68+
```
13569

13670
## Documentation
13771

docs/upgrade-to-v5.md

+26
Original file line numberDiff line numberDiff line change
@@ -238,3 +238,29 @@ const result = BSON.serialize(Object.fromEntries([1, true, 'blue'].entries()))
238238
BSON.deserialize(result)
239239
// { '0': 1, '1': true, '2': 'blue' }
240240
```
241+
242+
### Exports and available bundles
243+
244+
Most users should be unaffected by these changes, Node.js `require()` / Node.js `import` will fetch the corresponding BSON library as expected.
245+
And for folks using bundlers like, webpack or rollup a tree shakable es module bundle will be pulled in because of the settings in our package.json.
246+
247+
Our package.json defines the following `"exports"` settings.
248+
```json
249+
{
250+
"main": "./lib/bson.cjs",
251+
"module": "./lib/bson.mjs",
252+
"browser": "./lib/bson.mjs",
253+
"exports": {
254+
"browser": "./lib/bson.mjs",
255+
"import": "./lib/bson.mjs",
256+
"require": "./lib/bson.cjs"
257+
}
258+
}
259+
```
260+
261+
You can now find compiled bundles of the BSON library in 3 common formats in the `lib` directory.
262+
263+
- CommonJS - `lib/bson.cjs`
264+
- ES Module - `lib/bson.mjs`
265+
- Immediate Invoked Function Expression (IIFE) - `lib/bson.bundle.js`
266+
- Typically used when trying to import JS on the web CDN style, but the ES Module (`.mjs`) bundle is fully browser compatible and should be preferred if it works in your use case.

0 commit comments

Comments
 (0)