vite-plugin-wat2wasm 2.0.0 → 2.1.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 +5 -5
- package/dist/index.d.ts +5 -3
- package/dist/index.js +14 -2
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -49,7 +49,7 @@ or include it in your `tsconfig.json` file
|
|
|
49
49
|
### Using it in your application/library
|
|
50
50
|
|
|
51
51
|
``` ts
|
|
52
|
-
import initFoo from "./foo.wat"
|
|
52
|
+
import initFoo from "./foo.wat";
|
|
53
53
|
|
|
54
54
|
const bar = new WebAssembly.Global("i32", { value: 13 });
|
|
55
55
|
const foo = await initFoo<FooExports, FooImports>({
|
|
@@ -83,7 +83,7 @@ This section contains more in-depth details about the `vite-plugin-wat2wasm` lib
|
|
|
83
83
|
|
|
84
84
|
### `wat2WasmPlugin`
|
|
85
85
|
|
|
86
|
-
Enables compilation of `.wat` files and generation of
|
|
86
|
+
Enables compilation of `.wat` files and generation of WebAssembly, with modifiable settings.
|
|
87
87
|
|
|
88
88
|
`wat2WasmPlugin(options:` [`Wat2WasmOptions`](#wat2wasmoptions)`):`[`Plugin`](<https://vite.dev/guide/api-plugin>)
|
|
89
89
|
|
|
@@ -101,10 +101,10 @@ The configuration settings for `vite-plugin-wat2wasm`.
|
|
|
101
101
|
|
|
102
102
|
#### Properties
|
|
103
103
|
|
|
104
|
-
- `
|
|
105
|
-
Configures `.wasm` features you wish to enable for `vite-plugin-wat2wasm`.
|
|
104
|
+
- `inlineAssemblies?:` `boolean` `= false` - Whether to inline generated WebAssembly output within JavaScript files when transforming `.wat` files.
|
|
106
105
|
|
|
107
|
-
- `
|
|
106
|
+
- `parser?:` [`WasmParserOptions`](#wasmparseroptions) `= {}` - Configures WebAssembly features you wish to enable for `vite-plugin-wat2wasm`.
|
|
107
|
+
- `generator?:` [`WasmGeneratorOptions`](#wasmgeneratoroptions) `= {}` - Configures how `vite-plugin-wat2wasm` to generate WebAssembly output.
|
|
108
108
|
|
|
109
109
|
### `WasmParserOptions`
|
|
110
110
|
|
package/dist/index.d.ts
CHANGED
|
@@ -9,12 +9,14 @@ type WasmParserOptions = Parameters<WabtParserFunc>[2];
|
|
|
9
9
|
type WasmGeneratorOptions = Parameters<ReturnType<WabtParserFunc>["toBinary"]>[0];
|
|
10
10
|
/** The configuration settings for `vite-plugin-wat2wasm`. @see Wat2WasmOptions */
|
|
11
11
|
interface Wat2WasmOptions {
|
|
12
|
-
/**
|
|
12
|
+
/** Whether to inline generated WebAssembly output within JavaScript files when transforming `.wat` files. */
|
|
13
|
+
inlineAssemblies?: boolean;
|
|
14
|
+
/** Configures WebAssembly features you wish to enable for `vite-plugin-wat2wasm`. @default {} @see {@link WasmParserOptions|`WasmParserOptions`} */
|
|
13
15
|
parser?: WasmParserOptions;
|
|
14
|
-
/** Configures how `vite-plugin-wat2wasm` to generate
|
|
16
|
+
/** Configures how `vite-plugin-wat2wasm` to generate WebAssembly output. @default {} @see {@link WasmGeneratorOptions|`WasmGeneratorOptions`} */
|
|
15
17
|
generator?: WasmGeneratorOptions;
|
|
16
18
|
}
|
|
17
|
-
/** Enables compilation of `.wat` files and generation of
|
|
19
|
+
/** Enables compilation of `.wat` files and generation of WebAssembly, with modifiable settings.
|
|
18
20
|
*
|
|
19
21
|
* @param options - the configuration options for `vite-plugin-wat2wasm`. @see {@link Wat2WasmOptions|`Wat2WasmOptions`}
|
|
20
22
|
* @returns a Vite plugin object that allows for compilation of `.wat` files. @see {@link https://vite.dev/guide/api-plugin|`Plugin`}
|
package/dist/index.js
CHANGED
|
@@ -1,4 +1,16 @@
|
|
|
1
|
-
import e from"wabt";const
|
|
1
|
+
import e from"path";import t from"wabt";const n=await t();var r=(t={})=>{let{inlineAssemblies:r=!1,parser:i={},generator:a={}}=t,o,s,c={},l=(e,t)=>r||s?`
|
|
2
|
+
export default async (imports = {}) => {
|
|
3
|
+
const data = atob("${btoa(String.fromCharCode(...e))}");
|
|
2
4
|
|
|
3
|
-
|
|
5
|
+
const len = data.length;
|
|
6
|
+
|
|
7
|
+
const bfr = new Uint8Array(len);
|
|
8
|
+
for (let i = 0; i < len; i++) bfr[i] = data.charCodeAt(i);
|
|
9
|
+
|
|
10
|
+
return WebAssembly.instantiate(bfr).then((src) => src.instance.exports);
|
|
11
|
+
};
|
|
12
|
+
`:`
|
|
13
|
+
export default (imports = {}) => WebAssembly.instantiateStreaming(fetch(new URL("${t}", import.meta.url))).then((src) => src.instance.exports);
|
|
14
|
+
`;return{name:`wat2wasm`,enforce:`pre`,transform(t,r){if(!r.endsWith(`.wat`))return null;r=r.replaceAll(`\\`,`/`);let s=e.relative(o,r),u=e.basename(r,`.wat`),d=n.parseWat(u+`.wat`,t,i).toBinary(a);a.log&&console.log(`\x1B[1;35m[plugin-wat2wasm]\x1B[0;39m \x1B[36mLog Output\x1B[39m - \x1B[92m`+s+`
|
|
15
|
+
\x1B[39m`+d.log);let f=d.buffer;return c[r]=f,l(f,u+`.wasm`)},generateBundle(t,n){if(!r)for(let[t,r]of Object.entries(n)){if(r.type===`asset`)continue;t=t.replaceAll(`\\`,`/`);let n=e.join(t,`../`),i=r.moduleIds;for(let t of i){if(t=t.replaceAll(`\\`,`/`),!(t in c))continue;let r=e.basename(t,`.wat`)+`.wasm`,i=e.join(n,r),a=c[t];this.emitFile({type:`asset`,source:a,fileName:i})}}},configResolved(e){o=e.root.replaceAll(`\\`,`/`),s=e.command===`serve`}}};export{r as default};
|
|
4
16
|
//# sourceMappingURL=index.js.map
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
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 /**
|
|
1
|
+
{"version":3,"file":"index.js","names":["root: string","isServing: boolean","filesToEmit: Record<string, Uint8Array>"],"sources":["../src/index.ts"],"sourcesContent":["import path from \"path\";\r\n\r\nimport 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 /** Whether to inline generated WebAssembly output within JavaScript files when transforming `.wat` files. */\r\n inlineAssemblies?: boolean;\r\n\r\n /** Configures WebAssembly 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 WebAssembly output. @default {} @see {@link WasmGeneratorOptions|`WasmGeneratorOptions`} */\r\n generator?: WasmGeneratorOptions;\r\n}\r\n\r\n/** Enables compilation of `.wat` files and generation of WebAssembly, 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 inlineAssemblies = false,\r\n\r\n parser: parserOptions = {},\r\n generator: generatorOptions = {},\r\n } = options;\r\n\r\n let root: string;\r\n let isServing: boolean;\r\n\r\n const filesToEmit: Record<string, Uint8Array> = {};\r\n\r\n const transformWasm = (bfr: Uint8Array, filename: string) => inlineAssemblies || isServing\r\n ?\r\n `\r\n export default async (imports = {}) => {\r\n const data = atob(\"${btoa(String.fromCharCode(...bfr))}\");\r\n\r\n const len = data.length;\r\n\r\n const bfr = new Uint8Array(len);\r\n for (let i = 0; i < len; i++) bfr[i] = data.charCodeAt(i);\r\n\r\n return WebAssembly.instantiate(bfr).then((src) => src.instance.exports);\r\n };\r\n `\r\n :\r\n `\r\n export default (imports = {}) => WebAssembly.instantiateStreaming(fetch(new URL(\"${filename}\", import.meta.url))).then((src) => src.instance.exports);\r\n `;\r\n\r\n return {\r\n name: \"wat2wasm\",\r\n enforce: \"pre\",\r\n\r\n transform(code: string, pathId: string) {\r\n if (!pathId.endsWith(\".wat\")) return null;\r\n\r\n pathId = pathId.replaceAll(\"\\\\\", \"/\");\r\n\r\n const pathRel = path.relative(root, pathId);\r\n const basename = path.basename(pathId, \".wat\");\r\n\r\n const module = wabt.parseWat(basename + \".wat\", code, parserOptions);\r\n\r\n const output = module.toBinary(generatorOptions);\r\n if (generatorOptions.log) console.log(\"\\x1b[1;35m[plugin-wat2wasm]\\x1b[0;39m \\x1b[36mLog Output\\x1b[39m - \\x1b[92m\" + pathRel + \"\\n\" + \"\\x1b[39m\" + output.log);\r\n\r\n const bfr = output.buffer;\r\n\r\n filesToEmit[pathId] = bfr;\r\n\r\n return transformWasm(bfr, basename + \".wasm\");\r\n },\r\n\r\n generateBundle(_options, bundles) {\r\n if (inlineAssemblies) return;\r\n\r\n for (let [pathBundle, bundle] of Object.entries(bundles)) {\r\n if (bundle.type === \"asset\") continue;\r\n\r\n pathBundle = pathBundle.replaceAll(\"\\\\\", \"/\");\r\n\r\n const pathParent = path.join(pathBundle, \"../\");\r\n\r\n const modules = bundle.moduleIds;\r\n for (let pathModule of modules) {\r\n pathModule = pathModule.replaceAll(\"\\\\\", \"/\");\r\n if (!(pathModule in filesToEmit)) continue;\r\n\r\n const filename = path.basename(pathModule, \".wat\") + \".wasm\";\r\n const pathEmittedFile = path.join(pathParent, filename);\r\n\r\n const bfr = filesToEmit[pathModule]!;\r\n\r\n this.emitFile({\r\n type: \"asset\",\r\n\r\n source: bfr,\r\n fileName: pathEmittedFile\r\n });\r\n }\r\n }\r\n },\r\n\r\n configResolved(config) {\r\n root = config.root.replaceAll(\"\\\\\", \"/\");\r\n isServing = config.command === \"serve\";\r\n }\r\n };\r\n};\r\n\r\nexport default wat2WasmPlugin;\r\nexport type { Wat2WasmOptions, WasmParserOptions, WasmGeneratorOptions };\r\n"],"mappings":"wCAKA,MAAM,EAAO,MAAM,GAAU,CAwH7B,IAAA,GA7FwB,EAA2B,EAAE,GAAa,CAC9D,GAAM,CACF,mBAAmB,GAEnB,OAAQ,EAAgB,EAAE,CAC1B,UAAW,EAAmB,EAAE,EAChC,EAEAA,EACAC,EAEEC,EAA0C,EAAE,CAE5C,GAAiB,EAAiB,IAAqB,GAAoB,EAE7E;;iCAEyB,KAAK,OAAO,aAAa,GAAG,EAAI,CAAC,CAAC;;;;;;;;;UAW3D;2FACmF,EAAS;UAGhG,MAAO,CACH,KAAM,WACN,QAAS,MAET,UAAU,EAAc,EAAgB,CACpC,GAAI,CAAC,EAAO,SAAS,OAAO,CAAE,OAAO,KAErC,EAAS,EAAO,WAAW,KAAM,IAAI,CAErC,IAAM,EAAU,EAAK,SAAS,EAAM,EAAO,CACrC,EAAW,EAAK,SAAS,EAAQ,OAAO,CAIxC,EAFS,EAAK,SAAS,EAAW,OAAQ,EAAM,EAAc,CAE9C,SAAS,EAAiB,CAC5C,EAAiB,KAAK,QAAQ,IAAI,8EAAgF,EAAU;UAAoB,EAAO,IAAI,CAE/J,IAAM,EAAM,EAAO,OAInB,MAFA,GAAY,GAAU,EAEf,EAAc,EAAK,EAAW,QAAQ,EAGjD,eAAe,EAAU,EAAS,CAC1B,MAEJ,IAAK,GAAI,CAAC,EAAY,KAAW,OAAO,QAAQ,EAAQ,CAAE,CACtD,GAAI,EAAO,OAAS,QAAS,SAE7B,EAAa,EAAW,WAAW,KAAM,IAAI,CAE7C,IAAM,EAAa,EAAK,KAAK,EAAY,MAAM,CAEzC,EAAU,EAAO,UACvB,IAAK,IAAI,KAAc,EAAS,CAE5B,GADA,EAAa,EAAW,WAAW,KAAM,IAAI,CACzC,EAAE,KAAc,GAAc,SAElC,IAAM,EAAW,EAAK,SAAS,EAAY,OAAO,CAAG,QAC/C,EAAkB,EAAK,KAAK,EAAY,EAAS,CAEjD,EAAM,EAAY,GAExB,KAAK,SAAS,CACV,KAAM,QAEN,OAAQ,EACR,SAAU,EACb,CAAC,IAKd,eAAe,EAAQ,CACnB,EAAO,EAAO,KAAK,WAAW,KAAM,IAAI,CACxC,EAAY,EAAO,UAAY,SAEtC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "vite-plugin-wat2wasm",
|
|
3
|
-
"version": "2.
|
|
3
|
+
"version": "2.1.0",
|
|
4
4
|
"description": "A simple Vite plugin that enables `.wat` (WebAssembly Text Format) compilation and allows usage of WebAssembly within your codebase/library",
|
|
5
5
|
|
|
6
6
|
"homepage": "https://github.com/osoclos/vite-plugin-wat2wasm",
|