vue.config.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. const TransformPages = require('uni-read-pages')
  2. const {webpack} = new TransformPages()
  3. // 读取 manifest.json ,修改后重新写入
  4. const fs = require('fs')
  5. const path = require('path');
  6. // 获取绝对路径
  7. function resolve(dir) {
  8. return path.join(__dirname, dir)
  9. }
  10. const manifestPath = './src/manifest.json'
  11. let Manifest = fs.readFileSync(manifestPath, { encoding: 'utf-8' })
  12. function replaceManifest(path, value) {
  13. const arr = path.split('.')
  14. const len = arr.length
  15. const lastItem = arr[len - 1]
  16. let i = 0
  17. let ManifestArr = Manifest.split(/\n/)
  18. for (let index = 0; index < ManifestArr.length; index++) {
  19. const item = ManifestArr[index]
  20. if (new RegExp(`"${arr[i]}"`).test(item)) ++i;
  21. if (i === len) {
  22. const hasComma = /,/.test(item)
  23. if(typeof(value) === "boolean") {
  24. ManifestArr[index] = item.replace(new RegExp(`"${lastItem}"[\\s\\S]*:[\\s\\S]*`), `"${lastItem}": ${value}${hasComma ? ',' : ''}`)
  25. }else {
  26. ManifestArr[index] = item.replace(new RegExp(`"${lastItem}"[\\s\\S]*:[\\s\\S]*`), `"${lastItem}": "${value}"${hasComma ? ',' : ''}`)
  27. }
  28. break;
  29. }
  30. }
  31. Manifest = ManifestArr.join('\n')
  32. }
  33. // 使用
  34. replaceManifest('app-plus.usingComponents', false)
  35. replaceManifest('app-plus.splashscreen.alwaysShowBeforeRender', false)
  36. replaceManifest('mp-baidu.usingComponents', false)
  37. replaceManifest('h5.publicPath', process.env.VUE_APP_BASE_PATH)
  38. replaceManifest('h5.router.base', process.env.VUE_APP_BASE_PATH)
  39. fs.writeFileSync(manifestPath, Manifest, {
  40. "flag": "w"
  41. })
  42. module.exports = {
  43. publicPath: process.env.VUE_APP_BASE_PATH,
  44. transpileDependencies: ['uview-ui','uni-simple-router','wx-open-launch-weapp'],
  45. configureWebpack: {
  46. module: {
  47. rules: [{
  48. test: /\.vue$/,
  49. use: {
  50. loader: path.resolve(__dirname, "./node_modules/vue-inset-loader")
  51. },
  52. }]
  53. },
  54. plugins: [
  55. new webpack.DefinePlugin({
  56. ROUTES: webpack.DefinePlugin.runtimeValue(() => {
  57. const tfPages = new TransformPages({
  58. includes: ['path', 'name', 'meta', 'aliasPath']
  59. });
  60. return JSON.stringify(tfPages.routes)
  61. }, true )
  62. })
  63. ],
  64. resolve: {
  65. // 文件别名
  66. alias: {
  67. "@":resolve('src'),
  68. "store":resolve('src/store'),
  69. "common": resolve('src/common'),
  70. "router":resolve('src/router'),
  71. "styles":resolve('src/styles'),
  72. "mixins":resolve('src/mixins'),
  73. "static":resolve('src/static'),
  74. }
  75. }
  76. },
  77. chainWebpack: (config) => {
  78. // 发行或运行时启用了压缩时会生效
  79. config.optimization.minimizer('terser').tap((args) => {
  80. const compress = args[0].terserOptions.compress
  81. // 非 App 平台移除 console 代码(包含所有 console 方法,如 log,debug,info...)
  82. // compress.drop_console = true
  83. compress.pure_funcs = [
  84. '__f__', // App 平台 vue 移除日志代码
  85. // 'console.debug' // 可移除指定的 console 方法
  86. ]
  87. return args
  88. })
  89. config
  90. .plugin('define')
  91. .tap(args => {
  92. args[0]['process.env'].VUE_APP_TEST = '"test"'
  93. return args
  94. })
  95. }
  96. }