vue.config.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. 'use strict'
  2. const { defineConfig } = require('@vue/cli-service')
  3. const WriteFilePlugin = require('write-file-webpack-plugin')
  4. const UglifyJsPlugin = require('uglifyjs-webpack-plugin')
  5. // const BundleAnalyzerPlugin = require('webpack-bundle-analyzer').BundleAnalyzerPlugin
  6. const fs = require('fs')
  7. // 配置编辑器
  8. const Timestamp = new Date().getTime()
  9. // if (process.env.NODE_ENV === 'production') {
  10. // fs.writeFile('./public/version.json', JSON.stringify({ version: Timestamp }), () => {
  11. // console.log('新版本号生成成功')
  12. // })
  13. // }
  14. module.exports = defineConfig({
  15. // 项目部署的基本路径,默认 '/'
  16. publicPath: '/',
  17. assetsDir: 'static',
  18. // 项目打包是否生成js的 source map 调试包,默认 true,生产部署设置为false
  19. productionSourceMap: process.env.NODE_ENV !== 'production',
  20. // devServer 支持 webpack-dev-server 所有选项
  21. devServer: {
  22. open: true,
  23. // host: 'localhost',
  24. port: 18183,
  25. hot: true,
  26. // https: false,
  27. proxy: {
  28. '/api': {
  29. target: process.env.VUE_APP_BASE_API,
  30. secure: false, // 是否支持 https,默认 false
  31. changeOrigin: true, // 是否支持跨域
  32. pathRewrite: {
  33. '^/api': ''
  34. }
  35. }
  36. }
  37. },
  38. configureWebpack: config => {
  39. config.devtool = 'source-map'
  40. // 代码拆分
  41. config.optimization.splitChunks.chunks = 'all'
  42. config.output.filename = `static/js/[name].${Timestamp}.js`
  43. config.output.chunkFilename = `static/js/[name].${Timestamp}.js`
  44. config.module.rules.push(
  45. {
  46. test: /\.geojson$/,
  47. loader: 'json-loader',
  48. type: 'javascript/auto'
  49. },
  50. // 其他加载器规则...
  51. {
  52. test: /\.jade$/,
  53. loader: 'jade'
  54. },
  55. {
  56. test: /\.pug$/,
  57. loader: 'pug-plain-loader'
  58. })
  59. config.plugins.push(
  60. new WriteFilePlugin({
  61. test: /version\.json$/,
  62. useHashIndex: true
  63. })
  64. )
  65. if (process.env.NODE_ENV === 'production') {
  66. // 压缩 JS 文件
  67. config.plugins.push(
  68. new UglifyJsPlugin({
  69. uglifyOptions: {
  70. compress: {
  71. drop_console: true,
  72. drop_debugger: true
  73. },
  74. output: {
  75. comments: false
  76. }
  77. },
  78. sourceMap: false,
  79. parallel: true
  80. }))
  81. }
  82. },
  83. chainWebpack: config => {
  84. config.when(process.env.NODE_ENV !== 'development', config => {
  85. config.optimization.minimizer('terser').tap(options => {
  86. options[0].terserOptions.compress.drop_console = true
  87. options[0].terserOptions.output = {
  88. comments: false
  89. }
  90. return options
  91. })
  92. })
  93. config.plugin('generate-version-file').use({
  94. apply: (compiler) => {
  95. // 生成当前时间戳
  96. // const timestamp = Date.now()
  97. // 构造 version.json 内容
  98. const versionData = {
  99. version: Timestamp
  100. }
  101. // 写入 version.json 文件
  102. if (process.env.NODE_ENV === 'production') {
  103. fs.writeFileSync('./public/version.json', JSON.stringify(versionData, null, 2))
  104. }
  105. }
  106. })
  107. // config.output.chunkFilename('js/[name].[contenthash].js').end()
  108. },
  109. // babel-loader 是否处理 node_modules 中的依赖包,处理哪些依赖包,参数类型: boolean | Array<string | RegExp>
  110. transpileDependencies: [
  111. '@vue-office/excel',
  112. 'vue-demi',
  113. '@vue-office',
  114. 'xlsx',
  115. 'exceljs',
  116. 'html2canvas',
  117. '@handsontable',
  118. '@handsontable/vue',
  119. 'handsontable',
  120. 'handsontable/registry',
  121. 'js-base64',
  122. 'vis-network',
  123. 'vuetify',
  124. 'axios'
  125. ]
  126. })