Skip to content

Commit c59a5b3

Browse files
committed
Compile node package to CommonJS module
1 parent 525916a commit c59a5b3

File tree

5 files changed

+472
-913
lines changed

5 files changed

+472
-913
lines changed

packages/node/package.json

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,23 +6,25 @@
66
"access": "public"
77
},
88
"description": "PowerSync - sync Postgres with SQLite in your Node.js app for offline-first and real-time data",
9-
"main": "./lib/index.js",
10-
"module": "./lib/index.js",
11-
"types": "./lib/index.d.ts",
129
"files": [
1310
"lib",
1411
"dist",
1512
"download_core.js"
1613
],
1714
"scripts": {
1815
"install": "node download_core.js",
19-
"build": "tsc -b",
16+
"build": "tsc -b && rollup --config",
2017
"build:prod": "tsc -b --sourceMap false",
2118
"clean": "rm -rf lib dist tsconfig.tsbuildinfo dist",
2219
"watch": "tsc -b -w",
2320
"test": "vitest"
2421
},
2522
"type": "module",
23+
"exports": {
24+
"import": "./lib/index.js",
25+
"require": "./dist/bundle.cjs",
26+
"types": "./lib/index.d.ts"
27+
},
2628
"repository": {
2729
"type": "git",
2830
"url": "git+https://github.com/powersync-ja/powersync-js.git"
@@ -34,17 +36,18 @@
3436
},
3537
"homepage": "https://docs.powersync.com/",
3638
"peerDependencies": {
37-
"@powersync/common": "workspace:^1.22.0",
38-
"@powersync/better-sqlite3": "^0.1.0"
39+
"@powersync/common": "workspace:^1.22.0"
3940
},
4041
"dependencies": {
42+
"@powersync/better-sqlite3": "^0.1.0",
4143
"@powersync/common": "workspace:*",
4244
"async-lock": "^1.4.0",
4345
"bson": "^6.6.0",
4446
"comlink": "^4.4.2"
4547
},
4648
"devDependencies": {
4749
"@types/async-lock": "^1.4.0",
50+
"rollup": "4.14.3",
4851
"typescript": "^5.5.3",
4952
"vitest": "^3.0.5"
5053
},

packages/node/rollup.config.mjs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
const plugin = () => {
2+
return {
3+
name: 'mark-as-commonjs',
4+
transform: (code) => {
5+
return code.replace('const isCommonJsModule = false;', 'const isCommonJsModule = true;');
6+
},
7+
};
8+
};
9+
10+
export default [
11+
{
12+
input: 'lib/index.js',
13+
plugins: [plugin()],
14+
output: {
15+
file: 'dist/bundle.cjs',
16+
format: 'cjs',
17+
}
18+
},
19+
{
20+
input: 'lib/db/SqliteWorker.js',
21+
plugins: [plugin()],
22+
output: {
23+
file: 'dist/worker.cjs',
24+
format: 'cjs',
25+
}
26+
}
27+
];

packages/node/src/db/BetterSQLite3DBAdapter.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,16 @@ export class BetterSQLite3DBAdapter extends BaseObserver<DBAdapterListener> impl
5353
}
5454

5555
const openWorker = async (isWriter: boolean) => {
56-
const worker = new Worker(new URL('./SqliteWorker.js', import.meta.url), {name: isWriter ? `write ${dbFilePath}` : `read ${dbFilePath}`});
56+
const isCommonJsModule = false; // Replaced with true by rollup plugin
57+
let worker: Worker;
58+
const workerName = isWriter ? `write ${dbFilePath}` : `read ${dbFilePath}`;
59+
60+
if (isCommonJsModule) {
61+
worker = new Worker(path.resolve(__dirname, 'worker.cjs'), {name: workerName});
62+
} else {
63+
worker = new Worker(new URL('./SqliteWorker.js', import.meta.url), {name: workerName});
64+
}
65+
5766
const listeners = new WeakMap<EventListenerOrEventListenerObject, (e: any) => void>();
5867

5968
const comlink = Comlink.wrap<AsyncDatabaseOpener>({

packages/node/src/db/SqliteWorker.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import * as path from 'node:path';
12
import BetterSQLite3Database, { Database } from '@powersync/better-sqlite3';
23
import * as Comlink from 'comlink';
34
import { parentPort, threadId } from 'node:worker_threads';
@@ -97,6 +98,8 @@ class BetterSqliteWorker implements AsyncDatabaseOpener {
9798
}
9899

99100
const loadExtension = (db: Database) => {
101+
const isCommonJsModule = false; // Replaced with true by rollup plugin
102+
100103
const platform = OS.platform();
101104
let extensionPath: string;
102105
if (platform === 'win32') {
@@ -109,7 +112,13 @@ const loadExtension = (db: Database) => {
109112
throw 'Unknown platform, PowerSync for Node.js currently supports Windows, Linux and macOS.';
110113
}
111114

112-
const resolved = url.fileURLToPath(new URL(`../${extensionPath}`, import.meta.url));
115+
let resolved: string;
116+
if (isCommonJsModule) {
117+
resolved = path.resolve(__dirname, '../lib/', extensionPath);
118+
} else {
119+
resolved = url.fileURLToPath(new URL(`../${extensionPath}`, import.meta.url));
120+
}
121+
113122
db.loadExtension(resolved, 'sqlite3_powersync_init');
114123
};
115124

0 commit comments

Comments
 (0)