diff --git a/build.ps1 b/build.ps1 index 1465b91..a6ab963 100644 --- a/build.ps1 +++ b/build.ps1 @@ -10,8 +10,6 @@ $prodFlag = If($prod) {"production"} Else {""} node .\esbuild.config.mjs -- $prodFlag if($LASTEXITCODE -ne 0) { - $host.SetShouldExit(1); - exit 1; } $pluginName = "sqlite3-opfs-test-plugin" diff --git a/esbuild.config.mjs b/esbuild.config.mjs index 888f546..7f07975 100644 --- a/esbuild.config.mjs +++ b/esbuild.config.mjs @@ -1,54 +1,61 @@ import esbuild from "esbuild"; import process from "process"; import builtins from "builtin-modules"; +import { TsconfigPathsPlugin } from "@esbuild-plugins/tsconfig-paths"; import inlineWorkerPlugin from "esbuild-plugin-inline-worker"; +import inlineImportPlugin from "esbuild-plugin-inline-import"; import fs from "fs"; - +const external = [ + "obsidian", + "electron", + "@codemirror/autocomplete", + "@codemirror/collab", + "@codemirror/commands", + "@codemirror/language", + "@codemirror/lint", + "@codemirror/search", + "@codemirror/state", + "@codemirror/view", + "@lezer/common", + "@lezer/highlight", + "@lezer/lr", + "pdfjs-dist", + "path", + "url", + "electron", + ...builtins, +]; async function build(prod) { fs.mkdirSync("build/plugin", { recursive: true }); const result = await esbuild .build({ plugins: [ + TsconfigPathsPlugin({}), + inlineImportPlugin(), inlineWorkerPlugin({ - alias: { - "@sqlite.org/sqlite-wasm/sqlite-wasm/jswasm/sqlite3-worker1-bundler-friendly": - "node_modules/@sqlite.org/sqlite-wasm/sqlite-wasm/jswasm/sqlite3-worker1-bundler-friendly.mjs", - }, + plugins: [TsconfigPathsPlugin({}), inlineImportPlugin()], + external, target: "es2020", format: "cjs", sourcemap: prod ? false : "inline", + banner: { + js: "", + }, + define: { + "import.meta.url": "globalThis.__importMetaUrl", + }, }), ], entryPoints: ["src/main.ts"], bundle: true, - external: [ - "obsidian", - "electron", - "@codemirror/autocomplete", - "@codemirror/collab", - "@codemirror/commands", - "@codemirror/language", - "@codemirror/lint", - "@codemirror/search", - "@codemirror/state", - "@codemirror/view", - "@lezer/common", - "@lezer/highlight", - "@lezer/lr", - "pdfjs-dist", - ...builtins, - ], + external, format: "cjs", target: "es2020", logLevel: "info", sourcemap: prod ? false : "inline", treeShaking: true, - outfile: "build/plugin/main.js", - alias: { - "@sqlite.org/sqlite-wasm/sqlite-wasm/jswasm/sqlite3-worker1-bundler-friendly": - "node_modules/@sqlite.org/sqlite-wasm/sqlite-wasm/jswasm/sqlite3-worker1-bundler-friendly.mjs", - }, + outfile: "build/plugin/main.js", }) .catch(() => process.exit(1)); @@ -56,10 +63,10 @@ async function build(prod) { fs.copyFileSync("manifest.json", "build/plugin/manifest.json"); fs.copyFileSync("src/styles.css", "build/plugin/styles.css"); fs.copyFileSync( - "node_modules/@sqlite.org/sqlite-wasm/sqlite-wasm/jswasm/sqlite3.wasm", + "node_modules/@btfash/sqlite-wasm/sqlite-wasm/jswasm/sqlite3.wasm", "build/plugin/sqlite3.wasm" ); - // fs.writeFileSync("build/meta.json", JSON.stringify(result.metafile)); + // fs.writeFileSync("build/meta.json", JSON.stringify(result.metafile)); } // Run the build. diff --git a/package.json b/package.json index 38e2c8c..5555fbd 100644 --- a/package.json +++ b/package.json @@ -12,17 +12,19 @@ "author": "", "license": "MIT", "devDependencies": { + "@esbuild-plugins/tsconfig-paths": "^0.1.2", "@types/node": "^16.11.6", "@typescript-eslint/eslint-plugin": "5.29.0", "@typescript-eslint/parser": "5.29.0", "builtin-modules": "3.3.0", "esbuild": "0.17.3", + "esbuild-plugin-inline-import": "^1.1.0", "obsidian": "latest", "tslib": "2.4.0", - "typescript": "4.7.4" + "typescript": "4.7.4", + "esbuild-plugin-inline-worker": "^0.1.1" }, "dependencies": { - "@sqlite.org/sqlite-wasm": "^3.45.1-build1", - "esbuild-plugin-inline-worker": "^0.1.1" + "@btfash/sqlite-wasm": "3.46.2" } } diff --git a/src/main.ts b/src/main.ts index da20acc..c50ce4f 100644 --- a/src/main.ts +++ b/src/main.ts @@ -9,7 +9,7 @@ import { Setting, } from "obsidian"; import TestWorker from "sqlite3.worker"; -import { sqlite3Worker1Promiser } from "@sqlite.org/sqlite-wasm"; +import { sqlite3Worker1Promiser } from "@btfash/sqlite-wasm/index.mjs"; // Remember to rename these classes and interfaces! @@ -61,10 +61,11 @@ export default class MyPlugin extends Plugin { let w = this.worker; this.worker.postMessage({ type: "init", + root: this.locateFile("", ""), path: this.locateFile("sqlite3.wasm", ""), }); this.promiser = await new Promise((resolve) => { - const _promiser = sqlite3Worker1Promiser({ + const _promiser = (sqlite3Worker1Promiser as (...args: any[]) => any)({ print: this.printNotice, printErr: console.error, locateFile: this.locateFile.bind(this), @@ -79,8 +80,8 @@ export default class MyPlugin extends Plugin { async start() { let openRes = await this.promiser("open", { - filename: "/test.sqlite2", - vfs: "opfs-sahpool", + filename: "/test.sqlite3", + vfs: "opfs", }); const { dbId } = openRes; console.log(openRes); diff --git a/src/sqlite.d.ts b/src/sqlite.d.ts deleted file mode 100644 index 08766fd..0000000 --- a/src/sqlite.d.ts +++ /dev/null @@ -1,5 +0,0 @@ -import "@sqlite.org/sqlite-wasm"; -declare module "@sqlite.org/sqlite-wasm" { - export const sqlite3Worker1Promiser: any; -} -declare module "@sqlite.org/sqlite-wasm/sqlite-wasm/jswasm/sqlite3-worker1-bundler-friendly"; diff --git a/src/sqlite3.worker.ts b/src/sqlite3.worker.ts index 7ab057a..a107b13 100644 --- a/src/sqlite3.worker.ts +++ b/src/sqlite3.worker.ts @@ -1,19 +1,22 @@ -import sqlite3InitModule from "@sqlite.org/sqlite-wasm"; -(async () => {})(); +import sqlite3InitModule from "@btfash/sqlite-wasm/sqlite-wasm/jswasm/sqlite3.mjs"; +import str from "inline:../node_modules/@btfash/sqlite-wasm/sqlite-wasm/jswasm/sqlite3-opfs-async-proxy.js" +global.__importMetaUrl = "" let sqlite3; +globalThis +let inited = false; onmessage = async (event) => { console.log(event); - if (event.data.type == "init") { + if (event.data.type == "init" && !inited) { + globalThis.__importMetaUrl = event.data.root sqlite3 = await sqlite3InitModule({ - locateFile: (path, prefix) => event.data.path, + locateFile: (path: string, prefix: string) => event.data.path, }); // debugger; console.log(`Running sqlite version: ${sqlite3.version.libVersion}`); - await sqlite3.installOpfsSAHPoolVfs({ - clearOnInit: false, - initialCapacity: 65536, + await sqlite3.installOpfsVfs({ + proxyUri: URL.createObjectURL(new Blob([str], {type: "application/javascript"})) }); sqlite3.initWorker1API(); - console.log(sqlite3.capi.sqlite3_vfs_find("opfs-sah")); + console.log(sqlite3.capi.sqlite3_vfs_find("opfs")); } }; diff --git a/src/typings/other.d.ts b/src/typings/other.d.ts new file mode 100644 index 0000000..1bd741f --- /dev/null +++ b/src/typings/other.d.ts @@ -0,0 +1,4 @@ +declare global { + var __importMetaUrl: string +} +export {} diff --git a/src/typings/sqlite.d.ts b/src/typings/sqlite.d.ts new file mode 100644 index 0000000..5b84496 --- /dev/null +++ b/src/typings/sqlite.d.ts @@ -0,0 +1,8 @@ +declare module "@btfash/sqlite-wasm/index.mjs" { + + import blah from "@btfash/sqlite-wasm"; + + export const sqlite3Worker1Promiser: any; + export {blah as default}; +} +declare module "@btfash/sqlite-wasm/sqlite-wasm/jswasm/sqlite3.mjs"; diff --git a/src/typings/workers.d.ts b/src/typings/workers.d.ts new file mode 100644 index 0000000..b78a25c --- /dev/null +++ b/src/typings/workers.d.ts @@ -0,0 +1,6 @@ +declare module "sqlite3.worker" { + const WorkerFactory: new () => Worker; + export default WorkerFactory; +} + +declare module "inline:../node_modules/@btfash/sqlite-wasm/sqlite-wasm/jswasm/sqlite3-opfs-async-proxy.js" diff --git a/src/workers.d.ts b/src/workers.d.ts deleted file mode 100644 index c053c77..0000000 --- a/src/workers.d.ts +++ /dev/null @@ -1,4 +0,0 @@ -declare module "sqlite3.worker" { - const WorkerFactory: new () => Worker; - export default WorkerFactory; -} diff --git a/tsconfig.json b/tsconfig.json index e6078de..0e217ec 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -11,6 +11,7 @@ "importHelpers": true, "isolatedModules": true, "strictNullChecks": true, + "allowSyntheticDefaultImports": true, "lib": [ "DOM", "ES5", @@ -18,7 +19,7 @@ "ES7" ] }, - + "files": ["src/typings/sqlite.d.ts"], "include": [ "**/*.ts" ] diff --git a/yarn.lock b/yarn.lock index ad7ae2c..2998898 100644 --- a/yarn.lock +++ b/yarn.lock @@ -2,6 +2,20 @@ # yarn lockfile v1 +"@btfash/sqlite-wasm@3.46.2": + version "3.46.2" + resolved "https://registry.yarnpkg.com/@btfash/sqlite-wasm/-/sqlite-wasm-3.46.2.tgz#56446e4e401e3544c34a8660b258918fbd4574ab" + integrity sha512-IPgIlas0XqCQ9/6kfQUd9VmO/diRoZ/ETzpoWAEIk2Bqz7xVCVsswmm/POZBkayGTMnJ331yJDc3iTjgJnYKKw== + +"@esbuild-plugins/tsconfig-paths@^0.1.2": + version "0.1.2" + resolved "https://registry.yarnpkg.com/@esbuild-plugins/tsconfig-paths/-/tsconfig-paths-0.1.2.tgz#1955de0a124ecf4364717a2fadbfbea876955232" + integrity sha512-TusFR26Y+Ze+Zm+NdfqZTSG4XyrXKxIaAfYCL3jASEI/gHjSdoCujATjzNWaaXs6Sk6Bv2D7NLr4Jdz1gysy/Q== + dependencies: + debug "^4.3.1" + find-up "^5.0.0" + strip-json-comments "^3.1.1" + "@esbuild/aix-ppc64@0.20.1": version "0.20.1" resolved "https://registry.yarnpkg.com/@esbuild/aix-ppc64/-/aix-ppc64-0.20.1.tgz#eafa8775019b3650a77e8310ba4dbd17ca7af6d5" @@ -248,11 +262,6 @@ "@nodelib/fs.scandir" "2.1.5" fastq "^1.6.0" -"@sqlite.org/sqlite-wasm@^3.45.1-build1": - version "3.45.1-build1" - resolved "https://registry.yarnpkg.com/@sqlite.org/sqlite-wasm/-/sqlite-wasm-3.45.1-build1.tgz#648f6a0a0a4c3a67aff24b0e1331af655b86fb60" - integrity sha512-1EgshFNhVeBtZ9KtQPm3PzzJ2CtpmXAq2DAPywy7WZ3gOK6p5n8TY+M+mBMpQCF5cLqrdNFb3Kp9uNie9rUAHw== - "@types/codemirror@5.60.8": version "5.60.8" resolved "https://registry.yarnpkg.com/@types/codemirror/-/codemirror-5.60.8.tgz#b647d04b470e8e1836dd84b2879988fc55c9de68" @@ -384,6 +393,13 @@ commondir@^1.0.1: resolved "https://registry.yarnpkg.com/commondir/-/commondir-1.0.1.tgz#ddd800da0c66127393cca5950ea968a3aaf1253b" integrity sha512-W9pAhw0ja1Edb5GVdIF1mjZw/ASI0AlShXM83UUGe2DVr5TdAPEA1OA8m/g8zWp9x6On7gqufY+FatDbC3MDQg== +debug@^4.3.1: + version "4.3.5" + resolved "https://registry.yarnpkg.com/debug/-/debug-4.3.5.tgz#e83444eceb9fedd4a1da56d671ae2446a01a6e1e" + integrity sha512-pt0bNEmneDIvdL1Xsd9oDQ/wrQRkXDT4AUWlNZNPKvW5x/jyO9VFXkJUP07vQ2upmw5PlaITaPKc31jK13V+jg== + dependencies: + ms "2.1.2" + debug@^4.3.4: version "4.3.4" resolved "https://registry.yarnpkg.com/debug/-/debug-4.3.4.tgz#1319f6579357f2338d3337d2cdd4914bb5dcc865" @@ -398,6 +414,11 @@ dir-glob@^3.0.1: dependencies: path-type "^4.0.0" +esbuild-plugin-inline-import@^1.1.0: + version "1.1.0" + resolved "https://registry.yarnpkg.com/esbuild-plugin-inline-import/-/esbuild-plugin-inline-import-1.1.0.tgz#9a581a50918021c48962da2309aecf24d3fb9fcd" + integrity sha512-b0xX4tPKBdRjX1CkzpnULpEdeTo9vxD+wf83PKvgUYnOEaJfVxLey4q+sfTUPAdDnRRYasJsQUgQW7/e2Gm5Dw== + esbuild-plugin-inline-worker@^0.1.1: version "0.1.1" resolved "https://registry.yarnpkg.com/esbuild-plugin-inline-worker/-/esbuild-plugin-inline-worker-0.1.1.tgz#f21a610ad7410972a408272e0d254654381eac58" @@ -547,6 +568,14 @@ find-up@^4.0.0: locate-path "^5.0.0" path-exists "^4.0.0" +find-up@^5.0.0: + version "5.0.0" + resolved "https://registry.yarnpkg.com/find-up/-/find-up-5.0.0.tgz#4c92819ecb7083561e4f4a240a86be5198f536fc" + integrity sha512-78/PXT1wlLLDgTzDs7sjq9hzz0vXD+zn+7wypEe4fXQxCmdmqfGsEPQxmiCSQI3ajFV91bVSsvNtrJRiW6nGng== + dependencies: + locate-path "^6.0.0" + path-exists "^4.0.0" + functional-red-black-tree@^1.0.1: version "1.0.1" resolved "https://registry.yarnpkg.com/functional-red-black-tree/-/functional-red-black-tree-1.0.1.tgz#1b0ab3bd553b2a0d6399d29c0e3ea0b252078327" @@ -600,6 +629,13 @@ locate-path@^5.0.0: dependencies: p-locate "^4.1.0" +locate-path@^6.0.0: + version "6.0.0" + resolved "https://registry.yarnpkg.com/locate-path/-/locate-path-6.0.0.tgz#55321eb309febbc59c4801d931a72452a681d286" + integrity sha512-iPZK6eYjbxRu3uB4/WZ3EsEIMJFMqAoopl3R+zuq0UjcAm/MO6KCweDgPfP3elTztoKP3KtnVHxTn2NHBSDVUw== + dependencies: + p-locate "^5.0.0" + lru-cache@^6.0.0: version "6.0.0" resolved "https://registry.yarnpkg.com/lru-cache/-/lru-cache-6.0.0.tgz#6d6fe6570ebd96aaf90fcad1dafa3b2566db3a94" @@ -652,6 +688,13 @@ p-limit@^2.2.0: dependencies: p-try "^2.0.0" +p-limit@^3.0.2: + version "3.1.0" + resolved "https://registry.yarnpkg.com/p-limit/-/p-limit-3.1.0.tgz#e1daccbe78d0d1388ca18c64fea38e3e57e3706b" + integrity sha512-TYOanM3wGwNGsZN2cVTYPArw454xnXj5qmWF1bEoAc4+cU/ol7GVh7odevjp1FNHduHc3KZMcFduxU5Xc6uJRQ== + dependencies: + yocto-queue "^0.1.0" + p-locate@^4.1.0: version "4.1.0" resolved "https://registry.yarnpkg.com/p-locate/-/p-locate-4.1.0.tgz#a3428bb7088b3a60292f66919278b7c297ad4f07" @@ -659,6 +702,13 @@ p-locate@^4.1.0: dependencies: p-limit "^2.2.0" +p-locate@^5.0.0: + version "5.0.0" + resolved "https://registry.yarnpkg.com/p-locate/-/p-locate-5.0.0.tgz#83c8315c6785005e3bd021839411c9e110e6d834" + integrity sha512-LaNjtRWUBY++zB5nE/NwcaoMylSPk+S+ZHNB1TzdbMJMny6dynpAGt7X/tl/QYq3TIeE6nxHppbo2LGymrG5Pw== + dependencies: + p-limit "^3.0.2" + p-try@^2.0.0: version "2.2.0" resolved "https://registry.yarnpkg.com/p-try/-/p-try-2.2.0.tgz#cb2868540e313d61de58fafbe35ce9004d5540e6" @@ -725,10 +775,10 @@ slash@^3.0.0: resolved "https://registry.yarnpkg.com/slash/-/slash-3.0.0.tgz#6539be870c165adbd5240220dbe361f1bc4d4634" integrity sha512-g9Q1haeby36OSStwb4ntCGGGaKsaVSjQ68fBxoQcutl5fS1vuY18H3wSt3jFyFtrkx+Kz0V1G85A4MyAdDMi2Q== -sqlite-wasm-esm@^0.0.30: - version "0.0.30" - resolved "https://registry.yarnpkg.com/sqlite-wasm-esm/-/sqlite-wasm-esm-0.0.30.tgz#b25492775bc9f26d260d683820787b840b466592" - integrity sha512-rLl+STKLfGXyBcpQlH6uEMMh76YXixY3s+qDEMzIiMMsyN7iXLmo4Mk1Su/6GoJFprSWP+cgOCWQsAbLELCEPg== +strip-json-comments@^3.1.1: + version "3.1.1" + resolved "https://registry.yarnpkg.com/strip-json-comments/-/strip-json-comments-3.1.1.tgz#31f1281b3832630434831c310c01cccda8cbe006" + integrity sha512-6fPc+R4ihwqP6N/aIv2f1gMH8lOVtWQHoqC4yK6oSDVVocumAsfCqjkXnqiYMhmMwS/mEHLp7Vehlt3ql6lEig== to-regex-range@^5.0.1: version "5.0.1" @@ -763,3 +813,8 @@ yallist@^4.0.0: version "4.0.0" resolved "https://registry.yarnpkg.com/yallist/-/yallist-4.0.0.tgz#9bb92790d9c0effec63be73519e11a35019a3a72" integrity sha512-3wdGidZyq5PB084XLES5TpOSRA3wjXAlIWMhum2kRcv/41Sn2emQ0dycQW4uZXLejwKvg6EsvbdlVL+FYEct7A== + +yocto-queue@^0.1.0: + version "0.1.0" + resolved "https://registry.yarnpkg.com/yocto-queue/-/yocto-queue-0.1.0.tgz#0294eb3dee05028d31ee1a5fa2c556a6aaf10a1b" + integrity sha512-rVksvsnNCdJ/ohGc6xgPwyN8eheCxsiLM8mxuE/t/mOVqJewPuO1miLpTHQiRgTKCLexL4MeAFVagts7HmNZ2Q==