vue.config.js 5.6 KB

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