vue.config.js 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. 'use strict'
  2. const path = require('path')
  3. // 导入compression-webpack-plugin 压缩JS大小
  4. const CompressionWebpackPlugin = require('compression-webpack-plugin')
  5. // 定义压缩文件类型 压缩JS大小
  6. const productionGzipExtensions = ['js', 'css']
  7. function resolve(dir) {
  8. return path.join(__dirname, dir)
  9. }
  10. const name = process.env.VUE_APP_TITLE || '互联网医院管理系统' // 网页标题
  11. const port = process.env.port || process.env.npm_config_port || 80 // 端口
  12. // vue.config.js 配置说明
  13. //官方vue.config.js 参考文档 https://cli.vuejs.org/zh/config/#css-loaderoptions
  14. // 这里只列一部分,具体配置参考文档
  15. module.exports = {
  16. // 需要 babel 转译的 node_modules 依赖(解决 @aws-sdk/@smithy 包的现代JS语法兼容问题)
  17. transpileDependencies: [/@aws/, /@smithy/],
  18. // 部署生产环境和开发环境下的URL。
  19. // 默认情况下,Vue CLI 会假设你的应用是被部署在一个域名的根路径上
  20. // 例如 https://www.test.vip/。如果应用被部署在一个子路径上,你就需要用这个选项指定这个子路径。例如,如果你的应用被部署在 https://www.test.vip/admin/,则设置 baseUrl 为 /admin/。
  21. publicPath: process.env.NODE_ENV === "production" ? "/" : "/",
  22. // 在npm run build 或 yarn build 时 ,生成文件的目录名称(要和baseUrl的生产环境路径一致)(默认dist)
  23. outputDir: 'dist',
  24. // 用于放置生成的静态资源 (js、css、img、fonts) 的;(项目打包之后,静态资源会放在这个文件夹下)
  25. assetsDir: 'static',
  26. // 是否开启eslint保存检测,有效值:ture | false | 'error'
  27. lintOnSave: process.env.NODE_ENV === 'development',
  28. // 如果你不需要生产环境的 source map,可以将其设置为 false 以加速生产环境构建。
  29. productionSourceMap: false,
  30. // webpack-dev-server 相关配置
  31. devServer: {
  32. host: '0.0.0.0',
  33. port: port,
  34. open: true,
  35. proxy: {
  36. // 为 watch 模块单独设置的代理
  37. '/watch-api': {
  38. // target: 'http://localhost:8114', // 另一个目标服务器
  39. target: 'http://localhost:8114', // 另一个目标服务器
  40. changeOrigin: true,
  41. pathRewrite: {
  42. '^/watch-api': '' // 将 /watch-api 替换为空
  43. }
  44. },
  45. // detail: https://cli.vuejs.org/config/#devserver-proxy
  46. [process.env.VUE_APP_BASE_API]: {
  47. target: `http://localhost:8006`,
  48. changeOrigin: true,
  49. pathRewrite: {
  50. ['^' + process.env.VUE_APP_BASE_API]: ''
  51. }
  52. }
  53. },
  54. disableHostCheck: true
  55. },
  56. configureWebpack: config => { //压缩JS大小
  57. // 第一部分配置(对象形式转函数操作)
  58. config.name = name;
  59. config.resolve = {
  60. ...config.resolve,
  61. alias: {
  62. ...(config.resolve.alias || {}),
  63. '@': resolve('src'),
  64. 'styles': resolve("src/components/LemonUI/styles"),
  65. // webpack4 无法解析 ES 构建里的 import.meta.url,强制走 CJS 入口
  66. '@openim/wasm-client-sdk': resolve('node_modules/@openim/wasm-client-sdk/lib/index.js'),
  67. // 解决 @aws-sdk/@smithy 在浏览器环境下引用 Node.js 内置模块的问题
  68. 'node:crypto': resolve('empty-module.js'),
  69. 'node:fs': resolve('empty-module.js'),
  70. 'node:fs/promises': resolve('empty-module.js'),
  71. 'node:http': resolve('empty-module.js'),
  72. 'node:os': resolve('empty-module.js'),
  73. 'node:path': resolve('empty-module.js'),
  74. 'node:process': resolve('empty-module.js'),
  75. 'node:stream': resolve('empty-module.js'),
  76. 'node:zlib': resolve('empty-module.js')
  77. }
  78. };
  79. // 配置 webpack4 的 node 选项,禁用 Node.js 内置模块的 polyfill
  80. config.node = {
  81. ...config.node,
  82. fs: 'empty',
  83. crypto: 'empty',
  84. http: 'empty',
  85. https: 'empty',
  86. os: 'empty',
  87. stream: 'empty',
  88. zlib: 'empty'
  89. };
  90. if (process.env.NODE_ENV === 'production') {
  91. config.plugins.push(
  92. new CompressionWebpackPlugin({
  93. // filename: '[path].gz[query]',
  94. // algorithm: 'gzip',
  95. test: new RegExp('\\.(' + productionGzipExtensions.join('|') + ')$'),
  96. threshold: 5120,
  97. minRatio: 0.4
  98. })
  99. )
  100. } else {
  101. // 开发环境
  102. }
  103. },
  104. chainWebpack(config) {
  105. // 解决 @aws-sdk/@smithy 引用 node:xxx 内置模块的问题(webpack4 下 alias 对子路径不生效,用插件拦截)
  106. const webpack = require('webpack')
  107. config.plugin('replace-node-modules').use(webpack.NormalModuleReplacementPlugin, [
  108. /^node:/,
  109. resolve('empty-module.js')
  110. ])
  111. config.plugins.delete('preload') // TODO: need test
  112. config.plugins.delete('prefetch') // TODO: need test
  113. // set svg-sprite-loader
  114. config.module
  115. .rule('svg')
  116. .exclude.add(resolve('src/assets/icons'))
  117. .end()
  118. config.module
  119. .rule('icons')
  120. .test(/\.svg$/)
  121. .include.add(resolve('src/assets/icons'))
  122. .end()
  123. .use('svg-sprite-loader')
  124. .loader('svg-sprite-loader')
  125. .options({
  126. symbolId: 'icon-[name]'
  127. })
  128. .end()
  129. config
  130. .when(process.env.NODE_ENV !== 'development',
  131. config => {
  132. config
  133. .plugin('ScriptExtHtmlWebpackPlugin')
  134. .after('html')
  135. .use('script-ext-html-webpack-plugin', [{
  136. // `runtime` must same as runtimeChunk name. default is `runtime`
  137. inline: /runtime\..*\.js$/
  138. }])
  139. .end()
  140. config
  141. .optimization.splitChunks({
  142. chunks: 'all',
  143. cacheGroups: {
  144. libs: {
  145. name: 'chunk-libs',
  146. test: /[\\/]node_modules[\\/]/,
  147. priority: 10,
  148. chunks: 'initial' // only package third parties that are initially dependent
  149. },
  150. elementUI: {
  151. name: 'chunk-elementUI', // split elementUI into a single package
  152. priority: 20, // the weight needs to be larger than libs and app or it will be packaged into libs or app
  153. test: /[\\/]node_modules[\\/]_?element-ui(.*)/ // in order to adapt to cnpm
  154. },
  155. commons: {
  156. name: 'chunk-commons',
  157. test: resolve('src/components'), // can customize your rules
  158. minChunks: 3, // minimum common number
  159. priority: 5,
  160. reuseExistingChunk: true
  161. }
  162. }
  163. })
  164. config.optimization.runtimeChunk('single')
  165. // 原下方有游离对象 { from: robots.txt, to: './' },未接入 CopyWebpackPlugin,不生效已移除
  166. // public/robots.txt 会由 Vue CLI 默认 copy 到 dist 根目录,无需额外配置
  167. }
  168. )
  169. }
  170. }