vite-plugin-wat2wasm 1.2.7 → 2.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +7 -23
- package/dist/index.d.ts +2 -11
- package/dist/index.js +2 -29
- package/dist/index.js.map +1 -1
- package/package.json +25 -18
- /package/dist/{modules.d.ts → types.d.ts} +0 -0
package/README.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# vite-plugin-wat2wasm
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
A simple Vite plugin that enables `.wat` (WebAssembly Text Format) compilation and allows usage of WebAssembly within your codebase/library.
|
|
4
4
|
|
|
5
5
|
## Installation
|
|
6
6
|
|
|
@@ -14,11 +14,11 @@ $ <your-preferred-package-manager> install -D vite-plugin-wat2wasm
|
|
|
14
14
|
|
|
15
15
|
``` ts
|
|
16
16
|
import { defineConfig } from "vite";
|
|
17
|
-
import
|
|
17
|
+
import wat2WasmPlugin from "vite-plugin-wat2wasm";
|
|
18
18
|
|
|
19
19
|
export default defineConfig({
|
|
20
20
|
plugins: [
|
|
21
|
-
|
|
21
|
+
wat2WasmPlugin({ /* Refer to the API docs below for options */ }),
|
|
22
22
|
/* ...other plugins */
|
|
23
23
|
],
|
|
24
24
|
|
|
@@ -31,7 +31,7 @@ export default defineConfig({
|
|
|
31
31
|
Reference it as a comment...
|
|
32
32
|
|
|
33
33
|
``` ts
|
|
34
|
-
/// <reference types="vite-plugin-wat2wasm/
|
|
34
|
+
/// <reference types="vite-plugin-wat2wasm/types" />
|
|
35
35
|
```
|
|
36
36
|
|
|
37
37
|
or include it in your `tsconfig.json` file
|
|
@@ -40,7 +40,7 @@ or include it in your `tsconfig.json` file
|
|
|
40
40
|
{
|
|
41
41
|
"include": ["src"],
|
|
42
42
|
"compilerOptions": {
|
|
43
|
-
"types": ["vite-plugin-wat2wasm/
|
|
43
|
+
"types": ["vite-plugin-wat2wasm/types", /* ... other type files/packages */]
|
|
44
44
|
// ... other options for Typescript
|
|
45
45
|
}
|
|
46
46
|
}
|
|
@@ -81,11 +81,11 @@ interface FooImports {
|
|
|
81
81
|
|
|
82
82
|
This section contains more in-depth details about the `vite-plugin-wat2wasm` library and how to make use of what `vite-plugin-wat2wasm` offers.
|
|
83
83
|
|
|
84
|
-
### `
|
|
84
|
+
### `wat2WasmPlugin`
|
|
85
85
|
|
|
86
86
|
Enables compilation of `.wat` files and generation of `.wasm`, with modifiable settings.
|
|
87
87
|
|
|
88
|
-
`
|
|
88
|
+
`wat2WasmPlugin(options:` [`Wat2WasmOptions`](#wat2wasmoptions)`):`[`Plugin`](<https://vite.dev/guide/api-plugin>)
|
|
89
89
|
|
|
90
90
|
#### Parameters
|
|
91
91
|
|
|
@@ -101,27 +101,11 @@ The configuration settings for `vite-plugin-wat2wasm`.
|
|
|
101
101
|
|
|
102
102
|
#### Properties
|
|
103
103
|
|
|
104
|
-
- `emitWasm?: boolean = true` - Determines whether `.wasm` files will be outputted after compiling `.wat` files during build. Useful if you want other bundlers/compilers to take over generation of `.wasm` files.
|
|
105
|
-
|
|
106
|
-
- `target?: "all" |` [`WasmTarget`](#wasmtarget) `| WasmTarget[] = "all"` - Selects the targets that can use the `.wat` modules. `"all"` means that all targets available in [`WasmTarget`](#wasmtarget) are targeted and can use said modules.
|
|
107
|
-
|
|
108
104
|
- `parser?:` [`WasmParserOptions`](#wasmparseroptions) `= {}` -
|
|
109
105
|
Configures `.wasm` features you wish to enable for `vite-plugin-wat2wasm`.
|
|
110
106
|
|
|
111
107
|
- `generator?:` [`WasmGeneratorOptions`](#wasmgeneratoroptions) `= {}` - Configures how `vite-plugin-wat2wasm` to generate `.wasm` files.
|
|
112
108
|
|
|
113
|
-
- `relDir?: string = "."` - Where, relative from the root directory specified in the Vite config, should it generate the emitted `.wasm` files in the output directory.
|
|
114
|
-
|
|
115
|
-
### `WasmTarget`
|
|
116
|
-
|
|
117
|
-
The available targets that is supported by `vite-plugin-wat2wasm`.
|
|
118
|
-
|
|
119
|
-
#### Values
|
|
120
|
-
|
|
121
|
-
`"browser"` - Browsers and runtimes that include DOM libraries can use the bundled `.wat` modules.
|
|
122
|
-
|
|
123
|
-
`"node"` - Node.js and runtimes that include the node library can use the bundled `.wat` modules.
|
|
124
|
-
|
|
125
109
|
### `WasmParserOptions`
|
|
126
110
|
|
|
127
111
|
See [`wabt.WasmFeatures`](https://github.com/AssemblyScript/wabt.js/blob/main/README.md) for more info.
|
package/dist/index.d.ts
CHANGED
|
@@ -7,28 +7,19 @@ type WabtParserFunc = Awaited<ReturnType<typeof initWabt>>["parseWat"];
|
|
|
7
7
|
type WasmParserOptions = Parameters<WabtParserFunc>[2];
|
|
8
8
|
/** See @see {@link https://github.com/AssemblyScript/wabt.js/blob/main/README.md|`wabt.ToBinaryOptions`} for more info. @see WasmGeneratorOptions */
|
|
9
9
|
type WasmGeneratorOptions = Parameters<ReturnType<WabtParserFunc>["toBinary"]>[0];
|
|
10
|
-
declare const WASM_TARGETS: readonly ["browser", "node"];
|
|
11
|
-
/** The available targets that is supported by `vite-plugin-wat2wasm`. @see WasmTarget */
|
|
12
|
-
type WasmTarget = typeof WASM_TARGETS[number];
|
|
13
10
|
/** The configuration settings for `vite-plugin-wat2wasm`. @see Wat2WasmOptions */
|
|
14
11
|
interface Wat2WasmOptions {
|
|
15
|
-
/** Determines whether `.wasm` files will be outputted after compiling `.wat` files during build. Useful if you want other bundlers/compilers to take over generation of `.wasm` files. @default true */
|
|
16
|
-
emitWasm?: boolean;
|
|
17
|
-
/** Selects the targets that can use the `.wat` modules. "all" means that all targets available in @see {@link WasmTarget|`WasmTarget`} can use said modules. @default "all" */
|
|
18
|
-
target?: "all" | WasmTarget | WasmTarget[];
|
|
19
12
|
/** Configures `.wasm` features you wish to enable for `vite-plugin-wat2wasm`. @default {} @see {@link WasmParserOptions|`WasmParserOptions`} */
|
|
20
13
|
parser?: WasmParserOptions;
|
|
21
14
|
/** Configures how `vite-plugin-wat2wasm` to generate `.wasm` files. @default {} @see {@link WasmGeneratorOptions|`WasmGeneratorOptions`} */
|
|
22
15
|
generator?: WasmGeneratorOptions;
|
|
23
|
-
/** Where, relative from the root directory specified in the Vite config, should it generate the emitted `.wasm` files in the output directory. @default "." */
|
|
24
|
-
relDir?: string;
|
|
25
16
|
}
|
|
26
17
|
/** Enables compilation of `.wat` files and generation of `.wasm`, with modifiable settings.
|
|
27
18
|
*
|
|
28
19
|
* @param options - the configuration options for `vite-plugin-wat2wasm`. @see {@link Wat2WasmOptions|`Wat2WasmOptions`}
|
|
29
20
|
* @returns a Vite plugin object that allows for compilation of `.wat` files. @see {@link https://vite.dev/guide/api-plugin|`Plugin`}
|
|
30
21
|
*/
|
|
31
|
-
declare const
|
|
22
|
+
declare const wat2WasmPlugin: (options?: Wat2WasmOptions) => Plugin;
|
|
32
23
|
//#endregion
|
|
33
|
-
export { type WasmGeneratorOptions, type WasmParserOptions, type
|
|
24
|
+
export { type WasmGeneratorOptions, type WasmParserOptions, type Wat2WasmOptions, wat2WasmPlugin as default };
|
|
34
25
|
//# sourceMappingURL=index.d.ts.map
|
package/dist/index.js
CHANGED
|
@@ -1,31 +1,4 @@
|
|
|
1
|
-
import e from"
|
|
1
|
+
import e from"wabt";const t=await e();var n=(e={})=>{let{parser:n={},generator:r={}}=e;return{name:`wat2wasm`,transform(e,i){if(!i.endsWith(`.wat`))return null;let a=t.parseWat(i,e,n).toBinary(r);r.log&&console.log(i+`
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
return WebAssembly.instantiate(new Uint8Array(str.length).fill(0x00).map((_, i) => str.charCodeAt(i)).buffer, imports).then(({ instance: { exports } }) => exports).catch((err) => {
|
|
5
|
-
const { message } = err;
|
|
6
|
-
if (message === undefined || typeof message !== "string") {
|
|
7
|
-
console.error(err[Symbol.toStringTag] === "WebAssembly.Exception" ? "Exception tag from WebAssembly file has been thrown while instantiating." : "Unknown error while instantiating WebAssembly file.");
|
|
8
|
-
return;
|
|
9
|
-
}
|
|
10
|
-
|
|
11
|
-
const res = message.match(/ @\\+(\\d+)\\s*\$/);
|
|
12
|
-
|
|
13
|
-
if (res === null) {
|
|
14
|
-
console.error(message);
|
|
15
|
-
return;
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
const [_, ptr] = res;
|
|
19
|
-
console.error(message + ": " + [...new TextEncoder().encode(str.slice(+ptr - 40, +ptr + 40))].map((byte, i) => (i === 40 ? "[" : "") + byte.toString(16).padStart(2, "0") + (i === 40 ? "]" : "")).join(" "));
|
|
20
|
-
});
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
export default init;
|
|
24
|
-
`},async transform(e,t){if(!(r.emitWasm??(`environment`in this&&this.environment.mode===`build`))||!o.test(t))return null;let s=`__`+n.randomBytes(4).toString(`hex`),c=`const ${s} = (path, imports) => {
|
|
25
|
-
`+(p.includes(`node`)?` if (typeof process !== "undefined" && "versions" in process && "node" in process.versions) return WebAssembly.instantiate(require("fs").readFileSync(path), imports);
|
|
26
|
-
`:``)+(p.includes(`browser`)?` if (typeof window !== "undefined" && typeof document !== "undefined") return WebAssembly.instantiateStreaming(fetch(path), imports);
|
|
27
|
-
`:``)+`
|
|
28
|
-
throw new Error("This JavaScript runtime is not supported by this module. If this is a mistake, adjust your target to fit your specific runtime.");
|
|
29
|
-
};
|
|
30
|
-
`,l=e=>`async (imports) => ${s}("${e}", imports).then(({ instance: { exports } }) => exports)`,u=e;return e=await g(this,e,t,i,(e,t)=>`const ${t} = ${l(e)};`),e=await g(this,e,t,a,(e,t)=>`const ${t} = { default: ${l(e)} };`),e!==u&&(e=c+e),e},buildEnd(){for(let[e,t]of m)this.emitFile({type:`asset`,fileName:e,source:t})}}};export{l as default};
|
|
3
|
+
`,a.log);let o=a.buffer;return`export default async (imports = {}) => WebAssembly.instantiate(Uint8Array.from(atob("${btoa(String.fromCharCode(...o))}"), (char) => char.charCodeAt(0)), imports).then(({ instance: { exports } }) => exports);`}}};export{n as default};
|
|
31
4
|
//# sourceMappingURL=index.js.map
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","names":["path"],"sources":["../src/index.ts"],"sourcesContent":["import fs from \"fs\";\r\nimport path from \"path\";\r\n\r\nimport crypto from \"crypto\";\r\n\r\nimport type { Plugin } from \"vite\";\r\nimport type { TransformPluginContext } from \"rolldown\";\r\n\r\nimport initWabt from \"wabt\";\r\n\r\nconst IMPORT_DEFAULT_REG = /(^|\\s)import\\s+([A-Za-z_$][\\w$]*)\\s+from\\s*['\"]([^'\"\\n]*\\.wat)['\"](\\s*;|$)/m;\r\nconst IMPORT_STAR_REG = /(^|\\s)import\\s*\\*\\s*as\\s+([A-Za-z_$][\\w$]*)\\s+from\\s*['\"]([^'\"\\n]*\\.wat)[;\"](\\s*;|$)/m;\r\n\r\nconst JS_FILE_EXT_REG = /\\.([mc]?[jt]sx?)$/i;\r\n\r\nconst wabt = await initWabt();\r\n\r\ntype WabtParserFunc = Awaited<ReturnType<typeof initWabt>>[\"parseWat\"];\r\n\r\n/** See @see {@link https://github.com/AssemblyScript/wabt.js/blob/main/README.md|`wabt.WasmFeatures`} for more info. @see WasmParserOptions */\r\ntype WasmParserOptions = Parameters<WabtParserFunc>[2];\r\n\r\n/** See @see {@link https://github.com/AssemblyScript/wabt.js/blob/main/README.md|`wabt.ToBinaryOptions`} for more info. @see WasmGeneratorOptions */\r\ntype WasmGeneratorOptions = Parameters<ReturnType<WabtParserFunc>[\"toBinary\"]>[0];\r\n\r\nconst WASM_TARGETS = [\"browser\", \"node\"] as const;\r\n\r\n/** The available targets that is supported by `vite-plugin-wat2wasm`. @see WasmTarget */\r\ntype WasmTarget = typeof WASM_TARGETS[number];\r\n\r\n/** The configuration settings for `vite-plugin-wat2wasm`. @see Wat2WasmOptions */\r\ninterface Wat2WasmOptions {\r\n /** Determines whether `.wasm` files will be outputted after compiling `.wat` files during build. Useful if you want other bundlers/compilers to take over generation of `.wasm` files. @default true */\r\n emitWasm?: boolean;\r\n\r\n /** Selects the targets that can use the `.wat` modules. \"all\" means that all targets available in @see {@link WasmTarget|`WasmTarget`} can use said modules. @default \"all\" */\r\n target?: \"all\" | WasmTarget | WasmTarget[];\r\n\r\n /** Configures `.wasm` features you wish to enable for `vite-plugin-wat2wasm`. @default {} @see {@link WasmParserOptions|`WasmParserOptions`} */\r\n parser?: WasmParserOptions;\r\n\r\n /** Configures how `vite-plugin-wat2wasm` to generate `.wasm` files. @default {} @see {@link WasmGeneratorOptions|`WasmGeneratorOptions`} */\r\n generator?: WasmGeneratorOptions;\r\n\r\n /** Where, relative from the root directory specified in the Vite config, should it generate the emitted `.wasm` files in the output directory. @default \".\" */\r\n relDir?: string;\r\n}\r\n\r\n/** Enables compilation of `.wat` files and generation of `.wasm`, with modifiable settings.\r\n *\r\n * @param options - the configuration options for `vite-plugin-wat2wasm`. @see {@link Wat2WasmOptions|`Wat2WasmOptions`}\r\n * @returns a Vite plugin object that allows for compilation of `.wat` files. @see {@link https://vite.dev/guide/api-plugin|`Plugin`}\r\n */\r\nconst watCompilerPlugin = (options: Wat2WasmOptions = {}): Plugin => {\r\n const {\r\n target: optionsTarget = \"all\",\r\n\r\n parser: parserOptions = {},\r\n generator: generatorOptions = {},\r\n\r\n relDir = \".\"\r\n } = options;\r\n\r\n const targets =\r\n optionsTarget === \"all\"\r\n ? WASM_TARGETS as unknown as WasmTarget[] :\r\n typeof optionsTarget === \"string\"\r\n ? [optionsTarget] :\r\n optionsTarget.length === 0\r\n ? WASM_TARGETS as unknown as WasmTarget[]\r\n : optionsTarget;\r\n\r\n const importedWatFiles = new Map<string, Uint8Array>();\r\n\r\n const compileWat = (watPath: string): Uint8Array => {\r\n const name = path.basename(watPath, \".wat\");\r\n const textBfr = fs.readFileSync(watPath, { encoding: \"utf-8\" });\r\n\r\n const module = wabt.parseWat(name, textBfr, parserOptions);\r\n const bfr = module.toBinary(generatorOptions).buffer;\r\n\r\n return bfr;\r\n };\r\n\r\n const transformImports = async (ctx: TransformPluginContext, code: string, codePath: string, statementReg: RegExp, replacement: (importPath: string, name: string) => string): Promise<string> => {\r\n for (let match = code.match(statementReg); match !== null; match = code.match(statementReg)) {\r\n match.index ??= 0;\r\n\r\n const watRelPath = match[3];\r\n\r\n const watAbsPath = (await ctx.resolve(watRelPath, codePath))?.id.replaceAll(\"\\\\\", \"/\") ?? null;\r\n if (watAbsPath === null) return code;\r\n\r\n const bfr = compileWat(watAbsPath);\r\n\r\n const hasher = crypto.createHash(\"sha-256\");\r\n hasher.update(bfr);\r\n\r\n const hash = hasher.digest(\"base64url\").slice(0, 8);\r\n\r\n const rootPath = \"environment\" in ctx ? ctx.environment.config.root : process.cwd(); // some contexts do not provide an environment, so compute it separately.\r\n\r\n let distRelPath = path.posix.relative(path.join(rootPath, relDir), watAbsPath);\r\n if (distRelPath.startsWith(\".\")) distRelPath = path.win32.relative(path.join(rootPath, relDir), watAbsPath).replaceAll(\"\\\\\", \"/\"); // if posix relative function resolves the paths incorrectly, use the windows function instead.\r\n\r\n const watName = path.basename(distRelPath, \".wat\") + \"-\" + hash;\r\n const watParent = path.dirname(distRelPath);\r\n\r\n const wasmFilePath = path.posix.join(watParent, watName + \".wasm\");\r\n if (!importedWatFiles.has(wasmFilePath)) importedWatFiles.set(wasmFilePath, bfr);\r\n\r\n const importName = match[2];\r\n\r\n const preSpacing = match[1];\r\n const postSpacing = match[4];\r\n\r\n const statementStart = match.index ?? 0;\r\n const statementEnd = statementStart + match[0].length;\r\n\r\n code = code.slice(0, statementStart) + preSpacing + replacement(wasmFilePath, importName) + postSpacing + code.slice(statementEnd);\r\n }\r\n\r\n return code;\r\n };\r\n\r\n return {\r\n name: \"wat-compiler\",\r\n enforce: \"post\",\r\n\r\n async load(id: string) {\r\n const emitWasm = options.emitWasm ?? (\"environment\" in this && this.environment.mode === \"build\");\r\n\r\n if (emitWasm || !id.endsWith(\".wat\")) return null;\r\n\r\n const bfr = compileWat(id);\r\n const str = btoa(String.fromCharCode(...bfr));\r\n\r\n return (\r\n `const str = atob(\"${str}\");` + \"\\n\" +\r\n \"\" + \"\\n\" +\r\n \"const init = async (imports = {}) => {\" + \"\\n\" +\r\n \" return WebAssembly.instantiate(new Uint8Array(str.length).fill(0x00).map((_, i) => str.charCodeAt(i)).buffer, imports).then(({ instance: { exports } }) => exports).catch((err) => {\" + \"\\n\" +\r\n \" const { message } = err;\" + \"\\n\" +\r\n \" if (message === undefined || typeof message !== \\\"string\\\") {\" + \"\\n\" +\r\n \" console.error(err[Symbol.toStringTag] === \\\"WebAssembly.Exception\\\" ? \\\"Exception tag from WebAssembly file has been thrown while instantiating.\\\" : \\\"Unknown error while instantiating WebAssembly file.\\\");\" + \"\\n\" +\r\n \" return;\" + \"\\n\" +\r\n \" }\" + \"\\n\" +\r\n \" \" + \"\\n\" +\r\n \" const res = message.match(/ @\\\\+(\\\\d+)\\\\s*$/);\" + \"\\n\" +\r\n \" \" + \"\\n\" +\r\n \" if (res === null) {\" + \"\\n\" +\r\n \" console.error(message);\" + \"\\n\" +\r\n \" return;\" + \"\\n\" +\r\n \" }\" + \"\\n\" +\r\n \" \" + \"\\n\" +\r\n \" const [_, ptr] = res;\" + \"\\n\" +\r\n \" console.error(message + \\\": \\\" + [...new TextEncoder().encode(str.slice(+ptr - 40, +ptr + 40))].map((byte, i) => (i === 40 ? \\\"[\\\" : \\\"\\\") + byte.toString(16).padStart(2, \\\"0\\\") + (i === 40 ? \\\"]\\\" : \\\"\\\")).join(\\\" \\\"));\" + \"\\n\" +\r\n \" });\" + \"\\n\" +\r\n \"}\" + \"\\n\" +\r\n \"\" + \"\\n\" +\r\n \"export default init;\" + \"\\n\"\r\n );\r\n },\r\n\r\n async transform(code: string, id: string) {\r\n const emitWasm = options.emitWasm ?? (\"environment\" in this && this.environment.mode === \"build\");\r\n\r\n if (!emitWasm || !JS_FILE_EXT_REG.test(id)) return null;\r\n\r\n const initWasmFuncName = \"__\" + crypto.randomBytes(4).toString(\"hex\");\r\n const generateInitWasmFunc =\r\n `const ${initWasmFuncName} = (path, imports) => {` + \"\\n\" +\r\n (targets.includes(\"node\") ?\r\n \" if (typeof process !== \\\"undefined\\\" && \\\"versions\\\" in process && \\\"node\\\" in process.versions) return WebAssembly.instantiate(require(\\\"fs\\\").readFileSync(path), imports);\" + \"\\n\" :\r\n \"\") +\r\n (targets.includes(\"browser\") ?\r\n ` if (typeof window !== \"undefined\" && typeof document !== \"undefined\") return WebAssembly.instantiateStreaming(fetch(path), imports);` + \"\\n\" :\r\n \"\") +\r\n \"\" + \"\\n\" +\r\n \" throw new Error(\\\"This JavaScript runtime is not supported by this module. If this is a mistake, adjust your target to fit your specific runtime.\\\");\" + \"\\n\" +\r\n \"};\" + \"\\n\";\r\n\r\n const generateInitModuleFunc = (path: string): string => `async (imports) => ${initWasmFuncName}(\"${path}\", imports).then(({ instance: { exports } }) => exports)`;\r\n\r\n const originalCode = code;\r\n\r\n code = await transformImports(this as any, code, id, IMPORT_DEFAULT_REG, (path, name) => `const ${name} = ${generateInitModuleFunc(path)};`);\r\n code = await transformImports(this as any, code, id, IMPORT_STAR_REG , (path, name) => `const ${name} = { default: ${generateInitModuleFunc(path)} };`);\r\n\r\n if (code !== originalCode) code = generateInitWasmFunc + code;\r\n return code;\r\n },\r\n\r\n buildEnd() {\r\n for (const [distPath, bfr] of importedWatFiles) {\r\n this.emitFile({\r\n type: \"asset\",\r\n\r\n fileName: distPath,\r\n source: bfr\r\n });\r\n }\r\n }\r\n };\r\n};\r\n\r\nexport default watCompilerPlugin;\r\nexport type { Wat2WasmOptions, WasmParserOptions, WasmGeneratorOptions, WasmTarget };\r\n"],"mappings":"gFAUA,MAAM,EAAqB,8EACrB,EAAqB,wFAErB,EAAkB,qBAElB,EAAO,MAAM,GAAU,CAUvB,EAAe,CAAC,UAAW,OAAO,CAqLxC,IAAA,GAzJ2B,EAA2B,EAAE,GAAa,CACjE,GAAM,CACF,OAAQ,EAAgB,MAExB,OAAQ,EAAgB,EAAE,CAC1B,UAAW,EAAmB,EAAE,CAEhC,SAAS,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
|
|
1
|
+
{"version":3,"file":"index.js","names":[],"sources":["../src/index.ts"],"sourcesContent":["import type { Plugin } from \"vite\";\r\nimport initWabt from \"wabt\";\r\n\r\nconst wabt = await initWabt();\r\n\r\ntype WabtParserFunc = Awaited<ReturnType<typeof initWabt>>[\"parseWat\"];\r\n\r\n/** See @see {@link https://github.com/AssemblyScript/wabt.js/blob/main/README.md|`wabt.WasmFeatures`} for more info. @see WasmParserOptions */\r\ntype WasmParserOptions = Parameters<WabtParserFunc>[2];\r\n\r\n/** See @see {@link https://github.com/AssemblyScript/wabt.js/blob/main/README.md|`wabt.ToBinaryOptions`} for more info. @see WasmGeneratorOptions */\r\ntype WasmGeneratorOptions = Parameters<ReturnType<WabtParserFunc>[\"toBinary\"]>[0];\r\n\r\n/** The configuration settings for `vite-plugin-wat2wasm`. @see Wat2WasmOptions */\r\ninterface Wat2WasmOptions {\r\n /** Configures `.wasm` features you wish to enable for `vite-plugin-wat2wasm`. @default {} @see {@link WasmParserOptions|`WasmParserOptions`} */\r\n parser?: WasmParserOptions;\r\n\r\n /** Configures how `vite-plugin-wat2wasm` to generate `.wasm` files. @default {} @see {@link WasmGeneratorOptions|`WasmGeneratorOptions`} */\r\n generator?: WasmGeneratorOptions;\r\n}\r\n\r\n/** Enables compilation of `.wat` files and generation of `.wasm`, with modifiable settings.\r\n *\r\n * @param options - the configuration options for `vite-plugin-wat2wasm`. @see {@link Wat2WasmOptions|`Wat2WasmOptions`}\r\n * @returns a Vite plugin object that allows for compilation of `.wat` files. @see {@link https://vite.dev/guide/api-plugin|`Plugin`}\r\n */\r\nconst wat2WasmPlugin = (options: Wat2WasmOptions = {}): Plugin => {\r\n const {\r\n parser: parserOptions = {},\r\n generator: generatorOptions = {},\r\n } = options;\r\n\r\n return {\r\n name: \"wat2wasm\",\r\n\r\n transform(code: string, id: string) {\r\n if (!id.endsWith(\".wat\")) return null;\r\n\r\n const module = wabt.parseWat(id, code, parserOptions);\r\n\r\n const wasmWrapper = module.toBinary(generatorOptions);\r\n if (generatorOptions.log) console.log(id + \"\\n\" + \"\\n\", wasmWrapper.log);\r\n\r\n const bfr = wasmWrapper.buffer;\r\n\r\n return `export default async (imports = {}) => WebAssembly.instantiate(Uint8Array.from(atob(\"${btoa(String.fromCharCode(...bfr))}\"), (char) => char.charCodeAt(0)), imports).then(({ instance: { exports } }) => exports);`;\r\n }\r\n };\r\n};\r\n\r\nexport default wat2WasmPlugin;\r\nexport type { Wat2WasmOptions, WasmParserOptions, WasmGeneratorOptions };\r\n"],"mappings":"oBAGA,MAAM,EAAO,MAAM,GAAU,CAgD7B,IAAA,GAxBwB,EAA2B,EAAE,GAAa,CAC9D,GAAM,CACF,OAAQ,EAAgB,EAAE,CAC1B,UAAW,EAAmB,EAAE,EAChC,EAEJ,MAAO,CACH,KAAM,WAEN,UAAU,EAAc,EAAY,CAChC,GAAI,CAAC,EAAG,SAAS,OAAO,CAAE,OAAO,KAIjC,IAAM,EAFS,EAAK,SAAS,EAAI,EAAM,EAAc,CAE1B,SAAS,EAAiB,CACjD,EAAiB,KAAK,QAAQ,IAAI,EAAK;;EAAa,EAAY,IAAI,CAExE,IAAM,EAAM,EAAY,OAExB,MAAO,wFAAwF,KAAK,OAAO,aAAa,GAAG,EAAI,CAAC,CAAC,4FAExI"}
|
package/package.json
CHANGED
|
@@ -1,19 +1,31 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "vite-plugin-wat2wasm",
|
|
3
|
-
"
|
|
4
|
-
"
|
|
5
|
-
|
|
3
|
+
"version": "2.0.0",
|
|
4
|
+
"description": "A simple Vite plugin that enables `.wat` (WebAssembly Text Format) compilation and allows usage of WebAssembly within your codebase/library",
|
|
5
|
+
|
|
6
|
+
"homepage": "https://github.com/osoclos/vite-plugin-wat2wasm",
|
|
6
7
|
|
|
7
|
-
"author": "osoclos",
|
|
8
8
|
"license": "MIT",
|
|
9
|
+
"author": {
|
|
10
|
+
"name": "osoclos",
|
|
11
|
+
"email": "osoclosus@gmail.com",
|
|
12
|
+
|
|
13
|
+
"url": "https://github.com/osoclos"
|
|
14
|
+
},
|
|
9
15
|
|
|
10
|
-
"homepage": "https://github.com/osoclos/vite-plugin-wat2wasm#readme",
|
|
11
16
|
"repository": {
|
|
12
17
|
"type": "git",
|
|
13
18
|
"url": "git+https://github.com/osoclos/vite-plugin-wat2wasm.git"
|
|
14
19
|
},
|
|
15
20
|
|
|
16
|
-
"bugs": {
|
|
21
|
+
"bugs": {
|
|
22
|
+
"email": "osoclosus@gmail.com",
|
|
23
|
+
"url": "https://github.com/osoclos/vite-plugin-wat2wasm/issues"
|
|
24
|
+
},
|
|
25
|
+
|
|
26
|
+
"private": false,
|
|
27
|
+
|
|
28
|
+
"type": "module",
|
|
17
29
|
|
|
18
30
|
"main": "./dist/index.js",
|
|
19
31
|
"module": "./dist/index.js",
|
|
@@ -22,36 +34,31 @@
|
|
|
22
34
|
"files": ["dist"],
|
|
23
35
|
"exports": {
|
|
24
36
|
".": "./dist/index.js",
|
|
25
|
-
"./
|
|
37
|
+
"./types": "./dist/types.d.ts",
|
|
26
38
|
|
|
27
39
|
"./package.json": "./package.json"
|
|
28
40
|
},
|
|
29
41
|
|
|
30
42
|
"scripts": {
|
|
31
|
-
"dev-dev"
|
|
32
|
-
"dev-build": "concurrently 'bun run build' 'vite build --watch --config tests/
|
|
43
|
+
"dev-codebase-dev": "concurrently 'bun run build-watch' 'vite --config tests/codebase/vite.config.ts'",
|
|
44
|
+
"dev-codebase-build": "concurrently 'bun run build-watch' 'vite build --watch --config tests/codebase/vite.config.ts' 'vite preview --config tests/codebase/vite.config.ts'",
|
|
45
|
+
|
|
46
|
+
"dev-library": "concurrently 'bun run build-watch' 'tsdown --watch --config tests/library/tsdown.config.ts' 'vite --config tests/library/tests/vite.config.ts'",
|
|
33
47
|
|
|
34
48
|
"build": "tsdown && bun run scripts/post-build.ts",
|
|
35
49
|
"build-watch": "tsdown --watch --on-success 'bun run scripts/post-build.ts'",
|
|
36
50
|
|
|
37
|
-
"
|
|
38
|
-
"test-build": "vite build --watch --config tests/build/vite.config.ts && vite preview --config tests/build/vite.config.ts",
|
|
39
|
-
|
|
40
|
-
"bump": "tsdown && bun run scripts/post-build.ts && bumpp"
|
|
51
|
+
"bump": "bun run build && bumpp"
|
|
41
52
|
},
|
|
42
53
|
|
|
43
54
|
"dependencies": { "wabt": "^1.0.39" },
|
|
44
55
|
"devDependencies": {
|
|
45
56
|
"tsdown": "^0.18.4",
|
|
46
|
-
"rolldown": "^1.0.0-rc.9",
|
|
47
|
-
|
|
48
57
|
"vite": "^8.0.0",
|
|
49
|
-
"vitest": "^4.0.18",
|
|
50
|
-
"@vitest/browser-preview": "^4.0.18",
|
|
51
58
|
|
|
52
59
|
"typescript": "~5.9.3",
|
|
53
60
|
|
|
54
|
-
"@types/node": "^25.
|
|
61
|
+
"@types/node": "^25.5.0",
|
|
55
62
|
|
|
56
63
|
"bumpp": "^10.4.1",
|
|
57
64
|
"concurrently": "^9.2.1"
|
|
File without changes
|