Skip to content

Instantly share code, notes, and snippets.

@msonowal
Forked from calebporzio/SvgIcon.vue
Created January 3, 2020 10:42
Show Gist options
  • Save msonowal/2dd3baa41fe34e26542a82ac802620d5 to your computer and use it in GitHub Desktop.
Save msonowal/2dd3baa41fe34e26542a82ac802620d5 to your computer and use it in GitHub Desktop.
SVG Icon Vue Component
<template>
<div class="inline-block" v-html="require('icon-' + this.icon + '.svg')"></div>
</template>
<style module>
.svg {
fill: currentColor;
height: 1em;
margin-top: -4px;
vertical-align: middle;
width: 1em;
}
</style>
<script>
export default {
props: ['icon'],
mounted() {
this.$el.firstChild.classList.add(this.$style.svg)
this.$el.firstChild.removeAttribute('height')
this.$el.firstChild.removeAttribute('width')
}
}
</script>
// Here is the custom code for my webpack.mix.js file:
// Exclude svg icons
Mix.listen('configReady', function (config) {
const rules = config.module.rules;
const targetRegex = /(\.(png|jpe?g|gif)$|^((?!font).)*\.svg$)/;
for (let rule of rules) {
if (rule.test.toString() == targetRegex.toString()) {
rule.exclude = /\.svg$/;
break;
}
}
});
// Use a custom loader for inline icons
mix.webpackConfig({
module: {
rules: [{
test: /\.svg$/,
use: [{
loader: 'html-loader',
options: {
minimize: true
}
}]
}]
},
resolve: {
modules: [ path.resolve(__dirname, 'resources/assets/svg') ]
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment