vue.config.js 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. 'use strict'
  2. const path = require('path')
  3. const rm = require('rimraf')
  4. const defaultSettings = require('./src/settings.js')
  5. const CompressionWebpackPlugin = require('compression-webpack-plugin')
  6. const TerserPlugin = require('terser-webpack-plugin')
  7. const webpack = require('webpack')
  8. function resolve(dir) {
  9. return path.join(__dirname, dir)
  10. }
  11. rm(resolve('node_modules/.cache'), err => {
  12. if (err) {
  13. throw err
  14. }
  15. })
  16. const name = defaultSettings.title || '登录系统' // page title
  17. const BASE_URL = process.env.NODE_ENV === 'production' ? 'http://118.24.21.156:8087' : 'http://118.24.21.156:8087'// 这个BASE_URL这里没有用到,在request.js里面重新定义了后端接口
  18. const port = 9527
  19. // const port = process.env.port || process.env.npm_config_port || 9528 // dev por
  20. module.exports = {
  21. /**
  22. * 如果计划在子路径下部署站点,则需要设置publicPath
  23. */
  24. publicPath: '/',
  25. outputDir: 'dist',
  26. assetsDir: 'static',
  27. // lintOnSave: process.env.NODE_ENV === 'development',
  28. lintOnSave: false,
  29. productionSourceMap: false, // 生产环境是否生成 sourceMap 文件
  30. devServer: {
  31. // host: '192.168.31.235',
  32. port: port, // 端口
  33. open: true, // 自动开启浏览器
  34. compress: false, // 开启压缩
  35. overlay: {
  36. warnings: false,
  37. errors: false
  38. },
  39. proxy: {
  40. '/formapi': {
  41. target: 'http://192.168.2.15:10085',
  42. changeOrigin: true,
  43. pathRewrite: {
  44. '^/formapi': ''
  45. }
  46. },
  47. '/backstage': {
  48. target: 'http://36.138.232.124:10085',
  49. changeOrigin: true,
  50. pathRewrite: {
  51. '^/backstage': ''
  52. }
  53. },
  54. '/api': {
  55. target: 'http://36.138.232.124:10085',
  56. changeOrigin: true,
  57. pathRewrite: {
  58. '^/api': ''
  59. }
  60. },
  61. '/service': {
  62. target: 'http://36.138.232.124:10085',
  63. changeOrigin: true,
  64. pathRewrite: {
  65. '^/service': '/service'
  66. }
  67. },
  68. '/arcgis/rest': {
  69. target: 'http://36.138.232.124:6080',
  70. changeOrigin: true,
  71. pathRewrite: {
  72. '^/arcgis/rest': '/arcgis/rest'
  73. }
  74. }
  75. }
  76. // before: require('./mock/mock-server.js')
  77. },
  78. css: {
  79. // 是否使用css分离插件 ExtractTextPlugin
  80. extract: false,
  81. // 开启 CSS source maps?
  82. sourceMap: true,
  83. // css预设器配置项
  84. loaderOptions: {
  85. // pass options to sass-loader
  86. sass: {
  87. // 引入全局变量样式,@使我们设置的别名,执行src目录
  88. data: `@import "@/styles/index.scss";`
  89. }
  90. },
  91. // 启用 CSS modules for all css / pre-processor files.
  92. modules: false
  93. },
  94. configureWebpack: {
  95. name: name,
  96. resolve: {
  97. alias: {
  98. '@': resolve('src'),
  99. 'staticPub': resolve('public')
  100. }
  101. },
  102. devtool: '#eval-source-map',
  103. plugins: [// 压缩代码
  104. new CompressionWebpackPlugin(
  105. {
  106. filename: '[path].gz[query]',
  107. algorithm: 'gzip',
  108. test: /\.js$|\.html$|\.json$|\.css/,
  109. threshold: 0, // 只有大小大于该值的资源会被处理
  110. minRatio: 0.4, // 只有压缩率小于这个值的资源才会被处理
  111. deleteOriginalAssets: false // 删除原文件 ,需要nginx配置支持
  112. }
  113. ),
  114. new webpack.ProvidePlugin(
  115. {
  116. $: 'jquery',
  117. jQuery: 'jquery',
  118. 'windows.jQuery': 'jquery'
  119. }
  120. ),
  121. new TerserPlugin({
  122. cache: true, // 降低版本号后增加
  123. sourceMap: false, // 降低版本号后增加
  124. // 多进程
  125. parallel: true, // 降低版本号后增加
  126. terserOptions: {
  127. ecma: undefined,
  128. warnings: false,
  129. parse: {},
  130. compress: {
  131. drop_console: true,
  132. drop_debugger: true,
  133. pure_funcs: ['console.log'] // 移除console
  134. }
  135. }
  136. })
  137. ],
  138. optimization: {
  139. minimize: true, // 确保最小化是开启的(默认在生产模式下是开启的)
  140. minimizer: [
  141. new TerserPlugin({
  142. terserOptions: {
  143. compress: {
  144. warnings: false,
  145. drop_console: true,
  146. drop_debugger: true,
  147. pure_funcs: ['console.log']
  148. }
  149. }
  150. })
  151. ]
  152. }
  153. },
  154. chainWebpack(config) {
  155. // 删除预加载
  156. config.plugins.delete('preload')
  157. config.plugins.delete('prefetch')
  158. // 设置 svg-sprite-loader
  159. config.module.rule('svg').exclude.add(resolve('src/icons')).end()
  160. config.module
  161. .rule('icons')
  162. .test(/\.svg$/)
  163. .include.add(resolve('src/icons'))
  164. .end()
  165. .use('svg-sprite-loader')
  166. .loader('svg-sprite-loader')
  167. .options({
  168. symbolId: 'icon-[name]'
  169. })
  170. .end()
  171. // 设置保留空白
  172. config.module
  173. .rule('vue')
  174. .use('vue-loader')
  175. .loader('vue-loader')
  176. .tap(options => {
  177. options.compilerOptions.preserveWhitespace = true
  178. return options
  179. })
  180. .end()
  181. config
  182. .when(process.env.NODE_ENV === 'development',
  183. config => config.devtool('cheap-source-map')
  184. )
  185. config.when(process.env.NODE_ENV !== 'development',
  186. config => {
  187. config.plugin('ScriptExtHtmlWebpackPlugin').after('html').use('script-ext-html-webpack-plugin', [{
  188. // `runtime`必须与runtimeChunk名称相同。默认值为“runtime”`
  189. inline: /runtime\..*\.js$/
  190. }]).end()
  191. // 分割代码
  192. config.optimization.splitChunks({
  193. chunks: 'all',
  194. cacheGroups: {
  195. libs: {
  196. name: 'chunk-libs',
  197. test: /[\\/]node_modules[\\/]/,
  198. priority: 10,
  199. chunks: 'initial' // 仅打包最初依赖的第三方
  200. },
  201. elementUI: {
  202. name: 'chunk-elementUI', // 将elementUI拆分为一个包
  203. priority: 20, // 重量必须大于libs和app,否则将打包成libs或app
  204. test: /[\\/]node_modules[\\/]_?element-ui(.*)/ // 为了适应cnpm
  205. },
  206. commons: {
  207. name: 'chunk-commons',
  208. test: resolve('src/components'), // 可以自定义规则
  209. minChunks: 3, // 最小公共数
  210. priority: 5,
  211. reuseExistingChunk: true
  212. }
  213. }
  214. })
  215. config.optimization.runtimeChunk('single')
  216. }
  217. )
  218. }
  219. }