vue.config.js 8.2 KB

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