rollup-plugin-inline-svg
Version:
Support for inlining SVG files for Rollup
104 lines (103 loc) • 3.95 kB
JavaScript
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));
import { createFilter } from "@rollup/pluginutils";
import { parse, stringify } from "html-to-ast";
const markAsUnwrapped = (node) => {
node.attrs = {};
node.name = "element-marked-to-remove";
};
const markAsRemoved = (node) => {
markAsUnwrapped(node);
node.children = [];
};
const findSvgNode = (node) => {
let svgAst = null;
SvgProcessor.traverse(node, (node2) => {
var _a, _b;
const name = (_b = (_a = node2.name) == null ? void 0 : _a.toLowerCase()) != null ? _b : "";
if (name === "svg") {
svgAst = node2;
return true;
}
return false;
});
return svgAst;
};
const findSvgNodeInArray = (nodes) => {
for (const node of nodes) {
const ast = findSvgNode(node);
if (ast)
return ast;
}
return null;
};
class SvgProcessor {
static process(code, options) {
const ast = parse(code.trim());
const svgAst = findSvgNodeInArray(ast);
if (svgAst == null) {
throw new Error(`rollup-plugin-inline-svg: file ${options.fileName} is not a valid svg. The 'svg' node was not found.`);
}
const requiresTraverse = options.forbidden !== void 0 || options.traverse !== void 0;
if (requiresTraverse) {
SvgProcessor.traverse(svgAst, (node) => {
var _a, _b, _c, _d, _e, _f, _g, _h, _i;
if (((_a = options == null ? void 0 : options.forbidden) == null ? void 0 : _a.attrs) !== void 0 && node.attrs !== void 0) {
Object.keys(node.attrs).forEach((attrName) => {
if (options.forbidden.attrs.indexOf(attrName) >= 0)
delete node.attrs[attrName];
});
}
if (((_e = (_d = (_b = options == null ? void 0 : options.forbidden) == null ? void 0 : _b.tags) == null ? void 0 : _d.indexOf((_c = node.name) != null ? _c : "")) != null ? _e : -1) >= 0) {
markAsUnwrapped(node);
}
if (((_h = (_g = (_f = options == null ? void 0 : options.forbidden) == null ? void 0 : _f.tags) == null ? void 0 : _g.indexOf(node.name + ":remove")) != null ? _h : -1) >= 0) {
markAsRemoved(node);
}
(_i = options == null ? void 0 : options.traverse) == null ? void 0 : _i.call(options, node);
});
}
let transformedCode = stringify([svgAst]);
if (requiresTraverse) {
transformedCode = transformedCode.replace(/<\/?element-marked-to-remove>/g, "");
}
return transformedCode;
}
static traverse(node, callbackFn) {
if (!callbackFn(node) && node.children)
node.children.forEach((child) => this.traverse(child, callbackFn));
}
}
const defaultOptions = {
include: ["**/*.svg"]
};
function InlineSvg(options = {}) {
options = __spreadValues(__spreadValues({}, defaultOptions), options);
const filter = createFilter(options.include, options.exclude);
return {
name: "InlineSvg",
transform(code, id) {
if (!filter(id))
return;
return { code: `export default '${SvgProcessor.process(code, __spreadProps(__spreadValues({}, options), { fileName: id })).replace(/\n/g, "")}'`, map: { mappings: "" } };
}
};
}
export { InlineSvg as default };