// Copies the ZXing reader wasm into public/ so the browser loads it from our own origin. // // zxing-wasm fetches it from a CDN by default, which the CSP forbids (SPEC.md section 14: // no third party scripts) and which would break the offline scan queue. Runs before dev // and build, so a fresh clone never has to remember. import { copyFileSync, mkdirSync } from 'node:fs'; import { createRequire } from 'node:module'; import { dirname, join, sep } from 'node:path'; import { fileURLToPath } from 'node:url'; const require = createRequire(import.meta.url); // Walk up from whichever build the resolver picked to the package root, so the path does // not depend on cjs vs es. The package does not export ./package.json. const readerEntry = require.resolve('zxing-wasm/reader'); const packageRoot = readerEntry.slice(0, readerEntry.lastIndexOf(`${sep}dist${sep}`)); const source = join(packageRoot, 'dist', 'reader', 'zxing_reader.wasm'); const targetDir = join(dirname(dirname(fileURLToPath(import.meta.url))), 'public', 'zxing'); mkdirSync(targetDir, { recursive: true }); copyFileSync(source, join(targetDir, 'zxing_reader.wasm')); console.info(`[zxing] wasm copied to ${targetDir}`);