Skip to content

Commit 7913cff

Browse files
authored
Merge pull request #32 from sonicbaume/sqljs-remove-nodefs-dependency
2 parents 2e42aa1 + 3f704d3 commit 7913cff

9 files changed

Lines changed: 486 additions & 153 deletions

File tree

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,9 @@ Browser-safe entry that avoids Node-only dependencies. It expects `Buffer`,
3131
`Uint8Array`, or `ArrayBuffer` inputs rather than file paths.
3232

3333
SQLite-backed formats (Snap `.sps/.spb` and TouchChat `.ce`) require a WASM
34-
SQLite engine. Configure `sql.js` in your bundler before loading those formats:
34+
SQLite engine. To support these, configure `sql.js` in your bundler
35+
and either include `<script src="sql-wasm.js"></script>` in your HTML, or
36+
`window.initSqlJs = require('sql.js');` in your app.
3537

3638
```ts
3739
import { configureSqlJs, SnapProcessor } from '@willwade/aac-processors/browser';

examples/vitedemo/index.html

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -574,7 +574,6 @@ <h1>🎯 AAC Processors Browser Demo</h1>
574574
</div>
575575
</div>
576576
</div>
577-
578577
<script type="module" src="/src/main.ts"></script>
579578
</body>
580579
</html>

examples/vitedemo/package-lock.json

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

examples/vitedemo/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@
1010
},
1111
"devDependencies": {
1212
"typescript": "^5.6.3",
13-
"vite": "^6.0.1"
13+
"vite": "^7.3.1",
14+
"vite-plugin-commonjs": "^0.10.4"
1415
},
1516
"dependencies": {
1617
"@willwade/aac-processors": "file:../..",

examples/vitedemo/src/main.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
* It tests all browser-compatible processors with real file uploads.
66
*/
77

8+
window.initSqlJs = require('sql.js');
9+
810
// Polyfill Buffer for browser environment
911
if (typeof (window as any).Buffer === 'undefined') {
1012
// Create a proper Buffer wrapper class that extends Uint8Array
@@ -80,8 +82,9 @@ import {
8082
AACTree,
8183
AACPage,
8284
AACButton
83-
} from 'aac-processors';
84-
import { validateFileOrBuffer, type ValidationResult } from 'aac-processors/validation';
85+
} from '@willwade/aac-processors/browser';
86+
import { validateFileOrBuffer, type ValidationResult } from '@willwade/aac-processors/validation';
87+
import initSqlJs from 'sql.js';
8588

8689
import sqlWasmUrl from 'sql.js/dist/sql-wasm.wasm?url';
8790

examples/vitedemo/vite.config.ts

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,13 @@
11
import { defineConfig } from 'vite';
22
import path from 'path';
3+
import commonjs from 'vite-plugin-commonjs';
34

45
export default defineConfig({
6+
plugins: [
7+
commonjs()
8+
],
59
resolve: {
610
alias: [
7-
{
8-
find: /^aac-processors\/validation$/,
9-
replacement: path.resolve(__dirname, '../../src/validation.ts'),
10-
},
11-
{
12-
find: /^aac-processors$/,
13-
replacement: path.resolve(__dirname, '../../src/index.browser.ts'),
14-
},
1511
{
1612
find: /^stream$/,
1713
replacement: path.resolve(__dirname, 'node_modules/stream-browserify'),
@@ -31,8 +27,7 @@ export default defineConfig({
3127
],
3228
},
3329
optimizeDeps: {
34-
exclude: ['aac-processors'],
35-
include: []
30+
include: ['@willwade/aac-processors']
3631
},
3732
define: {
3833
'process.env': '{}',
@@ -51,7 +46,8 @@ export default defineConfig({
5146
sourcemap: true,
5247
commonjsOptions: {
5348
// Ignore Node.js built-in modules
54-
ignore: ['crypto', 'stream', 'timers', 'events', 'fs', 'path', 'os']
49+
ignore: ['crypto', 'stream', 'timers', 'events', 'fs', 'path', 'os'],
50+
include: [/@willwade\/aac-processors/, /node_modules/]
5551
}
5652
}
5753
});

package-lock.json

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

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@
181181
"fast-xml-parser": "^5.2.0",
182182
"jszip": "^3.10.1",
183183
"plist": "^3.1.0",
184-
"sql.js": "^1.13.0",
184+
"sql.js": "^1.14.1",
185185
"xml2js": "^0.6.2",
186186
"yauzl": "^3.2.0"
187187
},

src/utils/sqlite.ts

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { SqlJsConfig, SqlJsStatic } from 'sql.js';
1+
import type { SqlJsConfig, SqlJsStatic, InitSqlJsStatic } from 'sql.js';
22
import { defaultFileAdapter, FileAdapter, getNodeRequire, isNodeRuntime } from './io';
33

44
export interface SqliteStatementAdapter {
@@ -30,12 +30,14 @@ export function configureSqlJs(config: SqlJsConfig): void {
3030
sqlJsConfig = { ...(sqlJsConfig ?? {}), ...config };
3131
}
3232

33-
async function getSqlJs(): Promise<SqlJsStatic> {
33+
async function getSqlJsBrowser(): Promise<SqlJsStatic> {
3434
if (!sqlJsPromise) {
35-
sqlJsPromise = import('sql.js').then((module) => {
36-
const initSqlJs = module.default || module;
37-
return initSqlJs(sqlJsConfig ?? {});
38-
});
35+
const isBrowser = typeof globalThis !== 'undefined' && (globalThis as any).window !== undefined;
36+
if (!isBrowser) throw new Error('Must be run in a browser');
37+
const window = (globalThis as any).window;
38+
if (!('initSqlJs' in window)) throw new Error('Need to add sql-wasm.js script element to DOM');
39+
const initSqlJs = window.initSqlJs as InitSqlJsStatic;
40+
sqlJsPromise = initSqlJs(sqlJsConfig ?? {});
3941
}
4042
return sqlJsPromise;
4143
}
@@ -120,7 +122,7 @@ export async function openSqliteDatabase(
120122
const data = await readBinaryFromInput(input);
121123

122124
if (!isNodeRuntime()) {
123-
const SQL = await getSqlJs();
125+
const SQL = await getSqlJsBrowser();
124126
const db = new SQL.Database(data);
125127
return { db: createSqlJsAdapter(db) };
126128
}

0 commit comments

Comments
 (0)