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 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 `.wasm`, with modifiable settings.
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
- - `parser?:` [`WasmParserOptions`](#wasmparseroptions) `= {}` -
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
- - `generator?:` [`WasmGeneratorOptions`](#wasmgeneratoroptions) `= {}` - Configures how `vite-plugin-wat2wasm` to generate `.wasm` files.
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
- /** Configures `.wasm` features you wish to enable for `vite-plugin-wat2wasm`. @default {} @see {@link WasmParserOptions|`WasmParserOptions`} */
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 `.wasm` files. @default {} @see {@link WasmGeneratorOptions|`WasmGeneratorOptions`} */
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 `.wasm`, with modifiable settings.
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 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+`
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
- `,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};
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 /** 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"}
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,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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",