Skip to content

Commit cc25714

Browse files
committed
chore: require target
1 parent 0865dbb commit cc25714

File tree

2 files changed

+8
-4
lines changed

2 files changed

+8
-4
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(),

rollup.config.mjs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ const config = [
5757
input,
5858
plugins: [
5959
typescript(tsConfig),
60-
requireRewriter({ isBrowser: true }),
60+
requireRewriter({ target: 'browser' }),
6161
nodeResolve({ resolveOnly: [] })
6262
],
6363
output: {
@@ -68,7 +68,7 @@ const config = [
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',

0 commit comments

Comments
 (0)