vue.config.js 7.0 KB

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