UNPKG

rollup-plugin-caveman

Version:

Converts Caveman templates to ES6 modules

138 lines (134 loc) 4.95 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( // If the importer is in node compatibility mode or this is not an ESM // file that has been converted to a CommonJS file using a Babel- // compatible transform (i.e. "__esModule" has not been set), then set // "default" to the CommonJS "module.exports" for node compatibility. isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target, mod )); var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); // index.ts var rollup_plugin_caveman_exports = {}; __export(rollup_plugin_caveman_exports, { default: () => caveman }); module.exports = __toCommonJS(rollup_plugin_caveman_exports); var import_lodash = __toESM(require("lodash.camelcase")); var import_caveman = __toESM(require("caveman")); var import_promises = require("fs/promises"); var import_pluginutils = require("@rollup/pluginutils"); var import_path = require("path"); function toES6Module(content) { return ` import Caveman from "caveman" export function render(d = {}) { ${content} } export default { render }; `; } async function getValidPath(path) { await (0, import_promises.access)(path); return path; } function getPartialPath(fileName, paths) { return Promise.any(paths.map((path) => getValidPath((0, import_path.join)(path, fileName)))); } function caveman({ include = "**/*.html?caveman", exclude, partialPaths = [] } = {}) { const filter = (0, import_pluginutils.createFilter)(include, exclude); const postfixRE = /[?#].*$/s; const partialNameMap = /* @__PURE__ */ new Map(); const partialPathCache = /* @__PURE__ */ new Map(); return { name: "caveman", async resolveId(id, importer) { const extension = (0, import_path.extname)(id); if (!extension.includes(".html")) return null; const postfix = id.match(postfixRE)?.[0] ?? ""; const filePath = id.replace(postfixRE, ""); const result = await this.resolve(filePath, importer); if (!result) return result; return result.id + postfix; }, async load(id) { const filePath = id.replace(postfixRE, ""); const extension = (0, import_path.extname)(filePath); if (extension !== ".html" || !filter(id)) return; const code = await (0, import_promises.readFile)(filePath, "utf8"); let compiled = toES6Module(import_caveman.default.compile(code)); const hasPartials = /\b_Cr\('/.test(compiled); if (!hasPartials) { return { code: compiled, map: { mappings: "" } }; } compiled = compiled.replace( /\b_Cr\('([^']+)',/g, (match, partialName) => { if (!partialName) return match; const importName = (0, import_lodash.default)(partialName); partialNameMap.set(partialName, importName); return `${importName}.render(`; } ); const postfix = id.match(postfixRE)?.[0] ?? ""; const partialLookupPaths = [ (0, import_path.dirname)(filePath), // Always look for partials in the same directory as the template ...partialPaths ]; let partialImports = []; try { partialImports = await Promise.all( Array.from(partialNameMap, async ([partialName, importName]) => { let partialPath = partialPathCache.get(partialName); if (!partialPath) { partialPath = await getPartialPath( partialName + extension, partialLookupPaths ); partialPathCache.set(partialName, partialPath); } return `import ${importName} from "${partialPath}${postfix}"`; }) ); } catch (e) { this.error({ message: "Unable to find caveman partial", cause: e }); } const importBlock = partialImports.join("\n\n"); let output = importBlock + "\n\n" + compiled; return { code: output, map: { mappings: "" } }; } }; }