|
| 1 | +import { describe, test, expect } from 'vitest'; |
| 2 | +import { execSync } from 'node:child_process'; |
| 3 | +import path from 'node:path'; |
| 4 | +import fs from 'node:fs'; |
| 5 | + |
| 6 | +describe('Vite bundler compatibility', () => { |
| 7 | + test('should rename sqlite3.wasm with a hash and update the import URL', () => { |
| 8 | + const testDir = path.resolve(__dirname, 'vite-repro'); |
| 9 | + const distDir = path.resolve(testDir, 'dist'); |
| 10 | + |
| 11 | + // Clean up previous build |
| 12 | + if (fs.existsSync(distDir)) { |
| 13 | + fs.rmSync(distDir, { recursive: true, force: true }); |
| 14 | + } |
| 15 | + |
| 16 | + // Run vite build |
| 17 | + // We use npx to ensure we use the project's vite |
| 18 | + execSync('npx vite build', { cwd: testDir, stdio: 'inherit' }); |
| 19 | + |
| 20 | + // 1. Check if hashed WASM file exists in dist/assets |
| 21 | + const assetsDir = path.resolve(distDir, 'assets'); |
| 22 | + const files = fs.readdirSync(assetsDir); |
| 23 | + const wasmFile = files.find( |
| 24 | + (f) => f.startsWith('sqlite3-') && f.endsWith('.wasm'), |
| 25 | + ); |
| 26 | + |
| 27 | + expect(wasmFile).toBeDefined(); |
| 28 | + console.log('Found hashed WASM file:', wasmFile); |
| 29 | + |
| 30 | + // 2. Check if the JS bundle contains the hashed WASM filename |
| 31 | + const assetsDirJs = path.resolve(distDir, 'assets'); |
| 32 | + const jsFiles = fs |
| 33 | + .readdirSync(assetsDirJs) |
| 34 | + .filter((f) => f.endsWith('.js')); |
| 35 | + const mainBundle = jsFiles.find((f) => f.startsWith('index-')); |
| 36 | + expect(mainBundle).toBeDefined(); |
| 37 | + |
| 38 | + const bundleContent = fs.readFileSync( |
| 39 | + path.resolve(assetsDirJs, mainBundle), |
| 40 | + 'utf8', |
| 41 | + ); |
| 42 | + |
| 43 | + // It should contain something like: new URL("/assets/sqlite3-hash.wasm", import.meta.url) |
| 44 | + // Vite might use different quotes or spacing, but it should definitely have the hashed filename. |
| 45 | + expect(bundleContent).toContain(wasmFile); |
| 46 | + |
| 47 | + // Specifically check that it's part of a new URL call or at least correctly referenced |
| 48 | + // In our previous check it was: new URL("/assets/sqlite3-Bguoklsa.wasm",import.meta.url) |
| 49 | + const urlPattern = new RegExp( |
| 50 | + `new URL\\(".*${wasmFile}",\\s*import\\.meta\\.url\\)`, |
| 51 | + ); |
| 52 | + expect(bundleContent).toMatch(urlPattern); |
| 53 | + }); |
| 54 | +}); |
0 commit comments