mirror of
https://github.com/aljazceru/turso.git
synced 2026-01-08 18:54:21 +01:00
bundle browser packages too in order to easily consume them without bundlers
This commit is contained in:
24
bindings/javascript/packages/browser/index-bundle.ts
Normal file
24
bindings/javascript/packages/browser/index-bundle.ts
Normal file
@@ -0,0 +1,24 @@
|
||||
import { setupMainThread } from "@tursodatabase/database-browser-common";
|
||||
//@ts-ignore
|
||||
import TursoWorker from "./worker.js?worker&inline";
|
||||
|
||||
const __wasmUrl = new URL('./turso.wasm32-wasi.wasm', import.meta.url).href;
|
||||
const __wasmFile = await fetch(__wasmUrl).then((res) => res.arrayBuffer())
|
||||
|
||||
export let MainWorker = null;
|
||||
const napiModule = await setupMainThread(__wasmFile, () => {
|
||||
const worker = new TursoWorker({
|
||||
name: 'turso-database',
|
||||
type: 'module',
|
||||
})
|
||||
MainWorker = worker;
|
||||
return worker
|
||||
});
|
||||
|
||||
export default napiModule.exports
|
||||
export const Database = napiModule.exports.Database
|
||||
export const Opfs = napiModule.exports.Opfs
|
||||
export const OpfsFile = napiModule.exports.OpfsFile
|
||||
export const Statement = napiModule.exports.Statement
|
||||
export const connect = napiModule.exports.connect
|
||||
export const initThreadPool = napiModule.exports.initThreadPool
|
||||
@@ -18,8 +18,8 @@
|
||||
".": {
|
||||
"default": "./dist/promise-default.js"
|
||||
},
|
||||
"./bundled": {
|
||||
"default": "./bundle/main.js"
|
||||
"./bundle": {
|
||||
"default": "./bundle/main.es.js"
|
||||
},
|
||||
"./vite": {
|
||||
"development": "./dist/promise-vite-dev-hack.js",
|
||||
|
||||
22
bindings/javascript/packages/browser/promise-bundle.ts
Normal file
22
bindings/javascript/packages/browser/promise-bundle.ts
Normal file
@@ -0,0 +1,22 @@
|
||||
import { DatabaseOpts, SqliteError, } from "@tursodatabase/database-common"
|
||||
import { connect as promiseConnect, Database } from "./promise.js";
|
||||
import { connect as nativeConnect, initThreadPool, MainWorker } from "./index-bundle.js";
|
||||
|
||||
/**
|
||||
* Creates a new database connection asynchronously.
|
||||
*
|
||||
* @param {string} path - Path to the database file.
|
||||
* @param {Object} opts - Options for database behavior.
|
||||
* @returns {Promise<Database>} - A promise that resolves to a Database instance.
|
||||
*/
|
||||
async function connect(path: string, opts: DatabaseOpts = {}): Promise<Database> {
|
||||
return await promiseConnect(path, opts, nativeConnect, async () => {
|
||||
await initThreadPool();
|
||||
if (MainWorker == null) {
|
||||
throw new Error("panic: MainWorker is not initialized");
|
||||
}
|
||||
return MainWorker;
|
||||
});
|
||||
}
|
||||
|
||||
export { connect, Database, SqliteError }
|
||||
@@ -4,13 +4,15 @@ import { defineConfig } from 'vite';
|
||||
export default defineConfig({
|
||||
base: './',
|
||||
build: {
|
||||
lib: {
|
||||
entry: resolve(__dirname, 'promise-bundle.ts'),
|
||||
name: 'database-browser',
|
||||
fileName: format => `main.${format}.js`,
|
||||
formats: ['es'],
|
||||
},
|
||||
rollupOptions: {
|
||||
input: {
|
||||
main: resolve(__dirname, 'promise-default.ts'),
|
||||
},
|
||||
output: {
|
||||
dir: 'bundle',
|
||||
entryFileNames: '[name].js',
|
||||
}
|
||||
},
|
||||
},
|
||||
|
||||
Reference in New Issue
Block a user