From 1ee795d5533fe7e338f2a3667e2df18b17da17c8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E5=BC=BA?= <1206709430@qq.com> Date: Thu, 22 Jun 2023 11:06:36 +0800 Subject: [PATCH] =?UTF-8?q?=E5=8A=9F=E8=83=BD=E5=8F=98=E5=8C=96:=20?= =?UTF-8?q?=E6=94=AF=E6=8C=81=E6=8F=92=E4=BB=B6=E5=8A=A8=E6=80=81scss?= =?UTF-8?q?=E5=AF=BC=E5=85=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- web/src/views/plugins/index.js | 1 + web/src/views/plugins/scssImport.js | 46 +++++++++++++++++++++++++++++ web/vue.config.js | 13 ++++++-- 3 files changed, 57 insertions(+), 3 deletions(-) create mode 100644 web/src/views/plugins/scssImport.js diff --git a/web/src/views/plugins/index.js b/web/src/views/plugins/index.js index f4116b3..ef09a4c 100644 --- a/web/src/views/plugins/index.js +++ b/web/src/views/plugins/index.js @@ -103,6 +103,7 @@ export const getStoreModules = function (Vue, options) { // 注册组件 if (Module.default) { storeModules[key] = Module.default + console.log(`[${key}]store注册成功`) return true } return false diff --git a/web/src/views/plugins/scssImport.js b/web/src/views/plugins/scssImport.js new file mode 100644 index 0000000..d45a68f --- /dev/null +++ b/web/src/views/plugins/scssImport.js @@ -0,0 +1,46 @@ +const fs = require('fs') +const path = require('path') + +module.exports = () => { + // 获取每个插件的scss文件并进行注册 + const pluginsScssPath = [] + let components = [] + // 遍历 @great-dream/ 目录下的文件 + const greatDreamDir = path.resolve('./node_modules/@great-dream') + if (fs.existsSync(greatDreamDir)) { + const greatDreamFiles = fs.readdirSync(greatDreamDir) + greatDreamFiles.forEach(file => { + const scssPath = path.join(greatDreamDir, file, 'src', 'index.scss') + if (fs.existsSync(scssPath)) { + components.push(file) + } + }) + } + + // 遍历 ./src/views/plugins 目录下的文件 + const pluginsDir = path.resolve('./src/views/plugins') + if (fs.existsSync(pluginsDir)) { + const pluginFiles = fs.readdirSync(pluginsDir) + pluginFiles.forEach(file => { + const scssPath = path.join(pluginsDir, file, 'src', 'index.scss') + if (fs.existsSync(scssPath)) { + components.push(file) + } + }) + } + components = Array.from(new Set(components)) + components.filter(async (key, index) => { + const pluginDirectories = ['./src/views/plugins/', './node_modules/@great-dream/'] + // const pluginDirectories = ['./node_modules/@great-dream/'] + pluginDirectories.forEach(directory => { + const scssPath = directory + key + '/src/index.scss' + if (fs.existsSync(scssPath)) { + pluginsScssPath.push('"' + scssPath.replace('./src/views/plugins/', '~@/views/plugins/').replace('./node_modules/@great-dream/', '~@great-dream/') + '"') + console.log(`[${key}] scss注册成功`) + return true + } + return false + }) + }) + return pluginsScssPath +} diff --git a/web/vue.config.js b/web/vue.config.js index f9fb358..973885d 100644 --- a/web/vue.config.js +++ b/web/vue.config.js @@ -4,10 +4,14 @@ const ThemeColorReplacer = require('webpack-theme-color-replacer') const forElementUI = require('webpack-theme-color-replacer/forElementUI') const cdnDependencies = require('./dependencies-cdn') const { chain, set, each } = require('lodash') - +const scssImport = require('./src/views/plugins/scssImport.js')() +var prependData = '' // 拼接路径 const resolve = dir => require('path').join(__dirname, dir) - +for (const index in scssImport) { + prependData = prependData + '@import ' + scssImport[index] + ';' +} +console.error('prependData' + prependData) // 增加环境变量 process.env.VUE_APP_VERSION = require('./package.json').version process.env.VUE_APP_BUILD_TIME = require('dayjs')().format('YYYY-M-D HH:mm:ss') @@ -44,7 +48,10 @@ module.exports = { loaderOptions: { // 设置 scss 公用变量文件 sass: { - prependData: '@import \'~@/assets/style/public.scss\';' + prependData: ` + @import "~@/assets/style/public.scss"; + ${prependData} + ` } } },