UNPKG

vite-plugin-wat

Version:

A Vite plugin for WebAssembly Text Format.

78 lines (74 loc) 2.77 kB
"use strict"; var __create = Object.create; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __getProtoOf = Object.getPrototypeOf; var __hasOwnProp = Object.prototype.hasOwnProperty; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps( isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target, mod )); var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); // src/index.ts var src_exports = {}; __export(src_exports, { default: () => src_default }); module.exports = __toCommonJS(src_exports); var import_pathe = require("pathe"); var import_fs_extra = __toESM(require("fs-extra"), 1); // src/wabt.ts var import_wabt = __toESM(require("wabt"), 1); async function wat2wasm(wat) { const wabtModule = await (0, import_wabt.default)(); const wasmModule = wabtModule.parseWat("", wat); return wasmModule.toBinary({}).buffer; } // src/index.ts var WAT_ID_REG = /\.wat\?init$/; function isWatId(id) { return WAT_ID_REG.test(id); } function resolveWasmId(watId, cwd, wasmDir) { return (0, import_pathe.resolve)(wasmDir, (0, import_pathe.relative)(cwd, watId).replace(WAT_ID_REG, ".wasm")); } var src_default = () => { const cwd = (0, import_pathe.resolve)("./"); const storeDir = (0, import_pathe.resolve)(cwd, "node_modules", ".vite-plugin-wat"); const wasmDir = (0, import_pathe.resolve)(storeDir, "wasm"); return { name: "vite-plugin-wat", config: () => ({ server: { watch: { ignored: [wasmDir] } } }), buildStart: () => { import_fs_extra.default.removeSync(wasmDir); }, resolveId: (id) => { return isWatId(id) ? id : void 0; }, transform: async (code, id) => { if (!isWatId(id)) return; const wasmId = resolveWasmId(id, cwd, wasmDir); await import_fs_extra.default.ensureFile(wasmId); await import_fs_extra.default.writeFile(wasmId, await wat2wasm(code)); return `export { default } from '${wasmId}?init';`; } }; }; // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = {});