vue.config.js 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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: true,
  81. // 开启 CSS source maps?
  82. sourceMap: false,
  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. // ,
  122. // new TerserPlugin({
  123. // cache: true, // 降低版本号后增加
  124. // sourceMap: false, // 降低版本号后增加
  125. // // 多进程
  126. // parallel: true, // 降低版本号后增加
  127. // terserOptions: {
  128. // ecma: undefined,
  129. // warnings: false,
  130. // parse: {},
  131. // compress: {
  132. // drop_console: true,
  133. // drop_debugger: true,
  134. // pure_funcs: ['console.log'] // 移除console
  135. // }
  136. // }
  137. // })
  138. ]
  139. // optimization: {
  140. // minimize: true, // 确保最小化是开启的(默认在生产模式下是开启的)
  141. // minimizer: [
  142. // new TerserPlugin({
  143. // terserOptions: {
  144. // compress: {
  145. // warnings: false,
  146. // drop_console: true,
  147. // drop_debugger: true,
  148. // pure_funcs: ['console.log']
  149. // }
  150. // }
  151. // })
  152. // ]
  153. // }
  154. },
  155. chainWebpack(config) {
  156. // 删除预加载
  157. config.plugins.delete('preload')
  158. config.plugins.delete('prefetch')
  159. // 设置 svg-sprite-loader
  160. config.module.rule('svg').exclude.add(resolve('src/icons')).end()
  161. config.module
  162. .rule('icons')
  163. .test(/\.svg$/)
  164. .include.add(resolve('src/icons'))
  165. .end()
  166. .use('svg-sprite-loader')
  167. .loader('svg-sprite-loader')
  168. .options({
  169. symbolId: 'icon-[name]'
  170. })
  171. .end()
  172. // 设置保留空白
  173. config.module
  174. .rule('vue')
  175. .use('vue-loader')
  176. .loader('vue-loader')
  177. .tap(options => {
  178. options.compilerOptions.preserveWhitespace = true
  179. return options
  180. })
  181. .end()
  182. config
  183. .when(process.env.NODE_ENV === 'development',
  184. config => config.devtool('cheap-source-map')
  185. )
  186. config.when(process.env.NODE_ENV !== 'development',
  187. config => {
  188. config.plugin('ScriptExtHtmlWebpackPlugin').after('html').use('script-ext-html-webpack-plugin', [{
  189. // `runtime`必须与runtimeChunk名称相同。默认值为“runtime”`
  190. inline: /runtime\..*\.js$/
  191. }]).end()
  192. // 分割代码
  193. config.optimization.splitChunks({
  194. chunks: 'all',
  195. cacheGroups: {
  196. libs: {
  197. name: 'chunk-libs',
  198. test: /[\\/]node_modules[\\/]/,
  199. priority: 10,
  200. chunks: 'initial' // 仅打包最初依赖的第三方
  201. },
  202. elementUI: {
  203. name: 'chunk-elementUI', // 将elementUI拆分为一个包
  204. priority: 20, // 重量必须大于libs和app,否则将打包成libs或app
  205. test: /[\\/]node_modules[\\/]_?element-ui(.*)/ // 为了适应cnpm
  206. },
  207. commons: {
  208. name: 'chunk-commons',
  209. test: resolve('src/components'), // 可以自定义规则
  210. minChunks: 3, // 最小公共数
  211. priority: 5,
  212. reuseExistingChunk: true
  213. }
  214. }
  215. })
  216. config.optimization.runtimeChunk('single')
  217. }
  218. )
  219. }
  220. }