vue.config.js 4.2 KB

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