vite-svg-vue-component
Version:
Use svg as vue components with support for both vue2.7 and vue3.x
118 lines (114 loc) • 4.46 kB
JavaScript
;
var __create = Object.create;
var __defProp = Object.defineProperty;
var __defProps = Object.defineProperties;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getOwnPropSymbols = Object.getOwnPropertySymbols;
var __getProtoOf = Object.getPrototypeOf;
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 __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);
// src/index.ts
var src_exports = {};
__export(src_exports, {
PLUGIN_NAME: () => PLUGIN_NAME,
default: () => src_default
});
module.exports = __toCommonJS(src_exports);
var import_node_fs = require("fs");
var import_pluginutils = require("@rollup/pluginutils");
var import_svgo = require("svgo");
// src/utils.ts
var import_node_path = require("path");
var import_local_pkg = require("local-pkg");
async function compileSvg(svg, path, options) {
const version = options.vueVersion || detectVueVersion();
if (version === 2) {
const { compileTemplate } = await import("compiler-sfc-v2");
const { code } = compileTemplate({
source: svg.replace("<svg", '<svg v-on="$listeners"'),
filename: `${(0, import_node_path.basename)(path)}.vue`,
transformAssetUrls: false
});
return code;
} else {
const { compileTemplate } = await import("compiler-sfc-v3");
const { code } = compileTemplate({
id: path,
source: svg,
filename: `${(0, import_node_path.basename)(path)}.vue`,
transformAssetUrls: false
});
return code;
}
}
function detectVueVersion(root = process.cwd()) {
const vuePkg = (0, import_local_pkg.getPackageInfoSync)("vue", { paths: [root] });
if (vuePkg && vuePkg.version)
return Number.parseInt(vuePkg.version);
else
return 3;
}
// src/index.ts
var PLUGIN_NAME = "vite-svg-vue-component";
function createPlugin(options = {}) {
const filter = (0, import_pluginutils.createFilter)(
options.include || [/\.svg(\?component)?$/],
options.exclude || [/[\\/]node_modules[\\/]/, /[\\/]\.git[\\/]/]
);
return {
name: PLUGIN_NAME,
enforce: "pre",
async transform(_code, id) {
if (!filter(id))
return;
const [path] = id.split("?", 2);
let svg = (0, import_node_fs.readFileSync)(path, { encoding: "utf-8" });
if (options.optimize)
svg = (0, import_svgo.optimize)(svg, typeof options.optimize === "object" ? __spreadProps(__spreadValues({}, options.optimize), { path }) : { path }).data;
const code = await compileSvg(svg, path, options);
return `${code}
export default { render };`;
}
};
}
var src_default = createPlugin;
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
PLUGIN_NAME
});