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中列出来。详情参考

    使用示例
    自定义静态资源目录

    1. const path = require('path')
    2. const CopyWebpackPlugin = require('copy-webpack-plugin') //最新版本copy-webpack-plugin插件暂不兼容,推荐v5.0.0
    3. module.exports = {
    4. configureWebpack: {
    5. plugins: [
    6. new CopyWebpackPlugin([
    7. {
    8. from: path.join(__dirname, 'src/images'),
    9. to: path.join(__dirname, 'dist', process.env.NODE_ENV === 'production' ? 'build' : 'dev', process.env.UNI_PLATFORM, 'images')
    10. }
    11. ])
    12. ]
    13. }
    14. }

    注入全局依赖

    1. const webpack = require('webpack')
    2. module.exports = {
    3. configureWebpack: {
    4. plugins: [
    5. new webpack.ProvidePlugin({
    6. 'localStorage': ['mp-storage', 'localStorage'],
    7. 'window.localStorage': ['mp-storage', 'localStorage']
    8. })
    9. ]
    10. }
    11. }

    配置环境变量

    1. const webpack = require('webpack')
    2. module.exports = {
    3. chainWebpack: config => {
    4. config
    5. .plugin('define')
    6. .tap(args => {
    7. args[0]['process.env'].VUE_APP_TEST = '"test"'
    8. return args
    9. })
    10. }
    11. }

    发布时删除console
    HBuilderX 2.6.8+支持

    1. module.exports = {
    2. chainWebpack: (config) => {
    3. // 发行或运行时启用了压缩时会生效
    4. config.optimization.minimizer('terser').tap((args) => {
    5. const compress = args[0].terserOptions.compress
    6. // 非 App 平台移除 console 代码(包含所有 console 方法,如 log,debug,info...)
    7. compress.drop_console = true
    8. compress.pure_funcs = [
    9. '__f__', // App 平台 vue 移除日志代码
    10. // 'console.debug' // 可移除指定的 console 方法
    11. ]
    12. return args
    13. })
    14. }
    15. }

    发布时动态修改 manifest.json

    1. // 读取 manifest.json ,修改后重新写入
    2. const fs = require('fs')
    3. const manifestPath = './src/manifest.json'
    4. let Manifest = fs.readFileSync(manifestPath, { encoding: 'utf-8' })
    5. function replaceManifest(path, value) {
    6. const arr = path.split('.')
    7. const len = arr.length
    8. const lastItem = arr[len - 1]
    9. let i = 0
    10. let ManifestArr = Manifest.split(/\n/)
    11. for (let index = 0; index < ManifestArr.length; index++) {
    12. const item = ManifestArr[index]
    13. if (new RegExp(`"${arr[i]}"`).test(item)) ++i;
    14. if (i === len) {
    15. const hasComma = /,/.test(item)
    16. ManifestArr[index] = item.replace(new RegExp(`"${lastItem}"[\\s\\S]*:[\\s\\S]*`), `"${lastItem}": ${value}${hasComma ? ',' : ''}`)
    17. break;
    18. }
    19. }
    20. Manifest = ManifestArr.join('\n')
    21. }
    22. // 使用
    23. replaceManifest('app-plus.usingComponents', false)
    24. replaceManifest('app-plus.splashscreen.alwaysShowBeforeRender', false)
    25. replaceManifest('mp-baidu.usingComponents', false)
    26. fs.writeFileSync(manifestPath, Manifest, {
    27. "flag": "w"
    28. })
    29. module.exports = {
    30. // ...
    31. }

    启用压缩的方法:

    • HBuilderX创建的项目勾选运行—>运行到小程序模拟器—>运行时是否压缩代码
    • cli创建的项目可以在package.json中添加参数—minimize,示例:”dev:mp-weixin”: “cross-env NODE_ENV=development UNI_PLATFORM=mp-weixin vue-cli-service uni-build —watch —minimize”