DallasHoff / sqlocal

SQLocal makes it easy to run SQLite3 in the browser, backed by the origin private file system.
https://sqlocal.dallashoffman.com
MIT License
212 stars 8 forks source link

Bundling Error: Invalid URL #29

Closed muzhi1991 closed 1 month ago

muzhi1991 commented 1 month ago

When I use this project as a dependency for my plugin development (a plugin for Electron app), I need to package all the files into a main.js file. This is when calling this library will prompt Error

plugin:private-ai-plugin:45299 Uncaught (in promise) TypeError: Failed to construct 'URL': Invalid URL

It seems that a js file is referenced by the URL to start the worker. How to support this situation?

 this.worker = new Worker(new URL("./worker", import_meta3.url), {
      type: "module"
    });

How to deal with this situation? Can we add support for bundle into one file?

DallasHoff commented 1 month ago

./worker is the path written in the SQLocal code. Your bundler should parse this import and do the proper modifications to bundle it for you. It looks like your bundler is not configured correctly to do this if it is outputting the ./worker path with no modification, and that is why you are getting the "Invalid URL" error.

For example, in a project that I bundle using Vite, it detects the import and rewrites the path to ../workers/worker-BC_MBDWR.js. Any standard JavaScript bundling tool should be able to handle this for you.