Skip to content

Commit 7f2a6d3

Browse files
authored
test(NODE-6920): esm bundles do not have top-level await (#790)
1 parent 4602973 commit 7f2a6d3

File tree

5 files changed

+46
-10
lines changed

5 files changed

+46
-10
lines changed

etc/rollup/rollup-plugin-require-rewriter/require_rewriter.mjs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import assert from 'node:assert/strict';
12
import MagicString from 'magic-string';
23

34
const CRYPTO_IMPORT_ESM_SRC = `import { randomBytes as nodejsRandomBytes } from 'crypto';`;
@@ -11,7 +12,10 @@ const CODE_TO_REPLACE = `const nodejsRandomBytes = (() => {
1112
}
1213
})();`;
1314

14-
export function requireRewriter({ isBrowser = false } = {}) {
15+
/** @param {{ target: 'browser' | 'node'}} configuration - destination information that changes the replacement syntax used. */
16+
export function requireRewriter({ target }) {
17+
assert.match(target, /^(node|browser)$/, 'target must be either "node" or "browser"');
18+
1519
return {
1620
/**
1721
* Take the compiled source code input; types are expected to already have been removed
@@ -35,7 +39,7 @@ export function requireRewriter({ isBrowser = false } = {}) {
3539

3640
// MagicString lets us edit the source code and still generate an accurate source map
3741
const magicString = new MagicString(code);
38-
magicString.overwrite(start, end, isBrowser ? BROWSER_ESM_SRC : CRYPTO_IMPORT_ESM_SRC);
42+
magicString.overwrite(start, end, target === 'browser' ? BROWSER_ESM_SRC : CRYPTO_IMPORT_ESM_SRC);
3943

4044
return {
4145
code: magicString.toString(),

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@
7979
"exports": {
8080
"browser": {
8181
"types": "./bson.d.ts",
82-
"default": "./lib/bson.browser.mjs"
82+
"default": "./lib/bson.mjs"
8383
},
8484
"react-native": "./lib/bson.rn.cjs",
8585
"default": {

rollup.config.mjs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,18 +57,18 @@ const config = [
5757
input,
5858
plugins: [
5959
typescript(tsConfig),
60-
requireRewriter({ isBrowser: true }),
60+
requireRewriter({ target: 'browser' }),
6161
nodeResolve({ resolveOnly: [] })
6262
],
6363
output: {
64-
file: 'lib/bson.browser.mjs',
64+
file: 'lib/bson.mjs',
6565
format: 'esm',
6666
sourcemap: true
6767
}
6868
},
6969
{
7070
input,
71-
plugins: [typescript(tsConfig), requireRewriter(), nodeResolve({ resolveOnly: [] })],
71+
plugins: [typescript(tsConfig), requireRewriter({ target: 'node' }), nodeResolve({ resolveOnly: [] })],
7272
output: {
7373
file: 'lib/bson.node.mjs',
7474
format: 'esm',

test/node/exports.test.ts

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import * as BSON from '../register-bson';
33
import { sorted, byStrings } from './tools/utils';
44
import { readFile } from 'fs/promises';
55
import { resolve } from 'path';
6+
import * as child_process from 'node:child_process';
67

78
const EXPECTED_EXPORTS = [
89
// This is our added web indicator not a real export but a small exception for this test.
@@ -41,6 +42,7 @@ const EXPECTED_EXPORTS = [
4142
];
4243

4344
const EXPECTED_EJSON_EXPORTS = ['parse', 'stringify', 'serialize', 'deserialize'];
45+
const NODE_MAJOR = Number(process.versions.node.split('.')[0]);
4446

4547
describe('bson entrypoint', () => {
4648
it('should export all and only the expected keys in expected_exports', () => {
@@ -96,4 +98,32 @@ describe('bson entrypoint', () => {
9698
expect(pkg).nested.property('exports.default.types', './bson.d.ts');
9799
});
98100
});
101+
102+
function testSyncESMImport(name, module) {
103+
return () => {
104+
const child = child_process.spawnSync(
105+
'node',
106+
['--experimental-print-required-tla', '--print', `require('${module}')`],
107+
{ encoding: 'utf-8' }
108+
);
109+
110+
expect(
111+
child.status,
112+
`expected to be able to 'require' to import the ${name} ESM because there should be no top-level await:\n` +
113+
child.stderr
114+
).to.equal(0);
115+
};
116+
}
117+
118+
const itFn = NODE_MAJOR < 22 ? it.skip : it;
119+
120+
itFn(
121+
'browser bundle does not use top-level await',
122+
testSyncESMImport('browser', './lib/bson.mjs')
123+
);
124+
125+
itFn(
126+
'node bundle does not use top-level await',
127+
testSyncESMImport('node', './lib/bson.node.mjs')
128+
);
99129
});

test/node/release.test.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,16 @@ const REQUIRED_FILES = [
1111
'README.md',
1212
'bson.d.ts',
1313
'etc/prepare.js',
14-
'lib/bson.bundle.js',
1514
'lib/bson.bundle.js.map',
16-
'lib/bson.cjs',
15+
'lib/bson.bundle.js',
1716
'lib/bson.cjs.map',
18-
'lib/bson.mjs',
17+
'lib/bson.cjs',
1918
'lib/bson.mjs.map',
20-
'lib/bson.rn.cjs',
19+
'lib/bson.mjs',
20+
'lib/bson.node.mjs.map',
21+
'lib/bson.node.mjs',
2122
'lib/bson.rn.cjs.map',
23+
'lib/bson.rn.cjs',
2224
'package.json',
2325
'src/binary.ts',
2426
'src/bson_value.ts',

0 commit comments

Comments
 (0)