Skip to content

Commit b67649f

Browse files
committed
✨ feat: Inline SQLite worker source code into the main bundle
1 parent a28df7c commit b67649f

5 files changed

Lines changed: 35 additions & 14 deletions

File tree

package.json

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@haroonwaves/sqlite-wasm-easy",
3-
"version": "0.1.9",
3+
"version": "0.2.0",
44
"description": "A simple, zero-config wrapper around @sqlite.org/sqlite-wasm",
55
"type": "module",
66
"main": "./dist/index.js",
@@ -10,9 +10,6 @@
1010
".": {
1111
"import": "./dist/index.js",
1212
"types": "./dist/index.d.ts"
13-
},
14-
"./worker": {
15-
"import": "./dist/worker/sqliteWorker.js"
1613
}
1714
},
1815
"files": [
@@ -51,12 +48,11 @@
5148
],
5249
"author": "haroonwaves",
5350
"license": "MIT",
54-
"peerDependencies": {
51+
"dependencies": {
5552
"@sqlite.org/sqlite-wasm": "^3.51.2-build5"
5653
},
5754
"devDependencies": {
5855
"@eslint/js": "^9.39.2",
59-
"@sqlite.org/sqlite-wasm": "^3.51.2-build5",
6056
"@types/node": "^25.0.10",
6157
"cspell": "^9.6.0",
6258
"eslint": "^9.39.2",

pnpm-lock.yaml

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

src/core/database.ts

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,17 @@ import type {
77
WorkerResponse,
88
} from '../types/index';
99
import { mergeConfig } from './defaults';
10+
import { SQLITE_WORKER_SOURCE } from '../worker/sqliteWorker.inline';
11+
12+
function createWorker(): Worker {
13+
const blob = new Blob([SQLITE_WORKER_SOURCE], {
14+
type: 'text/javascript',
15+
});
16+
17+
const url = URL.createObjectURL(blob);
18+
19+
return new Worker(url, { type: 'module' });
20+
}
1021

1122
/**
1223
* Main SQLiteWASM class - provides a simple interface to SQLite WASM
@@ -40,9 +51,7 @@ export class SQLiteWASM<Schema = any> {
4051

4152
private async initialize(): Promise<void> {
4253
// Create worker
43-
const workerPath =
44-
this.config.worker.path || new URL('./worker/sqliteWorker.js', document.baseURI).href;
45-
this.worker = new Worker(workerPath, { type: 'module' });
54+
this.worker = createWorker();
4655

4756
// Setup message handler
4857
this.worker.addEventListener('message', this.handleWorkerMessage.bind(this));

src/worker/sqliteWorker.inline.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
// This file will be replaced at build time
2+
export const SQLITE_WORKER_SOURCE = '';

vite.config.ts

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,30 @@ import { defineConfig } from 'vite';
22
import { resolve } from 'path';
33
import { fileURLToPath } from 'url';
44
import dts from 'vite-plugin-dts';
5+
import fs from 'fs';
6+
import path from 'path';
57

68
const __dirname = fileURLToPath(new URL('.', import.meta.url));
79

810
export default defineConfig({
911
root: '.', // Root directory for dev server
10-
plugins: [dts({ rollupTypes: true })],
12+
plugins: [
13+
dts({ rollupTypes: true }),
14+
{
15+
name: 'inline-sqlite-worker',
16+
buildStart() {
17+
const workerPath = path.resolve(__dirname, 'src/worker/sqliteWorker.ts');
18+
const code = fs.readFileSync(workerPath, 'utf-8');
19+
const output = `export const SQLITE_WORKER_SOURCE = ${JSON.stringify(code)};`;
20+
21+
fs.writeFileSync(path.resolve(__dirname, 'src/worker/sqliteWorker.inline.ts'), output);
22+
},
23+
},
24+
],
1125
build: {
1226
lib: {
1327
entry: {
1428
index: resolve(__dirname, 'src/index.ts'),
15-
'worker/sqliteWorker': resolve(__dirname, 'src/worker/sqliteWorker.ts'),
1629
},
1730
formats: ['es'],
1831
fileName: (format, entryName) => `${entryName}.js`,

0 commit comments

Comments
 (0)