vue.config.js 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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: 'jgpt',
  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: false,
  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: name,
  85. resolve: {
  86. alias: {
  87. '@': resolve('src'),
  88. staticPub: resolve('public')
  89. }
  90. },
  91. plugins: [
  92. // 压缩代码
  93. new CompressionWebpackPlugin({
  94. filename: '[path].gz[query]',
  95. algorithm: 'gzip',
  96. test: /\.js$|\.html$|\.json$|\.css/,
  97. threshold: 0, // 只有大小大于该值的资源会被处理
  98. minRatio: 0.8, // 只有压缩率小于这个值的资源才会被处理
  99. deleteOriginalAssets: false // 删除原文件
  100. }),
  101. new webpack.ProvidePlugin({
  102. $: 'jquery',
  103. jQuery: 'jquery',
  104. 'windows.jQuery': 'jquery',
  105. 'vue-moment': 'vue-moment',
  106. 'vue-echarts': 'vue-echarts',
  107. xlsx: 'xlsx',
  108. 'xlsx-style': 'xlsx-style',
  109. 'benz-amr-recorder': 'benz-amr-recorder',
  110. html2canvas: 'html2canvas',
  111. html2pdf: 'html2pdf'
  112. })
  113. ]
  114. },
  115. chainWebpack(config) {
  116. // 删除预加载
  117. config.plugins.delete('preload')
  118. config.plugins.delete('prefetch')
  119. // 设置 svg-sprite-loader
  120. config.module
  121. .rule('svg')
  122. .exclude.add(resolve('src/icons'))
  123. .end()
  124. config.module
  125. .rule('icons')
  126. .test(/\.svg$/)
  127. .include.add(resolve('src/icons'))
  128. .end()
  129. .use('svg-sprite-loader')
  130. .loader('svg-sprite-loader')
  131. .options({
  132. symbolId: 'icon-[name]'
  133. })
  134. .end()
  135. // 设置保留空白
  136. config.module
  137. .rule('vue')
  138. .use('vue-loader')
  139. .loader('vue-loader')
  140. .tap((options) => {
  141. options.compilerOptions.preserveWhitespace = true
  142. return options
  143. })
  144. .end()
  145. config.when(process.env.NODE_ENV === 'development', (config) => config.devtool('cheap-source-map'))
  146. config.when(process.env.NODE_ENV !== 'development', (config) => {
  147. config
  148. .plugin('ScriptExtHtmlWebpackPlugin')
  149. .after('html')
  150. .use('script-ext-html-webpack-plugin', [
  151. {
  152. // `runtime`必须与runtimeChunk名称相同。默认值为“runtime”`
  153. inline: /runtime\..*\.js$/
  154. }
  155. ])
  156. .end()
  157. // 分割代码
  158. config.optimization.splitChunks({
  159. chunks: 'all',
  160. cacheGroups: {
  161. libs: {
  162. name: 'chunk-libs',
  163. test: /[\\/]node_modules[\\/]/,
  164. priority: 10,
  165. chunks: 'initial' // 仅打包最初依赖的第三方
  166. },
  167. commons: {
  168. name: 'chunk-commons',
  169. test: resolve('src/components'), // 可以自定义规则
  170. minChunks: 3, // 最小公共数
  171. priority: 5,
  172. reuseExistingChunk: true
  173. },
  174. elementUI: {
  175. name: 'chunk-elementUI', // 将elementUI拆分为一个包
  176. priority: 20, // 重量必须大于libs和app,否则将打包成libs或app
  177. test: /[\\/]node_modules[\\/]_?element-ui(.*)/ // 为了适应cnpm
  178. },
  179. monent: {
  180. name: 'chunk-moment',
  181. priority: 20,
  182. test: /[\\/]node_modules[\\/]_?vue-moment(.*)/
  183. },
  184. echarts: {
  185. name: 'chunk-echarts',
  186. priority: 20,
  187. test: /[\\/]node_modules[\\/]_?vue-echarts(.*)/
  188. },
  189. xlsx: {
  190. name: 'chunk-xlsx',
  191. priority: 20,
  192. test: /[\\/]node_modules[\\/]_?xlsx(.*)/
  193. },
  194. 'xlsx-style': {
  195. name: 'chunk-xlsx-style',
  196. priority: 20,
  197. test: /[\\/]node_modules[\\/]_?xlsx-style(.*)/
  198. },
  199. 'benz-amr-recorder': {
  200. name: 'chunk-amr',
  201. priority: 20,
  202. test: /[\\/]node_modules[\\/]_?benz-amr-recorder(.*)/
  203. },
  204. html2canvas: {
  205. name: 'chunk-html2canvas',
  206. priority: 20,
  207. test: /[\\/]node_modules[\\/]_?html2canvas(.*)/
  208. },
  209. html2pdf: {
  210. name: 'chunk-html2pdf',
  211. priority: 20,
  212. test: /[\\/]node_modules[\\/]_?html2pdf(.*)/
  213. }
  214. }
  215. })
  216. config.optimization.runtimeChunk('single')
  217. })
  218. config.externals({ './cptable': 'var cptable' })
  219. setMemeoy(config)
  220. }
  221. }