vue.config.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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. module.exports = defineConfig({
  8. // 项目部署的基本路径,默认 '/'
  9. publicPath: '/',
  10. assetsDir: 'static',
  11. // 项目打包是否生成js的 source map 调试包,默认 true,生产部署设置为false
  12. productionSourceMap: process.env.NODE_ENV !== 'production',
  13. // devServer 支持 webpack-dev-server 所有选项
  14. devServer: {
  15. open: true,
  16. // host: 'localhost',
  17. port: 9000,
  18. hot: true,
  19. // https: false,
  20. proxy: {
  21. '/api': {
  22. target: 'http://192.168.3.86:7654',
  23. secure: false, // 是否支持 https,默认 false
  24. changeOrigin: true, // 是否支持跨域
  25. pathRewrite: {
  26. '^/api': ''
  27. }
  28. }
  29. }
  30. },
  31. configureWebpack: config => {
  32. config.devtool = 'source-map'
  33. // 代码拆分
  34. config.optimization.splitChunks.chunks = 'all'
  35. config.output.filename = `static/js/[name].${Timestamp}.js`
  36. config.output.chunkFilename = `static/js/[name].${Timestamp}.js`
  37. config.module.rules.push(
  38. {
  39. test: /\.geojson$/,
  40. loader: 'json-loader',
  41. type: 'javascript/auto'
  42. },
  43. // 其他加载器规则...
  44. {
  45. test: /\.jade$/,
  46. loader: 'jade'
  47. },
  48. {
  49. test: /\.pug$/,
  50. loader: 'pug-plain-loader'
  51. })
  52. if (process.env.NODE_ENV === 'production') {
  53. // 压缩 JS 文件
  54. config.plugins.push(
  55. new UglifyJsPlugin({
  56. uglifyOptions: {
  57. compress: {
  58. drop_console: true,
  59. drop_debugger: true
  60. },
  61. output: {
  62. comments: false
  63. }
  64. },
  65. sourceMap: false,
  66. parallel: true
  67. }))
  68. }
  69. },
  70. chainWebpack: config => {
  71. config.when(process.env.NODE_ENV !== 'development', config => {
  72. config.optimization.minimizer('terser').tap(options => {
  73. options[0].terserOptions.compress.drop_console = true
  74. options[0].terserOptions.output = {
  75. comments: false
  76. }
  77. return options
  78. })
  79. })
  80. },
  81. // babel-loader 是否处理 node_modules 中的依赖包,处理哪些依赖包,参数类型: boolean | Array<string | RegExp>
  82. transpileDependencies: [
  83. 'axios'
  84. ],
  85. // 移除console
  86. terser: {
  87. terserOptions: {
  88. compress: {
  89. drop_console: true,
  90. drop_debugger: true
  91. }
  92. }
  93. },
  94. css: {
  95. loaderOptions: {
  96. sass: {
  97. additionalData: `
  98. @import "@/styles/config.scss";
  99. `
  100. }
  101. }
  102. }
  103. })