vite-plugin-wat2wasm 2.1.1 → 2.1.2

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/dist/index.d.ts CHANGED
@@ -20,6 +20,7 @@ interface Wat2WasmOptions {
20
20
  /** The directory path where utility functions used by JavaScript files to retrieve and interact with WebAssembly modules will be stored. */
21
21
  utilDirPath?: string;
22
22
  }
23
+ /** The runtimes supported as targets when fetching WebAssembly modules. */
23
24
  type FetchTarget = "browser" | "node";
24
25
  /** Enables compilation of `.wat` files and generation of WebAssembly, with modifiable settings.
25
26
  *
package/dist/index.js CHANGED
@@ -17,7 +17,7 @@ import e from"fs/promises";import t from"path";import n from"crypto";import r fr
17
17
  };
18
18
  `:`
19
19
  import { fetchWasm } from "${u}";
20
- export default (imports = {}) => fetchWasm("${t}", import.meta.url, imports).then((src) => src.instance.exports);
20
+ export default (imports = {}) => fetchWasm("${t.startsWith(`.`)?t:`./`+t}", import.meta.url, imports).then((src) => src.instance.exports);
21
21
  `;return{name:`wat2wasm`,enforce:`pre`,transform(e,n){if(!n.endsWith(`.wat`))return null;n=n.replaceAll(`\\`,`/`);let r=t.relative(f,n),a=t.basename(n,`.wat`),c=i.parseWat(a+`.wat`,e,o).toBinary(s);s.log&&console.log(`\x1B[1;35m[plugin-wat2wasm]\x1B[0;39m \x1B[36mLog Output\x1B[39m - \x1B[92m`+r+`
22
- \x1B[39m`+c.log);let l=c.buffer;return _[n]=l,b(l,a+`.wasm`)},generateBundle(e,n){if(!a)for(let[e,r]of Object.entries(n)){if(r.type===`asset`)continue;e=e.replaceAll(`\\`,`/`);let n=t.join(e,`../`),i=r.moduleIds;for(let e of i){if(e=e.replaceAll(`\\`,`/`),!(e in _))continue;let r=t.basename(e,`.wat`)+`.wasm`,i=t.join(n,r),a=_[e];this.emitFile({type:`asset`,source:a,fileName:i})}r.code=r.code.replaceAll(u,t.relative(n,d).replaceAll(`\\`,`/`))}},buildStart(){if(`data`in this){let e=this.data;f=(e.outputOptions.preserveModules?e.outputOptions.preserveModulesRoot:t.join(e.outputOptions.dir,`../`)).replaceAll(`\\`,`/`),p=e.outputOptions.dir.replaceAll(`\\`,`/`),m=!1}v=!(a||m)},async closeBundle(){if(v){let n=t.join(p,d).replaceAll(`\\`,`/`),r=t.join(n,`../`).replaceAll(`\\`,`/`);await e.mkdir(r,{recursive:!0}),await e.writeFile(n,y,`utf-8`)}},configResolved(e){f=e.root.replaceAll(`\\`,`/`),p=t.join(f,e.build.outDir).replaceAll(`\\`,`/`),m=e.command===`serve`}}};export{a as default};
22
+ \x1B[39m`+c.log);let l=c.buffer;return _[n]=l,b(l,a+`.wasm`)},generateBundle(e,n){if(!a)for(let[e,r]of Object.entries(n)){if(r.type===`asset`)continue;e=e.replaceAll(`\\`,`/`);let n=t.join(e,`../`),i=r.moduleIds;for(let e of i){if(e=e.replaceAll(`\\`,`/`),!(e in _))continue;let r=t.basename(e,`.wat`)+`.wasm`,i=t.join(n,r),a=_[e];this.emitFile({type:`asset`,source:a,fileName:i})}let a=t.relative(n,d).replaceAll(`\\`,`/`);r.code=r.code.replaceAll(u,a.startsWith(`.`)?a:`./`+a)}},buildStart(){if(`data`in this){let e=this.data;f=(e.outputOptions.preserveModules?e.outputOptions.preserveModulesRoot:t.join(e.outputOptions.dir,`../`)).replaceAll(`\\`,`/`),p=e.outputOptions.dir.replaceAll(`\\`,`/`),m=!1}v=!(a||m)},async closeBundle(){if(v){let n=t.join(p,d).replaceAll(`\\`,`/`),r=t.join(n,`../`).replaceAll(`\\`,`/`);await e.mkdir(r,{recursive:!0}),await e.writeFile(n,y,`utf-8`)}},configResolved(e){f=e.root.replaceAll(`\\`,`/`),p=t.join(f,e.build.outDir).replaceAll(`\\`,`/`),m=e.command===`serve`}}};export{a as default};
23
23
  //# sourceMappingURL=index.js.map
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","names":["root: string","rootDist: string","isServing: boolean","filesToEmit: Record<string, Uint8Array>","fetchWasmEmit: boolean","fetchWasmFunc: string"],"sources":["../src/index.ts"],"sourcesContent":["import fs from \"fs/promises\";\r\nimport path from \"path\";\r\n\r\nimport crypto from \"crypto\";\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 /** Determines the runtimes that can be targeted when the WebAssembly modules are fetched. Does not affect anything if `inlineAssemblies` is set to `true`. */\r\n fetchTargets?: FetchTarget | FetchTarget[];\r\n\r\n /** The directory path where utility functions used by JavaScript files to retrieve and interact with WebAssembly modules will be stored. */\r\n utilDirPath?: string;\r\n}\r\n\r\ntype FetchTarget = \"browser\" | \"node\";\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\r\n fetchTargets = [\"browser\", \"node\"],\r\n utilDirPath = \"./__wasm-utils\"\r\n } = options;\r\n\r\n const PLUGIN_ID: string = \"wat2wasm\";\r\n const FETCH_WASM_ID = PLUGIN_ID + \":\" + crypto.randomBytes(4).toString(\"hex\");\r\n\r\n const FETCH_WASM_PATH = path.join(utilDirPath, \"fetchWasm.js\");\r\n\r\n let root: string;\r\n let rootDist: string;\r\n\r\n let isServing: boolean;\r\n\r\n const targetsBrowser = fetchTargets.includes(\"browser\");\r\n const targetsNode = fetchTargets.includes(\"node\" );\r\n\r\n const filesToEmit: Record<string, Uint8Array> = {};\r\n\r\n let fetchWasmEmit: boolean = false;\r\n const fetchWasmFunc: string =\r\n `export async function fetchWasm(filename, parentPath, imports) {` + \"\\n\" +\r\n (targetsBrowser ? ` if (typeof window !== \"undefined\" && typeof document !== \"undefined\") return WebAssembly.instantiateStreaming(fetch(new URL(filename, parentPath)), imports);` : \"\") + \"\\n\" +\r\n (targetsNode ? ` if (typeof process !== \"undefined\" && \"versions\" in process && \"node\" in process.versions) return WebAssembly.instantiate(await (await import(\"fs/promises\").then(({ readFile }) => readFile))(filename), imports);` : \"\") + \"\\n\" +\r\n \"\\n\" +\r\n ` throw new Error(\"The runtime used to import this WebAssembly module is not supported. If this is a mistake, adjust your fetchTargets to fit the specific runtime.\");` + \"\\n\" +\r\n `}` + \"\\n\";\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 import { fetchWasm } from \"${FETCH_WASM_ID}\";\r\n export default (imports = {}) => fetchWasm(\"${filename}\", import.meta.url, imports).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 bundle.code = bundle.code.replaceAll(FETCH_WASM_ID, path.relative(pathParent, FETCH_WASM_PATH).replaceAll(\"\\\\\", \"/\"));\r\n }\r\n },\r\n\r\n buildStart() {\r\n if (\"data\" in this) {\r\n const data = this.data as any;\r\n\r\n root = (data.outputOptions.preserveModules ? data.outputOptions.preserveModulesRoot : path.join(data.outputOptions.dir, \"../\")).replaceAll(\"\\\\\", \"/\");\r\n rootDist = data.outputOptions.dir.replaceAll(\"\\\\\", \"/\");\r\n\r\n isServing = false;\r\n }\r\n\r\n fetchWasmEmit = !(inlineAssemblies || isServing);\r\n },\r\n\r\n async closeBundle() {\r\n if (fetchWasmEmit) {\r\n const fetchWasmPath = path.join(rootDist, FETCH_WASM_PATH).replaceAll(\"\\\\\", \"/\");\r\n const fetchWasmParent = path.join(fetchWasmPath, \"../\").replaceAll(\"\\\\\", \"/\");\r\n\r\n await fs.mkdir(fetchWasmParent, { recursive: true });\r\n await fs.writeFile(fetchWasmPath, fetchWasmFunc, \"utf-8\");\r\n }\r\n },\r\n\r\n configResolved(config) {\r\n root = config.root.replaceAll(\"\\\\\", \"/\");\r\n rootDist = path.join(root, config.build.outDir).replaceAll(\"\\\\\", \"/\");\r\n\r\n isServing = config.command === \"serve\";\r\n }\r\n };\r\n};\r\n\r\nexport default wat2WasmPlugin;\r\nexport type { Wat2WasmOptions, WasmParserOptions, WasmGeneratorOptions, FetchTarget };\r\n"],"mappings":"yFAQA,MAAM,EAAO,MAAM,GAAU,CAkL7B,IAAA,GA/IwB,EAA2B,EAAE,GAAa,CAC9D,GAAM,CACF,mBAAmB,GAEnB,OAAQ,EAAgB,EAAE,CAC1B,UAAW,EAAmB,EAAE,CAEhC,eAAe,CAAC,UAAW,OAAO,CAClC,cAAc,kBACd,EAGE,EAAkB,YAAkB,EAAO,YAAY,EAAE,CAAC,SAAS,MAAM,CAEzE,EAAkB,EAAK,KAAK,EAAa,eAAe,CAE1DA,EACAC,E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SAAU,EACb,CAAC,CAGN,EAAO,KAAO,EAAO,KAAK,WAAW,EAAe,EAAK,SAAS,EAAY,EAAgB,CAAC,WAAW,KAAM,IAAI,CAAC,GAI7H,YAAa,CACT,GAAI,SAAU,KAAM,CAChB,IAAM,EAAO,KAAK,KAElB,GAAQ,EAAK,cAAc,gBAAkB,EAAK,cAAc,oBAAsB,EAAK,KAAK,EAAK,cAAc,IAAK,MAAM,EAAE,WAAW,KAAM,IAAI,CACrJ,EAAW,EAAK,cAAc,IAAI,WAAW,KAAM,IAAI,CAEvD,EAAY,GAGhB,EAAgB,EAAE,GAAoB,IAG1C,MAAM,aAAc,CAChB,GAAI,EAAe,CACf,IAAM,EAAgB,EAAK,KAAK,EAAU,EAAgB,CAAC,WAAW,KAAM,IAAI,CAC1E,EAAkB,EAAK,KAAK,EAAe,MAAM,CAAC,WAAW,KAAM,IAAI,CAE7E,MAAM,EAAG,MAAM,EAAiB,CAAE,UAAW,GAAM,CAAC,CACpD,MAAM,EAAG,UAAU,EAAe,EAAe,QAAQ,GAIjE,eAAe,EAAQ,CACnB,EAAO,EAAO,KAAK,WAAW,KAAM,IAAI,CACxC,EAAW,EAAK,KAAK,EAAM,EAAO,MAAM,OAAO,CAAC,WAAW,KAAM,IAAI,CAErE,EAAY,EAAO,UAAY,SAEtC"}
1
+ {"version":3,"file":"index.js","names":["root: string","rootDist: string","isServing: boolean","filesToEmit: Record<string, Uint8Array>","fetchWasmEmit: boolean","fetchWasmFunc: string"],"sources":["../src/index.ts"],"sourcesContent":["import fs from \"fs/promises\";\r\nimport path from \"path\";\r\n\r\nimport crypto from \"crypto\";\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 /** Determines the runtimes that can be targeted when the WebAssembly modules are fetched. Does not affect anything if `inlineAssemblies` is set to `true`. */\r\n fetchTargets?: FetchTarget | FetchTarget[];\r\n\r\n /** The directory path where utility functions used by JavaScript files to retrieve and interact with WebAssembly modules will be stored. */\r\n utilDirPath?: string;\r\n}\r\n\r\n/** The runtimes supported as targets when fetching WebAssembly modules. */\r\ntype FetchTarget = \"browser\" | \"node\";\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\r\n fetchTargets = [\"browser\", \"node\"],\r\n utilDirPath = \"./__wasm-utils\"\r\n } = options;\r\n\r\n const PLUGIN_ID: string = \"wat2wasm\";\r\n const FETCH_WASM_ID = PLUGIN_ID + \":\" + crypto.randomBytes(4).toString(\"hex\");\r\n\r\n const FETCH_WASM_PATH = path.join(utilDirPath, \"fetchWasm.js\");\r\n\r\n let root: string;\r\n let rootDist: string;\r\n\r\n let isServing: boolean;\r\n\r\n const targetsBrowser = fetchTargets.includes(\"browser\");\r\n const targetsNode = fetchTargets.includes(\"node\" );\r\n\r\n const filesToEmit: Record<string, Uint8Array> = {};\r\n\r\n let fetchWasmEmit: boolean = false;\r\n const fetchWasmFunc: string =\r\n `export async function fetchWasm(filename, parentPath, imports) {` + \"\\n\" +\r\n (targetsBrowser ? ` if (typeof window !== \"undefined\" && typeof document !== \"undefined\") return WebAssembly.instantiateStreaming(fetch(new URL(filename, parentPath)), imports);` : \"\") + \"\\n\" +\r\n (targetsNode ? ` if (typeof process !== \"undefined\" && \"versions\" in process && \"node\" in process.versions) return WebAssembly.instantiate(await (await import(\"fs/promises\").then(({ readFile }) => readFile))(filename), imports);` : \"\") + \"\\n\" +\r\n \"\\n\" +\r\n ` throw new Error(\"The runtime used to import this WebAssembly module is not supported. If this is a mistake, adjust your fetchTargets to fit the specific runtime.\");` + \"\\n\" +\r\n `}` + \"\\n\";\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 import { fetchWasm } from \"${FETCH_WASM_ID}\";\r\n export default (imports = {}) => fetchWasm(\"${filename.startsWith(\".\") ? filename : \"./\" + filename}\", import.meta.url, imports).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 const fetchWasmPathRel = path.relative(pathParent, FETCH_WASM_PATH).replaceAll(\"\\\\\", \"/\");\r\n\r\n bundle.code = bundle.code.replaceAll(FETCH_WASM_ID, fetchWasmPathRel.startsWith(\".\") ? fetchWasmPathRel : \"./\" + fetchWasmPathRel);\r\n }\r\n },\r\n\r\n buildStart() {\r\n if (\"data\" in this) {\r\n const data = this.data as any;\r\n\r\n root = (data.outputOptions.preserveModules ? data.outputOptions.preserveModulesRoot : path.join(data.outputOptions.dir, \"../\")).replaceAll(\"\\\\\", \"/\");\r\n rootDist = data.outputOptions.dir.replaceAll(\"\\\\\", \"/\");\r\n\r\n isServing = false;\r\n }\r\n\r\n fetchWasmEmit = !(inlineAssemblies || isServing);\r\n },\r\n\r\n async closeBundle() {\r\n if (fetchWasmEmit) {\r\n const fetchWasmPath = path.join(rootDist, FETCH_WASM_PATH).replaceAll(\"\\\\\", \"/\");\r\n const fetchWasmParent = path.join(fetchWasmPath, \"../\").replaceAll(\"\\\\\", \"/\");\r\n\r\n await fs.mkdir(fetchWasmParent, { recursive: true });\r\n await fs.writeFile(fetchWasmPath, fetchWasmFunc, \"utf-8\");\r\n }\r\n },\r\n\r\n configResolved(config) {\r\n root = config.root.replaceAll(\"\\\\\", \"/\");\r\n rootDist = path.join(root, config.build.outDir).replaceAll(\"\\\\\", \"/\");\r\n\r\n isServing = config.command === \"serve\";\r\n }\r\n };\r\n};\r\n\r\nexport default wat2WasmPlugin;\r\nexport type { Wat2WasmOptions, WasmParserOptions, WasmGeneratorOptions, FetchTarget };\r\n"],"mappings":"yFAQA,MAAM,EAAO,MAAM,GAAU,CAqL7B,IAAA,GAjJwB,EAA2B,EAAE,GAAa,CAC9D,GAAM,CACF,mBAAmB,GAEnB,OAAQ,EAAgB,EAAE,CAC1B,UAAW,EAAmB,EAAE,CAEhC,eAAe,CAAC,UAAW,OAAO,CAClC,cAAc,kBACd,EAGE,EAAkB,YAAkB,EAAO,YAAY,EAAE,CAAC,SAAS,MAAM,CAEzE,EAAkB,EAAK,KAAK,EAAa,eAAe,CAE1DA,EACAC,EAEAC,EAEE,EAAiB,EAAa,SAAS,UAAU,CACjD,EAAiB,EAAa,SAAS,OAAU,CAEjDC,EAA0C,EAAE,CAE9CC,EAAyB,GACvBC,EACF;GACC,EAAiB,oKAAsK,IAAM;GAC7L,EAAiB,0NAA4N,IAAM;;;;EAKlP,GAAiB,EAAiB,IAAqB,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AiB,GAI1I,YAAa,CACT,GAAI,SAAU,KAAM,CAChB,IAAM,EAAO,KAAK,KAElB,GAAQ,EAAK,cAAc,gBAAkB,EAAK,cAAc,oBAAsB,EAAK,KAAK,EAAK,cAAc,IAAK,MAAM,EAAE,WAAW,KAAM,IAAI,CACrJ,EAAW,EAAK,cAAc,IAAI,WAAW,KAAM,IAAI,CAEvD,EAAY,GAGhB,EAAgB,EAAE,GAAoB,IAG1C,MAAM,aAAc,CAChB,GAAI,EAAe,CACf,IAAM,EAAgB,EAAK,KAAK,EAAU,EAAgB,CAAC,WAAW,KAAM,IAAI,CAC1E,EAAkB,EAAK,KAAK,EAAe,MAAM,CAAC,WAAW,KAAM,IAAI,CAE7E,MAAM,EAAG,MAAM,EAAiB,CAAE,UAAW,GAAM,CAAC,CACpD,MAAM,EAAG,UAAU,EAAe,EAAe,QAAQ,GAIjE,eAAe,EAAQ,CACnB,EAAO,EAAO,KAAK,WAAW,KAAM,IAAI,CACxC,EAAW,EAAK,KAAK,EAAM,EAAO,MAAM,OAAO,CAAC,WAAW,KAAM,IAAI,CAErE,EAAY,EAAO,UAAY,SAEtC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "vite-plugin-wat2wasm",
3
- "version": "2.1.1",
3
+ "version": "2.1.2",
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",