vue.config.js 6.7 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. const WebpackBar = require('webpackbar')
  9. function resolve(dir) {
  10. return path.join(__dirname, dir)
  11. }
  12. rm(resolve('node_modules/.cache'), err => {
  13. if (err) {
  14. throw err
  15. }
  16. })
  17. const name = defaultSettings.title || '登录系统' // page title
  18. const BASE_URL = process.env.NODE_ENV === 'production' ? 'http://118.24.21.156:8087' : 'http://118.24.21.156:8087'// 这个BASE_URL这里没有用到,在request.js里面重新定义了后端接口
  19. const port = 9527
  20. // const port = process.env.port || process.env.npm_config_port || 9528 // dev por
  21. module.exports = {
  22. /**
  23. * 如果计划在子路径下部署站点,则需要设置publicPath
  24. */
  25. publicPath: '/',
  26. outputDir: 'dist',
  27. assetsDir: 'static',
  28. // lintOnSave: process.env.NODE_ENV === 'development',
  29. lintOnSave: false,
  30. productionSourceMap: false, // 生产环境是否生成 sourceMap 文件
  31. devServer: {
  32. // host: '192.168.31.235',
  33. port: port, // 端口
  34. open: true, // 自动开启浏览器
  35. compress: false, // 开启压缩
  36. overlay: {
  37. warnings: false,
  38. errors: false
  39. },
  40. proxy: {
  41. '/formapi': {
  42. target: 'http://192.168.2.15:10085',
  43. changeOrigin: true,
  44. pathRewrite: {
  45. '^/formapi': ''
  46. }
  47. },
  48. '/backstage': {
  49. target: 'http://36.138.232.124:10085',
  50. changeOrigin: true,
  51. pathRewrite: {
  52. '^/backstage': ''
  53. }
  54. },
  55. '/api': {
  56. target: 'http://36.138.232.124:10085',
  57. changeOrigin: true,
  58. pathRewrite: {
  59. '^/api': ''
  60. }
  61. },
  62. '/service': {
  63. target: 'http://36.138.232.124:10085',
  64. changeOrigin: true,
  65. pathRewrite: {
  66. '^/service': '/service'
  67. }
  68. },
  69. '/arcgis/rest': {
  70. target: 'http://36.138.232.124:6080',
  71. changeOrigin: true,
  72. pathRewrite: {
  73. '^/arcgis/rest': '/arcgis/rest'
  74. }
  75. }
  76. }
  77. // before: require('./mock/mock-server.js')
  78. },
  79. css: {
  80. // 是否使用css分离插件 ExtractTextPlugin
  81. extract: true,
  82. // 开启 CSS source maps?
  83. sourceMap: false,
  84. // css预设器配置项
  85. loaderOptions: {
  86. // pass options to sass-loader
  87. sass: {
  88. // 引入全局变量样式,@使我们设置的别名,执行src目录
  89. data: `@import "@/styles/index.scss";`
  90. }
  91. },
  92. // 启用 CSS modules for all css / pre-processor files.
  93. modules: false
  94. },
  95. configureWebpack: {
  96. name: name,
  97. resolve: {
  98. alias: {
  99. '@': resolve('src'),
  100. 'staticPub': resolve('public')
  101. }
  102. },
  103. // TODO: 打包时打开--start
  104. cache: {
  105. type: 'filesystem',
  106. allowCollectingMemory: true
  107. },
  108. devtool: 'none', // 打包需要开启 '#eval-source-map'
  109. optimization: {
  110. minimize: true,
  111. minimizer: [
  112. new TerserPlugin({
  113. cache: true,
  114. sourceMap: false,
  115. parallel: true, // 使用多进程并发运行以提高构建速度 Boolean|Number 默认值: true
  116. terserOptions: {
  117. compress: {
  118. drop_console: true, // 移除所有console相关代码;
  119. drop_debugger: true, // 移除自动断点功能;
  120. pure_funcs: ["console.log", "console.error"], // 配置移除指定的指令,如console.log,alert等
  121. },
  122. format: {
  123. comments: false, // 删除注释
  124. },
  125. },
  126. extractComments: false, // 是否将注释剥离到单独的文件中
  127. })
  128. ]
  129. },
  130. plugins: [// 压缩代码
  131. new CompressionWebpackPlugin(
  132. {
  133. filename: '[path].gz[query]',
  134. algorithm: 'gzip',
  135. test: /\.js$|\.html$|\.json$|\.css/,
  136. threshold: 0, // 只有大小大于该值的资源会被处理
  137. minRatio: 0.8, // 只有压缩率小于这个值的资源才会被处理
  138. deleteOriginalAssets: false // 删除原文件
  139. }
  140. ),
  141. new webpack.ProvidePlugin({
  142. $: 'jquery',
  143. jQuery: 'jquery',
  144. jquery: 'jquery',
  145. 'windows.jQuery': 'jquery'
  146. }),
  147. new WebpackBar({
  148. color: '#85d', // 默认green,进度条颜色支持HEX
  149. basic: false, // 默认true,启用一个简单的日志报告器
  150. profile: false // 默认false,启用探查器。
  151. })
  152. ]
  153. // TODO: 打包时打开--end
  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. }