UNPKG

malinajs-unplugin

Version:
89 lines (87 loc) 2.81 kB
var __defProp = Object.defineProperty; var __defProps = Object.defineProperties; var __getOwnPropDescs = Object.getOwnPropertyDescriptors; var __getOwnPropSymbols = Object.getOwnPropertySymbols; var __hasOwnProp = Object.prototype.hasOwnProperty; var __propIsEnum = Object.prototype.propertyIsEnumerable; var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value; var __spreadValues = (a, b) => { for (var prop in b || (b = {})) if (__hasOwnProp.call(b, prop)) __defNormalProp(a, prop, b[prop]); if (__getOwnPropSymbols) for (var prop of __getOwnPropSymbols(b)) { if (__propIsEnum.call(b, prop)) __defNormalProp(a, prop, b[prop]); } return a; }; var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b)); var __require = typeof require !== "undefined" ? require : (x) => { throw new Error('Dynamic require of "' + x + '" is not supported'); }; // src/index.ts import { createUnplugin } from "unplugin"; import malina from "malinajs"; var unplugin = createUnplugin((options) => { if (!options) options = {}; if (!options.extensions) options.extensions = ["ma", "xht"]; if (options.displayVersion) console.log("! Malina.js", malina.version); let content_cache = {}; return { name: "malinajs", transformInclude(id) { return options.extensions.some((extension) => id.endsWith("." + extension)); }, async transform(code, id) { var _a; const compileOptions = __spreadProps(__spreadValues({}, options), { path: id, name: id.match(/([^\/\\]+)\.\w+$/)[1] }); try { const ctx = await malina.compile(code, compileOptions); let result = ctx.result; if ((_a = ctx == null ? void 0 : ctx.css) == null ? void 0 : _a.result) { const name = id.replace(/[^\w.\\-]/g, "") + ".malina.css"; content_cache[name] = ctx.css.result; result += ` import "${name}"; `; } return result; } catch (error) { if (options == null ? void 0 : options.warning) { if (typeof error === "string") { options.warning(error); } else if (error instanceof Error) { options.warning(error.message); } } throw error; } }, async resolveId(name) { if (content_cache[name]) return name; if (name === "malinajs") return await __require("malinajs/runtime"); return null; }, load(id) { return content_cache[id] || null; } }; }); var vitePlugin = unplugin.vite; var rollupPlugin = unplugin.rollup; var webpackPlugin = unplugin.webpack; export { rollupPlugin, unplugin, vitePlugin, webpackPlugin };