import wasmPath from './pglite.wasm' with { type: 'file', embed: 'true' }
import dataPath from './pglite.data' with { type: 'file', embed: 'true' }
export async function createPGlite(dataDir: string, options?: PGliteOptions): Promise<PGlite> {
// Read the embedded files
const [wasmBuffer, dataBuffer] = await Promise.all([
Bun.file(wasmPath).arrayBuffer(),
Bun.file(dataPath).arrayBuffer(),
])
// Compile the WASM module
const wasmModule = await WebAssembly.compile(wasmBuffer)
// Create a Blob for the fs bundle
const fsBundle = new Blob([dataBuffer])
// Create PGlite instance with pre-loaded modules
const db = await PGlite.create(dataDir, {
...options,
wasmModule,
fsBundle,
})
return db
}And const args = process.argv.slice(2)
const isJs = args.find((arg) => arg.startsWith('--js') || arg.startsWith('-j')) !== undefined
await build({
entrypoints: ['src/index.ts'],
target: 'bun',
minify: true,
sourcemap: 'linked',
outdir: 'dist',
compile: isJs ? false : { outfile: 'app' },
define: {
'process.env.NODE_ENV': JSON.stringify('production'),
},
})When singlefile, it just $ bun run src/build.ts
dist/app 1.70 MB
dist/index.js.map 5.00 MB
$ bun run src/build.ts -j
dist/index.js 1.70 MB
dist/index.js.map 5.00 MB
dist/pglite-834ka1w2.wasm 8.45 MB
dist/pglite-0xccq9tf.data 4.71 MB |
Replies: 2 comments 1 reply
|
To embed assets into a single binary compiled with In your code, you have: import wasmPath from './pglite.wasm' with { type: 'file', embed: 'true' }In Bun, the To fix this, update your imports to only use import wasmPath from './pglite.wasm' with { type: 'file' }
import dataPath from './pglite.data' with { type: 'file' }
export async function createPGlite(dataDir: string, options?: PGliteOptions): Promise<PGlite> {
// Read the embedded files using Bun.file()
const [wasmBuffer, dataBuffer] = await Promise.all([
Bun.file(wasmPath).arrayBuffer(),
Bun.file(dataPath).arrayBuffer()
]);
// ...
}When you compile using |
|
Embedding general files ( The detail that matters is that embedding only happens under Minimal repro on 1.3.12: // app.ts
import wasmPath from "./blob.wasm" with { type: "file" };
const bytes = await Bun.file(wasmPath).arrayBuffer();
console.log(wasmPath, bytes.byteLength);
console.log("embeddedFiles:", Bun.embeddedFiles.length);bun build ./app.ts --compile --outfile appCopy the produced The path prints as So your |
sorry, I made a mistake. The new version of pglite requires importing more wasm, it can work properly now.