vue.config.js 是一个可选的配置文件,如果项目的根目录中存在这个文件,那么它会被自动加载,一般用于配置 webpack 等编译选项,具体规范参考:vue.config.js
支持情况
- CLI 工程
- HBuilderX 2.1.5 及以上版本
注意事项
- 仅vue页面生效
部分配置项会被编译配置覆盖,例如:
- publicPath 不支持,如果需要配置,请在 manifest.json->h5->router->base 中配置,参考文档:h5-router
- outputDir 不支持
- assetsDir 固定 static
- pages 不支持
- runtimeCompiler 固定 false
- productionSourceMap 固定 false
- css.extract H5 平台固定 false,其他平台固定 true
- parallel 固定 false
- 使用cli项目时,默认情况下 babel-loader 会忽略所有 node_modules 中的文件。如果你想要通过 Babel 显式转译一个依赖,可以在transpileDependencies中列出来。详情参考
使用示例
自定义静态资源目录
const path = require('path')const CopyWebpackPlugin = require('copy-webpack-plugin') //最新版本copy-webpack-plugin插件暂不兼容,推荐v5.0.0module.exports = {configureWebpack: {plugins: [new CopyWebpackPlugin([{from: path.join(__dirname, 'src/images'),to: path.join(__dirname, 'dist', process.env.NODE_ENV === 'production' ? 'build' : 'dev', process.env.UNI_PLATFORM, 'images')}])]}}
注入全局依赖
const webpack = require('webpack')module.exports = {configureWebpack: {plugins: [new webpack.ProvidePlugin({'localStorage': ['mp-storage', 'localStorage'],'window.localStorage': ['mp-storage', 'localStorage']})]}}
配置环境变量
const webpack = require('webpack')module.exports = {chainWebpack: config => {config.plugin('define').tap(args => {args[0]['process.env'].VUE_APP_TEST = '"test"'return args})}}
发布时删除console
HBuilderX 2.6.8+支持
module.exports = {chainWebpack: (config) => {// 发行或运行时启用了压缩时会生效config.optimization.minimizer('terser').tap((args) => {const compress = args[0].terserOptions.compress// 非 App 平台移除 console 代码(包含所有 console 方法,如 log,debug,info...)compress.drop_console = truecompress.pure_funcs = ['__f__', // App 平台 vue 移除日志代码// 'console.debug' // 可移除指定的 console 方法]return args})}}
发布时动态修改 manifest.json
// 读取 manifest.json ,修改后重新写入const fs = require('fs')const manifestPath = './src/manifest.json'let Manifest = fs.readFileSync(manifestPath, { encoding: 'utf-8' })function replaceManifest(path, value) {const arr = path.split('.')const len = arr.lengthconst lastItem = arr[len - 1]let i = 0let ManifestArr = Manifest.split(/\n/)for (let index = 0; index < ManifestArr.length; index++) {const item = ManifestArr[index]if (new RegExp(`"${arr[i]}"`).test(item)) ++i;if (i === len) {const hasComma = /,/.test(item)ManifestArr[index] = item.replace(new RegExp(`"${lastItem}"[\\s\\S]*:[\\s\\S]*`), `"${lastItem}": ${value}${hasComma ? ',' : ''}`)break;}}Manifest = ManifestArr.join('\n')}// 使用replaceManifest('app-plus.usingComponents', false)replaceManifest('app-plus.splashscreen.alwaysShowBeforeRender', false)replaceManifest('mp-baidu.usingComponents', false)fs.writeFileSync(manifestPath, Manifest, {"flag": "w"})module.exports = {// ...}
启用压缩的方法:
- HBuilderX创建的项目勾选运行—>运行到小程序模拟器—>运行时是否压缩代码
- cli创建的项目可以在package.json中添加参数—minimize,示例:”dev:mp-weixin”: “cross-env NODE_ENV=development UNI_PLATFORM=mp-weixin vue-cli-service uni-build —watch —minimize”
