Skip to content

Commit

Permalink
feat: 添加 svg-sprite
Browse files Browse the repository at this point in the history
  • Loading branch information
yulimchen committed Jul 7, 2020
1 parent eabeb2d commit 8bd1bab
Show file tree
Hide file tree
Showing 10 changed files with 400 additions and 12 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
"less": "^3.8.1",
"less-loader": "^5.0.0",
"postcss-px-to-viewport": "^1.1.1",
"svg-sprite-loader": "^5.0.0",
"vue-template-compiler": "^2.6.10"
},
"eslintConfig": {
Expand Down
4 changes: 4 additions & 0 deletions src/App.vue
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@
<div id="app">
<img alt="Vue logo" src="./assets/logo.png">
<Dialogue />
<div>
<svg-icon icon-class="checkin" style="font-size: 50px; margin-top: 30px;" />
<div>SVG</div>
</div>
</div>
</template>

Expand Down
61 changes: 61 additions & 0 deletions src/components/SvgIcon/index.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<template>
<div v-if="isExternal" :style="styleExternalIcon" class="svg-external-icon svg-icon" v-on="$listeners" />
<svg v-else :class="svgClass" aria-hidden="true" v-on="$listeners">
<use :xlink:href="iconName" />
</svg>
</template>

<script>
import { isExternal } from '../../utils/validate'
export default {
name: 'SvgIcon',
props: {
iconClass: {
type: String,
required: true
},
className: {
type: String,
default: ''
}
},
computed: {
isExternal() {
return isExternal(this.iconClass)
},
iconName() {
return `#icon-${this.iconClass}`
},
svgClass() {
if (this.className) {
return 'svg-icon ' + this.className
} else {
return 'svg-icon'
}
},
styleExternalIcon() {
return {
mask: `url(${this.iconClass}) no-repeat 50% 50%`,
'-webkit-mask': `url(${this.iconClass}) no-repeat 50% 50%`
}
}
}
}
</script>

<style scoped>
.svg-icon {
width: 1em;
height: 1em;
vertical-align: -0.15em;
fill: currentColor;
overflow: hidden;
}
.svg-external-icon {
background-color: currentColor;
mask-size: cover!important;
display: inline-block;
}
</style>
9 changes: 9 additions & 0 deletions src/icons/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import Vue from 'vue'
import SvgIcon from '../components/SvgIcon'// svg component

// register globally
Vue.component('svg-icon', SvgIcon)

const req = require.context('./svg', false, /\.svg$/)
const requireAll = requireContext => requireContext.keys().map(requireContext)
requireAll(req)
1 change: 1 addition & 0 deletions src/icons/svg/checkin.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
22 changes: 22 additions & 0 deletions src/icons/svgo.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# replace default config

# multipass: true
# full: true

plugins:

# - name
#
# or:
# - name: false
# - name: true
#
# or:
# - name:
# param1: 1
# param2: 2

- removeAttrs:
attrs:
- 'fill'
- 'fill-rule'
2 changes: 2 additions & 0 deletions src/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import 'normalize.css'

// 按需引入 vant-ui 组件
import './plugins/vant'
// 引入 svg 图标
import './icons'

Vue.config.productionTip = false

Expand Down
92 changes: 92 additions & 0 deletions src/utils/validate.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
/**
* @param {string} path
* @returns {Boolean}
*/
export function isExternal(path) {
return /^(https?:|mailto:|tel:)/.test(path)
}

/**
* @param {string} str
* @returns {Boolean}
*/
export function validUsername(str) {
const valid_map = ['admin', 'editor']
return valid_map.indexOf(str.trim()) >= 0
}

/**
* @param {string} url
* @returns {Boolean}
*/
export function validURL(url) {
const reg = /^(https?|ftp):\/\/([a-zA-Z0-9.-]+(:[a-zA-Z0-9.&%$-]+)*@)*((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9][0-9]?)(\.(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9]?[0-9])){3}|([a-zA-Z0-9-]+\.)*[a-zA-Z0-9-]+\.(com|edu|gov|int|mil|net|org|biz|arpa|info|name|pro|aero|coop|museum|[a-zA-Z]{2}))(:[0-9]+)*(\/($|[a-zA-Z0-9.,?'\\+&%$#=~_-]+))*$/
return reg.test(url)
}

/**
* @param {string} str
* @returns {Boolean}
*/
export function validLowerCase(str) {
const reg = /^[a-z]+$/
return reg.test(str)
}

/**
* @param {string} str
* @returns {Boolean}
*/
export function validUpperCase(str) {
const reg = /^[A-Z]+$/
return reg.test(str)
}

/**
* @param {string} str
* @returns {Boolean}
*/
export function validAlphabets(str) {
const reg = /^[A-Za-z]+$/
return reg.test(str)
}

/**
* @param {string} email
* @returns {Boolean}
*/
export function validEmail(email) {
const reg = /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/
return reg.test(email)
}

/**
* @param {string} str
* @returns {Boolean}
*/
export function isString(str) {
if (typeof str === 'string' || str instanceof String) {
return true
}
return false
}

/**
* @param {Array} arg
* @returns {Boolean}
*/
export function isArray(arg) {
if (typeof Array.isArray === 'undefined') {
return Object.prototype.toString.call(arg) === '[object Array]'
}
return Array.isArray(arg)
}

// 是否空字符串
/**
* @param {string} str
* @returns {Boolean}
*/
export function isEmptyInput(str) {
return str.trim().length > 0
}
23 changes: 23 additions & 0 deletions vue.config.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
const autoprefixer = require('autoprefixer')
const pxtoviewport = require('postcss-px-to-viewport')

const path = require('path')
function resolve(dir) {
return path.join(__dirname, dir)
}

module.exports = {
lintOnSave: true,
productionSourceMap: false, // 去除生成环境.map文件
Expand All @@ -18,5 +23,23 @@ module.exports = {
]
}
}
},
chainWebpack: (config) => {
// 设置 svg-sprite-loader
config.module
.rule('svg')
.exclude.add(resolve('src/icons'))
.end()
config.module
.rule('icons')
.test(/\.svg$/)
.include.add(resolve('src/icons'))
.end()
.use('svg-sprite-loader')
.loader('svg-sprite-loader')
.options({
symbolId: 'icon-[name]'
})
.end()
}
}
Loading

0 comments on commit 8bd1bab

Please sign in to comment.