const IconfontWebpackPlugin = require("iconfont-webpack-plugin");
function tapPostcssLoaderOptions(config, moduleRule, rule) {
config.module
.rule(moduleRule)
.oneOf(rule)
.use("postcss-loader")
.loader("postcss-loader")
.tap(options => {
const iconFontOptions = {
plugins: loader => {
return [new IconfontWebpackPlugin(loader)];
},
};
Object.assign(options, iconFontOptions);
return options;
});
}
function configIconFont(config) {
const moduleRules = ["scss", "css"];
const rules = ["normal", "normal-modules", "vue", "vue-modules"];
for (let i = 0; i < moduleRules.length; i++) {
const moduleRule = moduleRules[i];
for (let i = 0; i < rules.length; i++) {
const rule = rules[i];
tapPostcssLoaderOptions(config, moduleRule, rule);
}
}
}
module.exports = {
chainWebpack: config => {
configIconFont(config);
},
};
function configSVGIcon(config) {
config.module
.rule("svg")
.exclude.add(path.resolve(__dirname, "./src/svg-icons"))
.end();
const options = {
plugins: [
{ cleanupAttrs: true },
{ cleanupEnableBackground: true },
{ cleanupIDs: true },
{ cleanupListOfValues: true },
{ cleanupNumericValues: true },
{ collapseGroups: true },
{ convertColors: true },
{ convertPathData: true },
{ convertShapeToPath: true },
{ convertStyleToAttrs: true },
{ convertTransform: true },
{ mergePaths: true },
{ removeComments: true },
{ removeDesc: true },
{ removeDimensions: true },
{ removeDoctype: true },
{ removeEditorsNSData: true },
{ removeEmptyAttrs: true },
{ removeEmptyContainers: true },
{ removeEmptyText: true },
{ removeHiddenElems: true },
{ removeMetadata: true },
{ removeNonInheritableGroupAttrs: true },
{ removeRasterImages: true },
{ removeTitle: true },
{ removeUnknownsAndDefaults: true },
{ removeUselessDefs: true },
{ removeUnusedNS: true },
{ removeUselessStrokeAndFill: true },
{
removeAttrs: { attrs: "fill" },
},
{ removeXMLProcInst: true },
{ removeStyleElement: true },
{ removeUnknownsAndDefaults: true },
{ sortAttrs: true },
],
};
config.module
.rule("svg-icon")
.test(/\.svg$/)
.include.add(path.resolve(__dirname, "./src/svg-icons"))
.end()
.use("svg-sprite-loader")
.loader("svg-sprite-loader")
.options({
symbolId: "icon-[name]",
})
.end()
.use("svgo-loader")
.loader("svgo-loader")
.options(options)
.end();
}
module.exports = {
chainWebpack: config => {
configSVGIcon(config);
},
};
// src/components/SvgIcon.vue
<template>
<svg :class="svgClass" aria-hidden="true">
<use :xlink:href="name"></use>
</svg>
</template>
<script>
// https://webpack.js.org/guides/dependency-management/#context-module-api
const requireAll = requireContext => requireContext.keys().map(requireContext);
const req = require.context("../svg-icons", false, /\.svg$/);
requireAll(req);
export default {
name: "SvgIcon",
props: {
iconName: {
// icon filename
type: String,
required: true,
},
className: {
// css class name
type: String,
default: "",
},
},
computed: {
name() {
let icon = this.iconName;
return icon ? `#icon-${icon}` : "";
},
svgClass() {
let className = this.className;
return className ? `svg-icon ${className}` : "svg-icon";
},
},
};
</script>
<style>
.svg-icon {
width: 1em;
height: 1em;
fill: currentColor; /* important */
overflow: hidden;
}
</style>
// Remember to copy svg files to svg-icons directory under src
<template>
<div>
<svg-icon icon-name="settings"></svg-icon>
</div>
</template>
<script>
import SvgIcon from "@/components/SvgIcon";
export default {
components: {
SvgIcon,
},
};
</script>