Skip to content

Commit 30807bf

Browse files
committed
Fix checking for commonjs bundling
1 parent 82b0acb commit 30807bf

File tree

4 files changed

+24
-6
lines changed

4 files changed

+24
-6
lines changed

packages/node/rollup.config.mjs

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,33 @@
1+
const plugin = () => {
2+
return {
3+
name: 'mark-as-commonjs',
4+
resolveImportMeta: (property) => {
5+
if (property == 'isBundlingToCommonJs') {
6+
return 'true';
7+
}
8+
9+
return null;
10+
},
11+
};
12+
};
13+
114
export default [
215
{
316
input: 'lib/index.js',
17+
plugins: [plugin()],
418
output: {
519
file: 'dist/bundle.cjs',
6-
format: 'cjs'
20+
format: 'cjs',
21+
sourcemap: true
722
}
823
},
924
{
1025
input: 'lib/db/SqliteWorker.js',
26+
plugins: [plugin()],
1127
output: {
1228
file: 'dist/worker.cjs',
13-
format: 'cjs'
29+
format: 'cjs',
30+
sourcemap: true
1431
}
1532
}
1633
];

packages/node/src/db/BetterSQLite3DBAdapter.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,7 @@ export class BetterSQLite3DBAdapter extends BaseObserver<DBAdapterListener> impl
5757
}
5858

5959
const openWorker = async (isWriter: boolean) => {
60-
// https://nodejs.org/api/esm.html#differences-between-es-modules-and-commonjs
61-
const isCommonJsModule = '__filename' in global;
60+
const isCommonJsModule = import.meta.isBundlingToCommonJs ?? false;
6261
let worker: Worker;
6362
const workerName = isWriter ? `write ${dbFilePath}` : `read ${dbFilePath}`;
6463

packages/node/src/db/SqliteWorker.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,8 +98,7 @@ class BetterSqliteWorker implements AsyncDatabaseOpener {
9898
}
9999

100100
const loadExtension = (db: Database) => {
101-
// https://nodejs.org/api/esm.html#differences-between-es-modules-and-commonjs
102-
const isCommonJsModule = '__filename' in global;
101+
const isCommonJsModule = import.meta.isBundlingToCommonJs ?? false;
103102

104103
const platform = OS.platform();
105104
let extensionPath: string;

packages/node/src/node.d.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
interface ImportMeta {
2+
isBundlingToCommonJs?: boolean // This property is set by our rollup configuration
3+
}

0 commit comments

Comments
 (0)