Skip to content

Commit 01bcba0

Browse files
committed
✨ feat: Configure Vite to produce a single, minified library output
1 parent 619a6eb commit 01bcba0

4 files changed

Lines changed: 63 additions & 29 deletions

File tree

README.md

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -52,10 +52,9 @@ await db.ready();
5252

5353
// 3. Use the table() API
5454
// The '$' symbol is automatically replaced by the table name ('users')
55-
const users = db.table('users');
5655

5756
// Create Table
58-
await users.exec(`
57+
await db.table('users').exec(`
5958
CREATE TABLE IF NOT EXISTS $ (
6059
id INTEGER PRIMARY KEY AUTOINCREMENT,
6160
name TEXT NOT NULL,
@@ -64,10 +63,12 @@ await users.exec(`
6463
`);
6564

6665
// Insert (Fully typed params are not enforced yet, but return types are)
67-
await users.run('INSERT INTO $ (name, email) VALUES (?, ?)', ['Alice', 'alice@dev.com']);
66+
await db
67+
.table('users')
68+
.run('INSERT INTO $ (name, email) VALUES (?, ?)', ['Alice', 'alice@dev.com']);
6869

6970
// Query (Returns User[])
70-
const allUsers = await users.query('SELECT * FROM $');
71+
const allUsers = await db.table('users').query('SELECT * FROM $');
7172
```
7273

7374
## Configuration
@@ -119,24 +120,34 @@ Key interfaces for better TypeScript integration.
119120

120121
```typescript
121122
interface SQLiteWASMConfig {
122-
filename: string;
123+
filename: string; // Required: Database file name
123124
vfs?: {
124-
type?: 'opfs' | 'opfs-sahpool' | 'memdb';
125+
type?: 'opfs' | 'opfs-sahpool' | 'memdb'; // Default: 'opfs'
126+
poolConfig?: {
127+
// Only used when type is 'opfs-sahpool'
128+
initialCapacity?: number; // Default: 3
129+
clearOnInit?: boolean; // Default: false
130+
name?: string; // Default: 'sqlite-wasm-pool'
131+
};
125132
};
126133
pragma?: {
127-
journal_mode?: 'DELETE' | 'TRUNCATE' | 'PERSIST' | 'MEMORY' | 'WAL' | 'OFF';
128-
synchronous?: 'OFF' | 'NORMAL' | 'FULL' | 'EXTRA';
129-
temp_store?: 'DEFAULT' | 'FILE' | 'MEMORY';
130-
foreign_keys?: 'ON' | 'OFF';
134+
journal_mode?: 'DELETE' | 'TRUNCATE' | 'PERSIST' | 'MEMORY' | 'WAL' | 'OFF'; // Default: 'WAL'
135+
synchronous?: 'OFF' | 'NORMAL' | 'FULL' | 'EXTRA'; // Default: 'NORMAL'
136+
temp_store?: 'DEFAULT' | 'FILE' | 'MEMORY'; // Default: 'MEMORY'
137+
foreign_keys?: 'ON' | 'OFF'; // Default: undefined (not set)
131138
};
132139
logging?: {
133-
filterSqlTrace?: boolean;
134-
print?: (message: string) => void;
135-
printErr?: (message: string) => void;
140+
filterSqlTrace?: boolean; // Default: true
141+
print?: (message: string) => void; // Default: console.log
142+
printErr?: (message: string) => void; // Default: console.error
136143
};
137144
}
138145
```
139146

147+
> **Note:** The `opfs` VFS requires your server to set COOP/COEP headers
148+
> (`Cross-Origin-Opener-Policy: same-origin` and `Cross-Origin-Embedder-Policy: require-corp`). If
149+
> these headers are not available, use `opfs-sahpool` instead, which works without them.
150+
140151
### `RunResult`
141152

142153
Returned by `run()` and `table().run()`.

package.json

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@haroonwaves/sqlite-wasm-easy",
3-
"version": "0.2.2",
3+
"version": "0.2.5",
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.js"
1613
}
1714
},
1815
"files": [

src/worker/sqliteWorker.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,15 @@ async function openDatabase(filename: string) {
7272
} else if (vfsType === 'memdb') {
7373
db = new sqlite3.oo1.DB(':memory:', 'c');
7474
} else {
75-
// Fallback to default database
75+
// Check if OpfsDb is available (requires COOP/COEP headers)
76+
if (typeof sqlite3.oo1.OpfsDb !== 'function') {
77+
throw new Error(
78+
'OPFS VFS is not available. This usually means your server is not configured with ' +
79+
'the required COOP/COEP headers (Cross-Origin-Opener-Policy: same-origin, ' +
80+
"Cross-Origin-Embedder-Policy: require-corp). Consider using 'opfs-sahpool' instead, " +
81+
'which does not require these headers.'
82+
);
83+
}
7684
db = new sqlite3.oo1.OpfsDb(filename, 'c');
7785
}
7886

vite.config.ts

Lines changed: 29 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,51 @@
1-
import { defineConfig } from 'vite';
1+
import { defineConfig, type Plugin } from 'vite';
22
import { resolve } from 'path';
33
import { fileURLToPath } from 'url';
4+
import { rmSync } from 'fs';
45
import dts from 'vite-plugin-dts';
56

67
const __dirname = fileURLToPath(new URL('.', import.meta.url));
78

9+
// Plugin to clean up extra assets generated by @sqlite.org/sqlite-wasm
10+
function cleanupAssets(): Plugin {
11+
return {
12+
name: 'cleanup-assets',
13+
closeBundle() {
14+
// Remove the assets folder containing duplicate SQLite workers
15+
// Everything is inlined via ?worker&inline, so these aren't needed
16+
try {
17+
rmSync(resolve(__dirname, 'dist/assets'), { recursive: true, force: true });
18+
} catch {
19+
// Ignore if folder doesn't exist
20+
}
21+
},
22+
};
23+
}
24+
825
export default defineConfig({
926
root: '.', // Root directory for dev server
10-
plugins: [dts({ rollupTypes: true })],
27+
plugins: [dts({ rollupTypes: true }), cleanupAssets()],
1128
build: {
1229
lib: {
13-
entry: {
14-
index: resolve(__dirname, 'src/index.ts'),
15-
worker: resolve(__dirname, 'src/worker/sqliteWorker.ts'),
16-
},
30+
// Only export the main entry - worker is inlined via ?worker&inline
31+
entry: resolve(__dirname, 'src/index.ts'),
1732
formats: ['es'],
18-
fileName: (format, entryName) => `${entryName}.js`,
33+
fileName: () => 'index.js',
1934
},
2035
rollupOptions: {
21-
// Don't mark sqlite-wasm as external - bundle it into the worker
22-
// so the inlined worker is self-contained and works offline
2336
output: {
24-
preserveModules: false,
37+
// Prevent code splitting - everything in one file
38+
manualChunks: undefined,
39+
inlineDynamicImports: true,
2540
},
2641
},
2742
target: 'es2022',
2843
outDir: 'dist',
2944
emptyOutDir: true,
30-
sourcemap: true,
45+
// Disable source maps for smaller production builds
46+
sourcemap: false,
47+
// Minify for smaller output
48+
minify: 'esbuild',
3149
},
3250
server: {
3351
open: '/examples/basic.html', // Auto-open examples on dev server start

0 commit comments

Comments
 (0)