vue.config.js 3.2 KB

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