From 099b6af6232d185a8c4be63ebbe445cb5ec97c67 Mon Sep 17 00:00:00 2001 From: dingtalk_umvbsp Date: Thu, 21 Apr 2022 10:56:25 +0800 Subject: [PATCH] master --- src/assets/style/index.less | 0 src/assets/svg/demo.svg | 2 ++ src/components/index.js | 13 ++++++++ src/components/svg-icon.vue | 27 +++++++++++++++ src/config/svgBuilder.js | 65 +++++++++++++++++++++++++++++++++++++ src/main.js | 2 ++ src/views/Home.vue | 3 +- vite.config.js | 16 ++++++++- 8 files changed, 126 insertions(+), 2 deletions(-) create mode 100644 src/assets/style/index.less create mode 100644 src/assets/svg/demo.svg create mode 100644 src/components/index.js create mode 100644 src/components/svg-icon.vue create mode 100644 src/config/svgBuilder.js diff --git a/src/assets/style/index.less b/src/assets/style/index.less new file mode 100644 index 0000000..e69de29 diff --git a/src/assets/svg/demo.svg b/src/assets/svg/demo.svg new file mode 100644 index 0000000..1dbe9d1 --- /dev/null +++ b/src/assets/svg/demo.svg @@ -0,0 +1,2 @@ + \ No newline at end of file diff --git a/src/components/index.js b/src/components/index.js new file mode 100644 index 0000000..865eff7 --- /dev/null +++ b/src/components/index.js @@ -0,0 +1,13 @@ +import svgIcon from './svg-icon.vue' + +const components = { + svgIcon, +} + +export default{ + install(Vue, opt){ + for(var i in components){ + Vue.component(i, components[i]) + } + } +} \ No newline at end of file diff --git a/src/components/svg-icon.vue b/src/components/svg-icon.vue new file mode 100644 index 0000000..1e4cf4f --- /dev/null +++ b/src/components/svg-icon.vue @@ -0,0 +1,27 @@ + + + \ No newline at end of file diff --git a/src/config/svgBuilder.js b/src/config/svgBuilder.js new file mode 100644 index 0000000..0466c9e --- /dev/null +++ b/src/config/svgBuilder.js @@ -0,0 +1,65 @@ +import { readFileSync, readdirSync } from 'fs' + +let idPerfix = '' +const svgTitle = /+].*?)>/ +const clearHeightWidth = /(width|height)="([^>+].*?)"/g + +const hasViewBox = /(viewBox="[^>+].*?")/g + +const clearReturn = /(\r)|(\n)/g + +function findSvgFile(dir) { + const svgRes = [] + const dirents = readdirSync(dir, { + withFileTypes: true + }) + for (const dirent of dirents) { + if (dirent.isDirectory()) { + svgRes.push(...findSvgFile(dir + dirent.name + '/')) + } else { + const svg = readFileSync(dir + dirent.name) + .toString() + .replace(clearReturn, '') + .replace(svgTitle, ($1, $2) => { + let width = 0 + let height = 0 + let content = $2.replace(clearHeightWidth, (s1, s2, s3) => { + if (s2 === 'width') { + width = s3 + } else if (s2 === 'height') { + height = s3 + } + return '' + }) + if (!hasViewBox.test($2)) { + content += `viewBox="0 0 ${width} ${height}"` + } + return `` + }) + .replace('', '') + svgRes.push(svg) + } + } + return svgRes +} + +export const svgBuilder = (path, perfix = 'icon') => { + if (path === '') return + idPerfix = perfix + const res = findSvgFile(path) + + return { + name: 'svg-transform', + transformIndexHtml(html) { + return html.replace( + '', + ` + + + ${res.join('')} + + ` + ) + } + } +} \ No newline at end of file diff --git a/src/main.js b/src/main.js index 6fbdc9b..709fd28 100644 --- a/src/main.js +++ b/src/main.js @@ -2,11 +2,13 @@ import { createApp } from 'vue' import App from './App.vue' import router from './router' import store from '@/store' +import regComponents from '@/components' console.log(import.meta.env,'--------------import------') var app=createApp(App) app.use(router) .use(store) + .use(regComponents) .mount('#app') app.config.globalProperties.foo='===foo====' // 全局配置属性 \ No newline at end of file diff --git a/src/views/Home.vue b/src/views/Home.vue index 8d86a58..768248a 100644 --- a/src/views/Home.vue +++ b/src/views/Home.vue @@ -3,11 +3,12 @@ diff --git a/vite.config.js b/vite.config.js index 85cdbc0..9b48741 100644 --- a/vite.config.js +++ b/vite.config.js @@ -1,9 +1,14 @@ import { defineConfig } from 'vite' import path from 'path' import vue from '@vitejs/plugin-vue' +import {svgBuilder} from './src/config/svgBuilder' + // https://vitejs.dev/config/ export default defineConfig({ - plugins: [vue()], + plugins: [ + vue(), + svgBuilder('./src/assets/svg/'),//提取导入svg文件 + ], resolve:{ alias:{ "@":path.resolve(__dirname,'src'), @@ -45,5 +50,14 @@ export default defineConfig({ rewrite: path=>path.replace(/^\/api/,'') } }, + }, + pluginOptions:{ + 'style-resources-loader':{ + preProcessor: 'less', + patterns:[ + // 全局less文件 + path.resolve(__dirname, 'assets/style/index.less') + ] + } } })