# vue-theme-peel **Repository Path**: xccjh/vue-theme-peel ## Basic Information - **Project Name**: vue-theme-peel - **Description**: vue换肤插件,支持vue-cli创建vue3/2项目,如vue3+typescript+ant/vant或element-ui - **Primary Language**: Unknown - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2023-07-24 - **Last Updated**: 2023-07-24 ## Categories & Tags **Categories**: Uncategorized **Tags**: 动态换肤工具 ## README ## @94ai/vue-theme-peel vue换肤插件 ## 安装 > 支持`webpack v4.x`和`html-webpack-plugin v3.x`(支持vue-cli创建vue3/2项目,如vue3+typescript+ant/vant或element-ui) ```bash $ yarn add @94ai/vue-theme-peel -D ``` ## 使用 ### 配置插件 ```js // vue.config.js const ThemeSwitchPlugin = require('@94ai/vue-theme-peel') const dev = process.env.NODE_ENV === 'development' const publicPath = 'http://localhost:8089/'; module.exports = { chainWebpack: (config) => { const newLoader = { loader: ThemeSwitchPlugin.loader // 👈 替换掉默认的所有样式处理loader } ;['vue'].forEach((item) => { ['css', 'scss', 'sass', 'less', 'stylus'].forEach((style) => { const originUse = config.module.rule(style).oneOf(item).toConfig().use originUse.splice(0, 1, newLoader) config.module.rule(style).oneOf(item).uses.clear() config.module.rule(style).oneOf(item).merge({ use: originUse }) }) }) if (!dev) { config.devtool('none') // 👈 关掉css映射 config .plugins.delete('extract-css') // 👈 替换掉默认的extract-css插件 config .plugin('ThemeSwitchPluginArgs') // 👈 使用ThemeSwitchPlugin .use(ThemeSwitchPlugin, [{ filename: 'static/css/[name].[hash:8].css', chunkFilename: 'static/css/[name].[contenthash:8].css' }]).before('optimize-css') config.optimization.minimizer('terser').tap(args => { // 👈 关掉js映射 args[0].sourceMap = false return args }) config .plugin('ThemeSwitchPluginInject') // 👈 注入主题变量工具函数 .use(ThemeSwitchPlugin.inject,[{ publicPath // 👈 配置动态加载的publicPath }]) }else { config .plugin('ThemeSwitchPluginInject') .use(ThemeSwitchPlugin.inject) } config.plugin('html').tap(args => { const param = args[0] param.minify = { // 👈 优化压缩 removeComments: true, collapseWhitespace: true, removeAttributeQuotes: true } param.chunksSortMode = 'dependency' return [param] }) } } ``` ### 组件使用 任意组件中使用theme标识区分不同的主题,开发环境动态生成style标签,生产环境动态加载link标签 ```vue ``` ### 开发阶段 ![hq-seeai-cli使用演示](https://xccjhzjh.oss-cn-hongkong.aliyuncs.com/xccjh-images/theme.gif) ### 线上环境 ![hq-seeai-cli使用演示](https://xccjhzjh.oss-cn-hongkong.aliyuncs.com/xccjh-images/themelocal.gif) ## 原理 ### 开发阶段 在开发阶段,对于vue项目,通用做法是将样式通过`vue-style-loader`提取出来,然后通过`