Open History to see the last commit
1import { readFileSync } from 'node:fs'
2import { createRequire } from 'node:module'
3import { defineConfig, mergeConfig } from 'vitest/config'
4import { tanstackViteConfig } from '@tanstack/vite-config'
5import packageJson from './package.json'
6import type { Plugin } from 'vite'
7
8const waSqliteWasmImport = `@journeyapps/wa-sqlite/dist/wa-sqlite.wasm?url&no-inline`
9const waSqliteWasmModule = `\0tanstack-wa-sqlite-wasm-url`
10
11function packageWaSqliteWasm(): Plugin {
12 return {
13 name: `package-wa-sqlite-wasm`,
14 enforce: `pre`,
15 apply: `build`,
16 // Source served by Vite already has resolved asset URLs. Only the built
17 // package must find its assets after Vite's dependency optimizer moves it.
18 config: () => ({
19 define: {
20 __BROWSER_SQLITE_PACKAGE_BUILD__: `true`,
21 __BROWSER_SQLITE_PACKAGE_VERSION__: JSON.stringify(
22 `${packageJson.name}@${packageJson.version}`,
23 ),
24 },
25 }),
26 resolveId(id) {
27 if (id === waSqliteWasmImport) return waSqliteWasmModule
28 return undefined
29 },
30 load(id) {
31 if (id !== waSqliteWasmModule) return
32 const require = createRequire(import.meta.url)
33 const wasmPath = require.resolve(
34 `@journeyapps/wa-sqlite/dist/wa-sqlite.wasm`,
35 )
36 const reference = this.emitFile({
37 type: `asset`,
38 fileName: `wa-sqlite.wasm`,
39 source: readFileSync(wasmPath),
40 })
41 return `export default import.meta.ROLLUP_FILE_URL_${reference}`
42 },
43 }
44}
45
46function preserveWaSqliteWasmUrl(): Plugin {
47 return {
48 name: `preserve-wa-sqlite-wasm-url`,
49 enforce: `pre`,
50 transform(source: string, id: string) {
51 if (
52 !/[\\/]@journeyapps[\\/]wa-sqlite[\\/]dist[\\/]wa-sqlite\.mjs$/.test(id)
53 ) {
54 return
55 }
56
57 // locateFile supplies the WASM URL at runtime. Keep Vite from turning
58 // wa-sqlite's unused fallback URL into an inlined data URL.
59 const defaultUrl = `new URL("wa-sqlite.wasm",import.meta.url)`
60 if (!source.includes(defaultUrl)) {
61 this.error(`wa-sqlite's WASM URL pattern changed`)
62 }
63 return {
64 code: source.replace(
65 defaultUrl,
66 `new URL("wa-sqlite" + ".wasm",import.meta.url)`,
67 ),
68 // The edit stays within one line, so the existing mappings still hold
69 // apart from columns after it on that line.
70 map: null,
71 }
72 },
73 }
74}
75
76const config = defineConfig({
77 // Build with relative paths so the ?worker&url and WASM URL imports emit
78 // new URL("../assets/opfs-worker.js", import.meta.url) and
79 // new URL("../../wa-sqlite.wasm", import.meta.url) instead of absolute
80 // "/assets/..." paths that break for library consumers. opfs-database.ts
81 // relies on the two depths differing to tell a pre-bundled copy of this
82 // package from one served in place; the install-layout e2e test covers it.
83 base: `./`,
84 plugins: [packageWaSqliteWasm()],
85 worker: {
86 // opfs-database.ts starts the built worker as a classic script.
87 format: `iife`,
88 // A fixed name lets package.json export the worker as one file. Consumer
89 // bundlers emit it under their own hashed name.
90 rollupOptions: { output: { entryFileNames: `assets/[name].js` } },
91 plugins: () => [preserveWaSqliteWasmUrl()],
92 },
93 test: {
94 name: packageJson.name,
95 include: [`tests/**/*.test.ts`],
96 environment: `node`,
97 coverage: { enabled: true, provider: `istanbul`, include: [`src/**/*`] },
98 typecheck: {
99 enabled: true,
100 include: [`tests/**/*.test.ts`, `tests/**/*.test-d.ts`],
101 },
102 },
103})
104
105export default mergeConfig(
106 config,
107 tanstackViteConfig({
108 entry: `./src/index.ts`,
109 srcDir: `./src`,
110 bundledDeps: [waSqliteWasmImport],
111 }),
112)