Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: WASM's imports not handled properly w/ optimizeDeps (#11)
- Loading branch information
Showing
9 changed files
with
270 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,20 @@ | ||
// @syntect/wasm requires WASM to ES module transform | ||
import { highlight } from "@syntect/wasm"; | ||
import { parse } from "@jsona/openapi"; | ||
|
||
const TEST_CODE = "#include <cstdio>"; | ||
const TEST_LANG = "cpp"; | ||
const expectedResult = { | ||
value: { | ||
openapi: "3.0.0", | ||
info: { version: "0.1.0", title: "openapi" }, | ||
paths: {}, | ||
components: {} | ||
}, | ||
errors: null | ||
}; | ||
const actualResult = parse("{}"); | ||
|
||
const div = document.createElement("div"); | ||
div.innerHTML = highlight(TEST_CODE, TEST_LANG, "hl-").html; | ||
const result = div.innerText.trim(); | ||
if (result !== TEST_CODE) { | ||
console.error(`Expected ${JSON.stringify(TEST_CODE)} but got ${JSON.stringify(result)}.`); | ||
const expectedJson = JSON.stringify(expectedResult); | ||
const actualJson = JSON.stringify(actualResult); | ||
if (actualJson !== expectedJson) { | ||
console.error(` | ||
Expected ${expectedJson} | ||
but got ${actualJson}`); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,5 +2,3 @@ | |
# yarn lockfile v1 | ||
|
||
|
||
"vite@file:vite-placeholder": | ||
version "0.0.0" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import fs from "fs"; | ||
import { posix as path } from "path"; | ||
import { Plugin } from "esbuild"; | ||
|
||
import * as wasmHelper from "./wasm-helper"; | ||
import { generateGlueCode } from "./wasm-parser"; | ||
|
||
export function esbuildPlugin(): Plugin { | ||
return { | ||
name: "vite-plugin-wasm", | ||
setup(build) { | ||
const NAMESPACE = "vite-plugin-wasm-namespace"; | ||
|
||
build.onResolve({ filter: /\.wasm$/ }, args => ({ | ||
path: path.join(path.dirname(args.importer), args.path), | ||
namespace: NAMESPACE | ||
})); | ||
|
||
build.onLoad({ filter: /.*/, namespace: NAMESPACE }, async args => { | ||
const base64 = await fs.promises.readFile(args.path, "base64"); | ||
const dataUri = "data:application/wasm;base64," + base64; | ||
return { | ||
contents: ` | ||
const wasmUrl = "${dataUri}"; | ||
const initWasm = ${wasmHelper.code}; | ||
${await generateGlueCode(args.path, { initWasm: "initWasm", wasmUrl: "wasmUrl" })} | ||
`, | ||
loader: "js", | ||
resolveDir: path.dirname(args.path) | ||
}; | ||
}); | ||
} | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.