vue.config.js 6.6 KB

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