The vue project dynamically loads the global css file according to the startup instruction

The vue project loads the global theme css according to the startup command

File: package.json

"start": "vue-cli-service serve --themVersion bs"

File: vue.config.js

//根据指令获取固定路径下的css文件

let themCssPathAry = [];
let roadAry = process.argv;
//去除前3位的路径和指令
roadAry.splice(0,3);
let themIndex = roadAry.indexOf('--themVersion');
if(themIndex !== -1){
    
    
  themCssPathAry.push(path.resolve(__dirname, `./src/assets/theme/${
      
      roadAry[themIndex+1]}.less`))
}
//在全局的扩展中载入添加的css文件
pluginOptions: {
    
    
    'style-resources-loader': {
    
    
      preProcessor: 'less',
      patterns: [
        path.resolve(__dirname, './src/assets/styles/*.less')
      ].concat(themCssPathAry)
    }
  },

that's all! !

Guess you like

Origin blog.csdn.net/m0_37138425/article/details/128813431