vue.config.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. const fs = require('fs')
  2. const path = require('path')
  3. const { defineConfig } = require('@vue/cli-service')
  4. const moment = require('moment')
  5. const CompressionPlugin = require('compression-webpack-plugin')
  6. const ForkTsCheckerWebpackPlugin = require('fork-ts-checker-webpack-plugin')
  7. const CopyWebpackPlugin = require('copy-webpack-plugin')
  8. const convertPath = (dir) => path.resolve(__dirname, dir)
  9. const outputDir = convertPath('app/dist') // 打包输出目录
  10. module.exports = defineConfig({
  11. transpileDependencies: [/node_modules/],
  12. productionSourceMap: false, // 打包取消.map
  13. publicPath: './',
  14. outputDir,
  15. pages: {
  16. indexPath: {
  17. entry: process.env.VUE_APP_ROOT + 'main.ts',
  18. template: process.env.VUE_APP_ROOT + 'index.html',
  19. filename: 'index.html',
  20. title: process.env.VUE_APP_TITLE,
  21. meta: {
  22. revised: moment(new Date()).format('YYYYMMDDHHmmss') // 打包生成版本号
  23. }
  24. }
  25. },
  26. devServer: {
  27. client: {
  28. overlay: false
  29. }
  30. },
  31. css: {
  32. extract: true, // 是否使用css分离插件 ExtractTextPlugin
  33. sourceMap: false,
  34. loaderOptions: {
  35. postcss: {
  36. postcssOptions: {
  37. config: process.env.VUE_APP_ROOT, // 自定义 postcss.config.js 文件路径,https://webpack.docschina.org/loaders/postcss-loader/#postcss-options
  38. }
  39. }
  40. }
  41. },
  42. chainWebpack: (config) => {
  43. config.resolve.alias
  44. .set('@pc', convertPath('src/packages/pc/'))
  45. .set('@mobile', convertPath('src/packages/mobile/'))
  46. config.plugin('compressionPlugin')
  47. .use(new CompressionPlugin({
  48. test: /\.js$|\.html$|\.css/, // 匹配文件名
  49. threshold: 10240, // 对超过10k的数据压缩
  50. }))
  51. config.plugin('fork-ts-checker')
  52. .use(new ForkTsCheckerWebpackPlugin({
  53. typescript: {
  54. memoryLimit: 4096,
  55. },
  56. }))
  57. },
  58. configureWebpack: (config) => {
  59. const oem = process.env.VUE_APP_OEM
  60. // const manifestPath = oem ? convertPath(oem + 'manifest.json') : convertPath('public/manifest.json')
  61. // const manifestContents = fs.readFileSync(manifestPath, 'utf-8')
  62. // const manifest = JSON.parse(manifestContents)
  63. // console.log(manifest)
  64. // 打包自动修改版本号
  65. if (process.env.NODE_ENV === 'production') {
  66. const configPath = oem ? convertPath(oem + 'config/appconfig.json') : convertPath('public/config/appconfig.json')
  67. const cfgContents = fs.readFileSync(configPath, 'utf-8')
  68. const cfg = JSON.parse(cfgContents)
  69. const arr = cfg.version.split('.').map(Number)
  70. arr[arr.length - 1] += 1 // 版本号自动+1
  71. // 版本号100进1
  72. for (let i = arr.length - 1; i >= 0; i--) {
  73. if (i > 0 && arr[i] > 99) {
  74. arr[i] = 0
  75. if (arr[i - 1] > -1) {
  76. arr[i - 1] += 1
  77. }
  78. }
  79. }
  80. cfg.version = arr.join('.')
  81. cfg.versionCode = (Number(cfg.versionCode) + 1).toString()
  82. fs.writeFileSync(configPath, JSON.stringify(cfg, null, 2))
  83. }
  84. if (oem) {
  85. // 打包时复制指定目录文件到目标目录
  86. config.plugins.push(new CopyWebpackPlugin({
  87. patterns: [
  88. {
  89. from: convertPath(oem), // 指定目录
  90. to: outputDir, // 目标目录
  91. force: true, // 强制覆盖文件
  92. }
  93. ]
  94. }))
  95. }
  96. }
  97. })