vue.config.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. 'use strict'
  2. const path = require('path')
  3. const defaultSettings = require('./src/settings.js')
  4. const CompressionWebpackPlugin = require('compression-webpack-plugin')
  5. const webpack = require('webpack')
  6. function resolve(dir) {
  7. return path.join(__dirname, dir)
  8. }
  9. const name = defaultSettings.title || '登录系统' // page title
  10. const BASE_URL = process.env.NODE_ENV === 'production' ? '' : 'http://192.168.100.204:1111'
  11. // const BASE_URL = process.env.NODE_ENV === 'production' ? '' : 'http://xrty.vipgz4.idcfengye.com'
  12. // const BASE_URL = process.env.NODE_ENV === 'production' ? '' : 'http://192.168.2.231:1111'
  13. const port = 8031
  14. // const port = process.env.port || process.env.npm_config_port || 9528 // dev port
  15. module.exports = {
  16. /**
  17. * 如果计划在子路径下部署站点,则需要设置publicPath
  18. */
  19. publicPath: '/',
  20. outputDir: 'dist',
  21. assetsDir: 'static',
  22. // lintOnSave: process.env.NODE_ENV === 'development',
  23. lintOnSave: false,
  24. productionSourceMap: false, // 生产环境是否生成 sourceMap 文件
  25. devServer: {
  26. port: port, // 端口
  27. open: true, // 自动开启浏览器
  28. compress: false, // 开启压缩
  29. overlay: {
  30. warnings: false,
  31. errors: false
  32. },
  33. proxy: {
  34. '/backstage': {
  35. // target: 'http://192.168.2.231:1111', // 修改后台接口地址
  36. // target: 'http://xrty.vipgz4.idcfengye.com',
  37. target: 'http://192.168.100.204:1111',
  38. changeOrigin: true,
  39. pathRewrite: {
  40. '^/backstage': ''
  41. }
  42. }
  43. // '/omsweb': { // 自定义 接口前缀
  44. // target: 'http://58.17.241.6:1212', // 这里可以跟随项目实际部署服务器来
  45. // changeOrigin: true,
  46. // pathRewrite: {
  47. // '^/omsweb': ''
  48. // }
  49. // }
  50. }
  51. // before: require('./mock/mock-server.js')
  52. },
  53. css: {
  54. // 是否使用css分离插件 ExtractTextPlugin
  55. extract: false,
  56. // 开启 CSS source maps?
  57. // sourceMap: false,
  58. // css预设器配置项
  59. loaderOptions: {
  60. // pass options to sass-loader
  61. sass: {
  62. // 引入全局变量样式,@使我们设置的别名,执行src目录
  63. data: `@import "@/styles/index.scss";`
  64. }
  65. },
  66. // 启用 CSS modules for all css / pre-processor files.
  67. modules: false
  68. },
  69. configureWebpack: {
  70. name: name,
  71. resolve: {
  72. alias: {
  73. '@': resolve('src'),
  74. 'staticPub': resolve('public')
  75. }
  76. },
  77. // devtool: '#eval-source-map',
  78. plugins: [// 压缩代码
  79. new CompressionWebpackPlugin(
  80. {
  81. filename: '[path].gz[query]',
  82. algorithm: 'gzip',
  83. test: /\.js$|\.html$|\.json$|\.css/,
  84. threshold: 0, // 只有大小大于该值的资源会被处理
  85. minRatio: 0.8, // 只有压缩率小于这个值的资源才会被处理
  86. deleteOriginalAssets: false // 删除原文件
  87. }
  88. ),
  89. new webpack.ProvidePlugin(
  90. {
  91. $: 'jquery',
  92. jQuery: 'jquery',
  93. 'windows.jQuery': 'jquery'
  94. }
  95. )
  96. ]
  97. },
  98. chainWebpack(config) {
  99. config.externals({ './cptable': 'var cptable' })
  100. // 删除预加载
  101. config.plugins.delete('preload')
  102. config.plugins.delete('prefetch')
  103. // 设置 svg-sprite-loader
  104. config.module
  105. .rule('svg')
  106. .exclude.add(resolve('src/icons'))
  107. .end()
  108. config.module
  109. .rule('icons')
  110. .test(/\.svg$/)
  111. .include.add(resolve('src/icons'))
  112. .end()
  113. .use('svg-sprite-loader')
  114. .loader('svg-sprite-loader')
  115. .options({
  116. symbolId: 'icon-[name]'
  117. })
  118. .end()
  119. // 设置保留空白
  120. config.module
  121. .rule('vue')
  122. .use('vue-loader')
  123. .loader('vue-loader')
  124. .tap(options => {
  125. options.compilerOptions.preserveWhitespace = true
  126. return options
  127. })
  128. .end()
  129. config
  130. .when(process.env.NODE_ENV === 'development',
  131. config => config.devtool('cheap-source-map')
  132. )
  133. config
  134. .when(process.env.NODE_ENV !== 'development',
  135. config => {
  136. config
  137. .plugin('ScriptExtHtmlWebpackPlugin')
  138. .after('html')
  139. .use('script-ext-html-webpack-plugin', [{
  140. // `runtime`必须与runtimeChunk名称相同。默认值为“runtime”`
  141. inline: /runtime\..*\.js$/
  142. }])
  143. .end()
  144. // 分割代码
  145. config
  146. .optimization.splitChunks({
  147. chunks: 'all',
  148. cacheGroups: {
  149. libs: {
  150. name: 'chunk-libs',
  151. test: /[\\/]node_modules[\\/]/,
  152. priority: 10,
  153. chunks: 'initial' // 仅打包最初依赖的第三方
  154. },
  155. elementUI: {
  156. name: 'chunk-elementUI', // 将elementUI拆分为一个包
  157. priority: 20, // 重量必须大于libs和app,否则将打包成libs或app
  158. test: /[\\/]node_modules[\\/]_?element-ui(.*)/ // 为了适应cnpm
  159. },
  160. commons: {
  161. name: 'chunk-commons',
  162. test: resolve('src/components'), // 可以自定义规则
  163. minChunks: 3, // 最小公共数
  164. priority: 5,
  165. reuseExistingChunk: true
  166. }
  167. }
  168. })
  169. config.optimization.runtimeChunk('single')
  170. }
  171. )
  172. }
  173. }