vue.config.js 6.8 KB

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