jlongster / absurd-sql

sqlite3 in ur indexeddb (hopefully a better backend soon)
MIT License
4.15k stars 101 forks source link

"Unexpected token 'export'" when installing #76

Open jeroenvanrensen opened 3 months ago

jeroenvanrensen commented 3 months ago

Hello,

When installing absurd-sql in a new Sveltekit project, I get the following error: Unexpected token 'export' from /node_modules/absurd-sql/dist/indexeddb-main-thread.js:66: export { initBackend };.

I hope anyone can help me, thanks in advance!

Steps to reproduce

  1. Create a new Sveltekit project: npm create svelte@latest my-app
  2. Install dependencies and start dev server
  3. Install @jlongster/sql.js and absurd-sql via NPM
  4. Update /src/routes/+page.svelte to:

    
    <script>
    import { initBackend } from 'absurd-sql/dist/indexeddb-main-thread'
    import { onMount } from 'svelte'
    
    onMount(() => {
        function init() {
            let worker = new Worker(new URL('./index.worker.js', import.meta.url))
            // This is only required because Safari doesn't support nested
            // workers. This installs a handler that will proxy creating web
            // workers through the main thread
            initBackend(worker)
        }
    
        init()
    })
    </script>

Welcome to SvelteKit

Visit kit.svelte.dev to read the documentation

5. Create a file `src/routes/index.worker.js` with the following code:
```js
import initSqlJs from '@jlongster/sql.js';
import { SQLiteFS } from 'absurd-sql';
import IndexedDBBackend from 'absurd-sql/dist/indexeddb-backend';

async function run() {
  let SQL = await initSqlJs({ locateFile: file => file });
  let sqlFS = new SQLiteFS(SQL.FS, new IndexedDBBackend());
  SQL.register_for_idb(sqlFS);

  SQL.FS.mkdir('/sql');
  SQL.FS.mount(sqlFS, {}, '/sql');

  const path = '/sql/db.sqlite';
  if (typeof SharedArrayBuffer === 'undefined') {
    let stream = SQL.FS.open(path, 'a+');
    await stream.node.contents.readIfFallback();
    SQL.FS.close(stream);
  }

  let db = new SQL.Database(path, { filename: true });
  // You might want to try `PRAGMA page_size=8192;` too!
  db.exec(`
    PRAGMA journal_mode=MEMORY;
  `);

   // Your code
}
  1. Open the browser

In the browser, I get the error message.