vue.config.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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_NAME,
  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('@public', convertPath('public/'))
  45. .set('@pc', convertPath('src/packages/pc/'))
  46. .set('@mobile', convertPath('src/packages/mobile/'))
  47. config.plugin('compressionPlugin')
  48. .use(new CompressionPlugin({
  49. test: /\.js$|\.html$|\.css/, // 匹配文件名
  50. threshold: 10240, // 对超过10k的数据压缩
  51. }))
  52. config.plugin('fork-ts-checker')
  53. .use(new ForkTsCheckerWebpackPlugin({
  54. typescript: {
  55. memoryLimit: 4096,
  56. },
  57. }))
  58. config.plugin('define').tap((definitions) => {
  59. Object.assign(definitions[0], {
  60. __VUE_OPTIONS_API__: 'true',
  61. __VUE_PROD_DEVTOOLS__: 'false',
  62. __VUE_PROD_HYDRATION_MISMATCH_DETAILS__: 'false'
  63. })
  64. return definitions
  65. })
  66. },
  67. configureWebpack: (config) => {
  68. const oem = process.env.VUE_APP_OEM
  69. config.devtool = 'source-map'
  70. // const manifestPath = oem ? convertPath(oem + 'manifest.json') : convertPath('public/manifest.json')
  71. // const manifestContents = fs.readFileSync(manifestPath, 'utf-8')
  72. // const manifest = JSON.parse(manifestContents)
  73. // console.log(manifest)
  74. if (process.env.NODE_ENV === 'production' && !process.argv.includes('test')) {
  75. const configPath = oem ? convertPath(oem + 'config/appconfig.json') : convertPath('public/config/appconfig.json')
  76. const cfgContents = fs.readFileSync(configPath, 'utf-8')
  77. const cfg = JSON.parse(cfgContents)
  78. // 自动修改版本号
  79. if (process.argv.includes('ver')) {
  80. const arr = cfg.version.split('.').map(Number)
  81. arr[arr.length - 1] += 1 // 版本号自动+1
  82. // 版本号100进1
  83. for (let i = arr.length - 1; i >= 0; i--) {
  84. if (i > 0 && arr[i] > 99) {
  85. arr[i] = 0
  86. if (arr[i - 1] > -1) {
  87. arr[i - 1] += 1
  88. }
  89. }
  90. }
  91. cfg.version = arr.join('.')
  92. cfg.versionCode = (Number(cfg.versionCode) + 1).toString()
  93. }
  94. cfg.appName = process.env.VUE_APP_NAME
  95. cfg.tradeChannel = process.env.VUE_APP_TRADE_CHANNEL
  96. fs.writeFileSync(configPath, JSON.stringify(cfg, null, 2))
  97. console.log(cfg)
  98. }
  99. if (oem) {
  100. // 打包时复制指定目录文件到目标目录
  101. config.plugins.push(new CopyWebpackPlugin({
  102. patterns: [
  103. {
  104. from: convertPath(oem), // 指定目录
  105. to: outputDir, // 目标目录
  106. force: true, // 强制覆盖文件
  107. }
  108. ]
  109. }))
  110. }
  111. }
  112. })