@maizzle/framework
Version:
Maizzle is a framework that helps you quickly build HTML emails with Tailwind CSS.
45 lines (44 loc) • 1.67 kB
JavaScript
//#region src/render/plugins/rowSourceLocation.ts
/**
* Vite plugin that injects `data-maizzle-loc="<file>:<line>"` into every
* `<Row>`/`<row>` opening tag in user templates.
*
* Used by Row.vue's runtime to point the user at the exact line in their
* template when they misuse Row (e.g. without a Column child).
*
* Only transforms inside `<template>` blocks of SFCs (or the entire file
* for `.md` templates) so `<Row>` mentions in `<script>` blocks (e.g. in
* string literals or comments) are left untouched.
*/
function rowSourceLocation() {
const tagRe = /(<(?:Row|row))(\b[^>]*?)(\/?>)/g;
function injectLoc(html, htmlOffset, fullCode, id) {
return html.replace(tagRe, (match, tag, attrs, end, localOffset) => {
if (/\bdata-maizzle-loc\s*=/.test(attrs)) return match;
const absoluteOffset = htmlOffset + localOffset;
return `${tag}${attrs} data-maizzle-loc="${id}:${fullCode.slice(0, absoluteOffset).split("\n").length}"${end}`;
});
}
return {
name: "maizzle:row-loc",
enforce: "pre",
transform(code, id) {
const isVue = id.endsWith(".vue");
const isMd = id.endsWith(".md");
if (!isVue && !isMd) return;
if (!code.includes("<Row") && !code.includes("<row")) return;
let transformed;
if (isVue) transformed = code.replace(/(<template\b[^>]*>)([\s\S]*?)(<\/template>)/g, (_match, open, inner, close, offset) => {
return open + injectLoc(inner, offset + open.length, code, id) + close;
});
else transformed = injectLoc(code, 0, code, id);
if (transformed !== code) return {
code: transformed,
map: null
};
}
};
}
//#endregion
export { rowSourceLocation };
//# sourceMappingURL=rowSourceLocation.js.map