vue.config.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. const path = require('path')
  2. function resolve(dir) {
  3. return path.join(__dirname, dir)
  4. }
  5. /**
  6. * cdn 引入资源
  7. */
  8. const cdn = {
  9. externals: {
  10. vue: 'Vue',
  11. 'vue-router': 'VueRouter',
  12. vuex: 'Vuex',
  13. axios: 'axios',
  14. 'element-ui': 'ELEMENT',
  15. 'echarts': 'echarts',
  16. winseaview: 'winseaview',
  17. // 'base-template-lib': 'base-template-lib',
  18. // 'ali-oss': 'ali-oss',
  19. // 'lodash': 'lodash'
  20. },
  21. js: {
  22. dev: [
  23. "//cdn.bootcdn.net/ajax/libs/sockjs-client/1.5.0/sockjs.min.js",
  24. "//cdn.bootcdn.net/ajax/libs/stomp.js/2.3.3/stomp.min.js",
  25. "//cdn.bootcdn.net/ajax/libs/Sortable/1.9.0/Sortable.min.js"
  26. ],
  27. ship: [
  28. `./static/libs/sock.min.js`,
  29. `./static/libs/stomp.min.js`,
  30. `./cdn/Sortable/1.9.0/Sortable.min.js`,
  31. `./cdn/vue/2.6.10/vue.min.js`,
  32. `./cdn/vuex/3.1.1/vuex.min.js`,
  33. `./cdn/vue-router/3.0.1/vue-router.min.js`,
  34. `./cdn/axios/1.0.0/axios.min.js`,
  35. `./cdn/element-ui/2.12.0/index.js`,
  36. `./cdn/echarts/echarts.min.js`,
  37. `./cdn/lodash/4.17.20/lodash.min.js`,
  38. // `${process.env.BASE_URL}cdn/base-template-lib/base-template-lib.umd.min.js`,
  39. // `${process.env.BASE_URL}cdn/winseaview/winseaview.min.js`
  40. ],
  41. product: [
  42. "//cdn.bootcdn.net/ajax/libs/sockjs-client/1.5.0/sockjs.min.js",
  43. "//cdn.bootcdn.net/ajax/libs/stomp.js/2.3.3/stomp.min.js",
  44. "//cdn.bootcdn.net/ajax/libs/Sortable/1.9.0/Sortable.min.js",
  45. "//cdn.bootcdn.net/ajax/libs/vue/2.6.10/vue.min.js",
  46. "//cdn.bootcdn.net/ajax/libs/vuex/3.1.1/vuex.min.js",
  47. "//cdn.bootcdn.net/ajax/libs/vue-router/3.0.1/vue-router.min.js",
  48. "//cdn.bootcdn.net/ajax/libs/axios/0.21.0/axios.min.js",
  49. "//cdn.bootcdn.net/ajax/libs/element-ui/2.12.0/index.js",
  50. "//cdn.bootcdn.net/ajax/libs/echarts/4.8.0/echarts.min.js",
  51. // "//cdn.bootcdn.net/ajax/libs/lodash.js/4.17.20/lodash.min.js",
  52. // "//cdn.bootcdn.net/ajax/libs/ali-oss/6.11.2/aliyun-oss-sdk.min.js",
  53. // `/cdn/base-template-lib/base-template-lib.umd.min.js`,
  54. `/cdn/winseaview/winseaview.min.js`
  55. ]
  56. },
  57. css: {
  58. dev: [
  59. "/cdn/element-ui/2.13.2/theme-chalk/index.css",
  60. `./static/iconfont/iconfont.css`
  61. ],
  62. ship: [
  63. "/cdn/element-ui/2.13.2/theme-chalk/index.css",
  64. `./static/iconfont/iconfont.css`
  65. ],
  66. product: [
  67. `/cdn/winseaview/index.css`,
  68. "/cdn/element-ui/2.13.2/theme-chalk/index.css",
  69. `./static/iconfont/iconfont.css`
  70. ]
  71. }
  72. }
  73. const port = 9527 // dev port
  74. const ignoreFile = ['production']
  75. module.exports = {
  76. assetsDir: 'static',
  77. lintOnSave: true,
  78. publicPath: process.env.VUE_APP_PACKAGE_ENV === 'ship' ? '/' : './',
  79. productionSourceMap: false, //去掉 .map 文件
  80. // runtimeCompiler: true,
  81. // cssSourceMap: true,
  82. devServer: {
  83. port: port,
  84. disableHostCheck: true,
  85. open: false,
  86. overlay: {
  87. warnings: false,
  88. errors: true
  89. },
  90. proxy: {
  91. '/pb/bijiadan': {
  92. // target: 'http://127.0.0.1:4523/mock/349485',//目标地址
  93. target: 'http://192.168.24.5:8045',//目标地址
  94. // target: 'http://192.168.24.24:8089',//目标地址
  95. changeOrigin: true, //开启代理:在本地会创建一个虚拟服务端,然后发送请求的数据,并同时接收请求的数据,这样服务端和服务端进行数据的交互就不会有跨域问题
  96. pathRewrite: {
  97. '^/pb/bijiadan': '/'
  98. }, //这里重写路径
  99. logLevel: 'debug',
  100. },
  101. '/pb/hs': {
  102. // target: 'http://127.0.0.1:4523/mock/349485',//目标地址
  103. target: 'http://192.168.24.24:8045',// 刘宪春本地目标地址
  104. // target: 'http://192.168.22.6:8020',//目标地址
  105. // target: 'http://192.168.24.24:8089',//目标地址
  106. changeOrigin: true, //开启代理:在本地会创建一个虚拟服务端,然后发送请求的数据,并同时接收请求的数据,这样服务端和服务端进行数据的交互就不会有跨域问题
  107. pathRewrite: {
  108. '^/pb/hs': '/'
  109. }, //这里重写路径
  110. logLevel: 'debug',
  111. },
  112. '/pb/mock': {
  113. target: 'http://127.0.0.1:4523/mock/349485',//目标地址
  114. changeOrigin: true, //开启代理:在本地会创建一个虚拟服务端,然后发送请求的数据,并同时接收请求的数据,这样服务端和服务端进行数据的交互就不会有跨域问题
  115. pathRewrite: {
  116. '^/pb/mock': '/'
  117. }, //这里重写路径
  118. logLevel: 'debug',
  119. },
  120. '/pb/apiFinance': {
  121. target: 'http://192.168.24.5:8098',//目标地址
  122. changeOrigin: true, //开启代理:在本地会创建一个虚拟服务端,然后发送请求的数据,并同时接收请求的数据,这样服务端和服务端进行数据的交互就不会有跨域问题
  123. pathRewrite: {
  124. '^/pb/apiFinance': '/'
  125. }, //这里重写路径
  126. logLevel: 'debug',
  127. },
  128. '/pb/api': {
  129. // target: 'http://127.0.0.1:4523/mock/349485',//目标地址
  130. target: 'http://192.168.24.5:8098',//目标地址
  131. // target: 'http://192.168.24.24:8089',//目标地址
  132. changeOrigin: true, //开启代理:在本地会创建一个虚拟服务端,然后发送请求的数据,并同时接收请求的数据,这样服务端和服务端进行数据的交互就不会有跨域问题
  133. pathRewrite: {
  134. '^/pb/api': '/'
  135. }, //这里重写路径
  136. logLevel: 'debug',
  137. },
  138. '/pb': {
  139. // target: 'https://product-server.winsea.com/', //目标地址
  140. // target: 'http://standard-dev.winsea.com/', //目标地址
  141. // target: 'http://localhost:8090/',
  142. target: 'http://192.168.1.122:9100/',
  143. // target: 'http://192.168.24.5:8098',//目标地址
  144. // target: 'http://product-server.winsea.com/',
  145. // target: 'http://192.168.24.5:8098',//目标地址
  146. changeOrigin: true, //开启代理:在本地会创建一个虚拟服务端,然后发送请求的数据,并同时接收请求的数据,这样服务端和服务端进行数据的交互就不会有跨域问题
  147. pathRewrite: { '^/pb': '/' }, //这里重写路径
  148. secure: false,
  149. logLevel: 'debug',
  150. // headers: {
  151. // Referer: 'http://192.168.23.9:9527/',//'https://product-server.winsea.com/'
  152. // }
  153. },
  154. }
  155. },
  156. configureWebpack: (config) => {
  157. // provide the app's title in webpack's name field, so that
  158. // it can be accessed in index.html to inject the correct title.
  159. // if (debug) { // 开发环境配置
  160. // config.devtool = 'source-map'
  161. // }
  162. //直接修改配置
  163. config.resolve.alias['@'] = resolve('src')
  164. config.resolve.alias['@/assets'] = resolve('src/assets')
  165. // if (ignoreFile.includes(process.env.NODE_ENV)) {
  166. // config.optimization.minimizer[0].options.terserOptions.compress.warnings = false
  167. // config.optimization.minimizer[0].options.terserOptions.compress.drop_console = false
  168. // config.optimization.minimizer[0].options.terserOptions.compress.drop_debugger = true
  169. // config.optimization.minimizer[0].options.terserOptions.compress.pure_funcs = ['console.log', 'console.info']
  170. // }
  171. },
  172. chainWebpack(config) {
  173. config.plugins.delete('preload')
  174. config.plugins.delete('prefetch')
  175. config.output.chunkFilename('static/js/[name].[chunkhash].js').end()
  176. const entry = config.entry('app')
  177. entry.add('babel-polyfill').end()
  178. entry.add('classlist-polyfill').end()
  179. /* 添加分析工具 */
  180. if (process.env.NODE_ENV === 'production') {
  181. // config
  182. // .plugin('webpack-bundle-analyzer')
  183. // .use(require('webpack-bundle-analyzer').BundleAnalyzerPlugin)
  184. // .end()
  185. }
  186. // entry.add('@/mock').end()
  187. //忽略的打包文件
  188. if (ignoreFile.includes(process.env.NODE_ENV)) {
  189. //忽略的打包文件
  190. config.externals(cdn.externals)
  191. //开启Gzip压缩
  192. config
  193. .plugin('gzip-plugin')
  194. .use('compression-webpack-plugin', [{
  195. filename: '[path].gz[query]',
  196. algorithm: 'gzip',
  197. test: /\.js$|\.html$|\.json$|\.css$|\.ttf$/,
  198. threshold: 10240, // 对超过10k的数据压缩
  199. minRatio: 0.8, // 只有压缩率小于这个值的资源才会被处理
  200. deleteOriginalAssets: false // 删除原文件
  201. }])
  202. .end()
  203. }
  204. config.plugin('html')
  205. .tap(args => {
  206. args[0].cdn = cdn
  207. return args
  208. })
  209. config.module.rule('images')
  210. .use('url-loader')
  211. .loader('url-loader')
  212. .tap(options => Object.assign(options, {
  213. esModule: false
  214. }))
  215. // set preserveWhitespace
  216. config.module
  217. .rule('vue')
  218. .use('vue-loader')
  219. .loader('vue-loader')
  220. .tap(options => {
  221. options.compilerOptions.preserveWhitespace = true
  222. return options
  223. })
  224. .end()
  225. config.when(process.env.NODE_ENV === 'development', config =>
  226. config.devtool('cheap-source-map')
  227. // config.devtool('source-map') // 源码-慢
  228. )
  229. config.when(process.env.NODE_ENV !== 'development', config => {
  230. // // 压缩代码
  231. config.optimization.minimize(true)
  232. config.optimization.splitChunks({
  233. chunks: 'all',
  234. cacheGroups: {
  235. libs: {
  236. name: 'chunk-libs',
  237. test: /[\\/]node_modules[\\/]/,
  238. priority: 10,
  239. chunks: 'initial' //只打包初始时依赖的第三方
  240. },
  241. // elementUI: {
  242. // name: "chunk-elementUI", // 单独将 elementUI 拆包
  243. // test: /[\\/]node_modules[\\/]element-ui[\\/]/,
  244. // chunks: "all",
  245. // priority: 3,
  246. // reuseExistingChunk: true,
  247. // enforce: true
  248. // },
  249. commons: {
  250. name: 'chunk-commons',
  251. test: resolve('src/components'), // 可自定义拓展你的规则
  252. minChunks: 3, // 最小共用次数
  253. priority: 5,
  254. reuseExistingChunk: true,
  255. minSize: 0,
  256. priority: 1,
  257. enforce: true
  258. }
  259. }
  260. })
  261. config.optimization.runtimeChunk('single')
  262. })
  263. }
  264. }