Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(vm): support wasm module #5131

Merged
merged 12 commits into from
Feb 8, 2024
11 changes: 9 additions & 2 deletions packages/vitest/src/runtime/external-executor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export interface ExternalModulesExecutorOptions {
}

interface ModuleInformation {
type: 'data' | 'builtin' | 'vite' | 'module' | 'commonjs'
type: 'data' | 'builtin' | 'vite' | 'wasm' | 'module' | 'commonjs'
url: string
path: string
}
Expand Down Expand Up @@ -165,7 +165,7 @@ export class ExternalModulesExecutor {
const pathUrl = isFileUrl ? fileURLToPath(identifier.split('?')[0]) : identifier
const fileUrl = isFileUrl ? identifier : pathToFileURL(pathUrl).toString()

let type: 'module' | 'commonjs' | 'vite'
let type: 'module' | 'commonjs' | 'vite' | 'wasm'
if (this.vite.canResolve(fileUrl)) {
type = 'vite'
}
Expand All @@ -175,6 +175,11 @@ export class ExternalModulesExecutor {
else if (extension === '.cjs') {
type = 'commonjs'
}
else if (extension === '.wasm') {
// still experimental on NodeJS --experimental-wasm-modules
// cf. ESM_FILE_FORMAT(url) in https://nodejs.org/api/esm.html
type = 'wasm'
}
else {
const pkgData = this.findNearestPackageData(normalize(pathUrl))
type = pkgData.type === 'module' ? 'module' : 'commonjs'
Expand Down Expand Up @@ -203,6 +208,8 @@ export class ExternalModulesExecutor {
}
case 'vite':
return await this.vite.createViteModule(url)
case 'wasm':
return await this.esm.createWebAssemblyModule(url, this.fs.readBuffer(path))
case 'module':
return await this.esm.createEsModule(url, this.fs.readFile(path))
case 'commonjs': {
Expand Down
9 changes: 9 additions & 0 deletions packages/vitest/src/runtime/vm/esm-executor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,15 @@ export class EsmExecutor {
return m
}

public async createWebAssemblyModule(fileUrl: string, code: Buffer) {
const cached = this.moduleCache.get(fileUrl)
if (cached)
return cached
const m = this.loadWebAssemblyModule(code, fileUrl)
this.moduleCache.set(fileUrl, m)
return m
}

public async loadWebAssemblyModule(source: Buffer, identifier: string) {
const cached = this.moduleCache.get(identifier)
if (cached)
Expand Down
2 changes: 1 addition & 1 deletion test/core/test/vm-wasm.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { expect, test, vi } from 'vitest'
// @ts-expect-error wasm is not typed
import { add } from '../src/add.wasm'

const wasmFileBuffer = readFileSync(resolve(__dirname, './src/add.wasm'))
const wasmFileBuffer = readFileSync(resolve(__dirname, '../src/add.wasm'))

test('supports native wasm imports', () => {
expect(add(1, 2)).toBe(3)
Expand Down
4 changes: 2 additions & 2 deletions test/core/vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ export default defineConfig({
},
test: {
name: 'core',
exclude: ['**/fixtures/**', '**/vm-wasm.test.ts', ...defaultExclude],
exclude: ['**/fixtures/**', ...defaultExclude],
slowTestThreshold: 1000,
testTimeout: 2000,
setupFiles: [
Expand Down Expand Up @@ -75,7 +75,7 @@ export default defineConfig({
},
server: {
deps: {
external: ['tinyspy', /src\/external/, /esm\/esm/, /\.wasm$/],
external: ['tinyspy', /src\/external/, /esm\/esm/, /\.wasm$/, /src\/wasm-bindgen\//],
hi-ogawa marked this conversation as resolved.
Show resolved Hide resolved
inline: ['inline-lib'],
},
},
Expand Down
Loading