Skip to content

Commit cce71f5

Browse files
committed
Reduce bundling code
1 parent 072e3cd commit cce71f5

File tree

8 files changed

+92
-56
lines changed

8 files changed

+92
-56
lines changed

packages/common/package.json

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,16 @@
1212
"types": "lib/index.d.ts",
1313
"exports": {
1414
".": {
15+
"node": {
16+
"import": {
17+
"types": "./lib/index.d.ts",
18+
"default": "./dist/bundle.node.mjs"
19+
},
20+
"require": {
21+
"types": "./dist/index.d.cts",
22+
"require": "./dist/bundle.node.cjs"
23+
}
24+
},
1525
"import": {
1626
"types": "./lib/index.d.ts",
1727
"default": "./dist/bundle.mjs"
@@ -47,19 +57,19 @@
4757
"async-mutex": "^0.5.0",
4858
"bson": "^6.10.4",
4959
"buffer": "^6.0.3",
50-
"event-iterator": "^2.0.0",
51-
"rsocket-core": "1.0.0-alpha.3",
52-
"rsocket-websocket-client": "1.0.0-alpha.3"
60+
"event-iterator": "^2.0.0"
5361
},
5462
"devDependencies": {
5563
"@rollup/plugin-commonjs": "^29.0.0",
64+
"@rollup/plugin-inject": "^5.0.5",
5665
"@rollup/plugin-json": "^6.1.0",
5766
"@rollup/plugin-node-resolve": "15.2.3",
5867
"@types/node": "^20.5.9",
5968
"@types/uuid": "^9.0.1",
6069
"rollup": "^4.52.5",
61-
"rollup-plugin-node-externals": "^8.1.1",
6270
"cross-fetch": "^4.0.0",
63-
"js-logger": "^1.6.1"
71+
"js-logger": "^1.6.1",
72+
"rsocket-core": "1.0.0-alpha.3",
73+
"rsocket-websocket-client": "1.0.0-alpha.3"
6474
}
6575
}

packages/common/rollup.config.mjs

Lines changed: 37 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,49 @@
1+
import * as path from 'node:path';
2+
13
import commonjs from '@rollup/plugin-commonjs';
4+
import inject from '@rollup/plugin-inject';
25
import json from '@rollup/plugin-json';
36
import nodeResolve from '@rollup/plugin-node-resolve';
4-
import { nodeExternals } from 'rollup-plugin-node-externals';
57
import { dts } from 'rollup-plugin-dts';
68

9+
function defineBuild(isNode) {
10+
const suffix = isNode ? '.node' : '';
11+
12+
return {
13+
input: 'lib/index.js',
14+
output: [
15+
{
16+
file: `dist/bundle${suffix}.mjs`,
17+
format: 'esm',
18+
sourcemap: true
19+
},
20+
{
21+
file: `dist/bundle${suffix}.cjs`,
22+
format: 'cjs',
23+
sourcemap: true
24+
}
25+
],
26+
plugins: [
27+
[
28+
json(),
29+
nodeResolve({ preferBuiltins: false, browser: true }),
30+
commonjs({}),
31+
inject({
32+
Buffer: isNode ? 'node:buffer' : path.resolve('lib/buffer.js')
33+
})
34+
]
35+
],
36+
external: ['async-mutex', 'bson', 'buffer/', 'event-iterator']
37+
};
38+
}
39+
740
/**
841
* @returns {import('rollup').RollupOptions}
942
*/
10-
export default (commandLineArgs) => {
43+
export default () => {
1144
return [
12-
{
13-
input: 'lib/index.js',
14-
output: [
15-
{
16-
file: 'dist/bundle.mjs',
17-
format: 'esm',
18-
sourcemap: true
19-
},
20-
{
21-
file: 'dist/bundle.cjs',
22-
format: 'cjs',
23-
sourcemap: true
24-
}
25-
],
26-
plugins: [json(), nodeResolve({ preferBuiltins: false, browser: true }), commonjs({}), nodeExternals()]
27-
},
45+
defineBuild(false),
46+
defineBuild(true),
2847
{
2948
input: './lib/index.d.ts',
3049
output: [{ file: 'dist/index.d.cts', format: 'cjs' }],

packages/common/src/buffer.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import { Buffer } from 'buffer/';
2+
3+
export default Buffer;

packages/common/src/client/sync/stream/AbstractRemote.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import type { BSON } from 'bson';
2-
import { Buffer } from 'buffer';
32
import { type fetch } from 'cross-fetch';
43
import Logger, { ILogger } from 'js-logger';
54
import { RSocket, RSocketConnector, Requestable } from 'rsocket-core';
@@ -263,6 +262,14 @@ export abstract class AbstractRemote {
263262
*/
264263
abstract getBSON(): Promise<BSONImplementation>;
265264

265+
/**
266+
* @returns A text decoder decoding UTF-8. This is a method to allow patching it for Hermes which doesn't support the
267+
* builtin, without forcing us to bundle a polyfill with `@powersync/common`.
268+
*/
269+
protected createTextDecoder(): TextDecoder {
270+
return new TextDecoder();
271+
}
272+
266273
protected createSocket(url: string): WebSocket {
267274
return new WebSocket(url);
268275
}
@@ -564,7 +571,7 @@ export abstract class AbstractRemote {
564571
closeReader();
565572
});
566573

567-
const decoder = new TextDecoder();
574+
const decoder = this.createTextDecoder();
568575
let buffer = '';
569576

570577
const stream = new DataStream<T, string>({

packages/react-native/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@
5757
"@rollup/plugin-terser": "^0.4.4",
5858
"@types/async-lock": "^1.4.0",
5959
"async-lock": "^1.4.0",
60-
"bson": "^6.6.0",
60+
"bson": "^6.10.4",
6161
"react": "18.3.1",
6262
"react-native": "0.72.4",
6363
"react-native-fetch-api": "^3.0.0",

packages/react-native/src/sync/stream/ReactNativeRemote.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import {
1414
import { Platform } from 'react-native';
1515
// Note docs for React Native https://github.com/mongodb/js-bson?tab=readme-ov-file#react-native
1616
import { BSON } from 'bson';
17+
import { TextDecoder } from 'text-encoding';
1718

1819
import { fetch } from 'react-native-fetch-api';
1920

@@ -55,6 +56,10 @@ export class ReactNativeRemote extends AbstractRemote {
5556
return BSON;
5657
}
5758

59+
protected createTextDecoder(): TextDecoder {
60+
return new TextDecoder();
61+
}
62+
5863
async postStreamRaw<T>(options: SyncStreamOptions, mapLine: (line: string) => T): Promise<DataStream<T>> {
5964
const timeout =
6065
Platform.OS == 'android'

packages/web/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@
6666
"dependencies": {
6767
"@powersync/common": "workspace:*",
6868
"async-mutex": "^0.4.0",
69-
"bson": "^6.6.0",
69+
"bson": "^6.10.4",
7070
"comlink": "^4.4.2",
7171
"commander": "^12.1.0"
7272
},

pnpm-lock.yaml

Lines changed: 21 additions & 29 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)