vue.config.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. const path = require('path');
  2. const { defineConfig } = require('@vue/cli-service');
  3. const StyleLintPlugin = require('stylelint-webpack-plugin');
  4. const CompressionPlugin = require('compression-webpack-plugin');
  5. const PreloadPlugin = require('@vue/preload-webpack-plugin');
  6. function resolve(dir) {
  7. return path.join(__dirname, './', dir);
  8. }
  9. const NODE_ENV = process.env.NODE_ENV;
  10. const port = process.env.port || 9564;
  11. const baseApiUrlList = {
  12. '': '',
  13. '/api': 'https://gcls.helxsoft.cn/',
  14. };
  15. const fileApiUrlList = {
  16. '': '',
  17. '/file': 'https://file-kf.helxsoft.cn/',
  18. };
  19. const eepApiUrlList = {
  20. '': '',
  21. '/eep': 'http://eep.helxsoft.cn/',
  22. };
  23. const proxy = {};
  24. let name = '智慧梧桐桌面端互动教材编辑器';
  25. if (NODE_ENV === 'production') {
  26. name += '【开发系统】';
  27. }
  28. if (NODE_ENV === 'development') {
  29. proxy[process.env.VUE_APP_BASE_API] = {
  30. target: baseApiUrlList[process.env.VUE_APP_BASE_API],
  31. changeOrigin: true,
  32. pathRewrite: {
  33. [`^${process.env.VUE_APP_BASE_API}`]: '',
  34. },
  35. };
  36. proxy[process.env.VUE_APP_EEP] = {
  37. target: eepApiUrlList[process.env.VUE_APP_EEP],
  38. changeOrigin: true,
  39. pathRewrite: {
  40. [`^${process.env.VUE_APP_EEP}`]: '',
  41. },
  42. };
  43. proxy[process.env.VUE_APP_FILE] = {
  44. target: fileApiUrlList[process.env.VUE_APP_FILE],
  45. changeOrigin: true,
  46. pathRewrite: {
  47. [`^${process.env.VUE_APP_FILE}`]: '',
  48. },
  49. };
  50. }
  51. module.exports = defineConfig({
  52. publicPath: NODE_ENV === 'development' ? '/' : './',
  53. outputDir: 'dist',
  54. assetsDir: 'static',
  55. lintOnSave: NODE_ENV === 'development',
  56. runtimeCompiler: true,
  57. productionSourceMap: false,
  58. devServer: {
  59. port,
  60. open: {
  61. target: [`http://localhost:${port}`],
  62. },
  63. client: {
  64. overlay: {
  65. warnings: false,
  66. errors: true,
  67. },
  68. },
  69. proxy,
  70. },
  71. css: {
  72. loaderOptions: {
  73. sass: {
  74. additionalData: '@use "@/styles/variables.scss" as *;',
  75. },
  76. },
  77. },
  78. configureWebpack: {
  79. name,
  80. // 配置路径别名
  81. resolve: {
  82. alias: {
  83. '@': resolve('src'),
  84. },
  85. },
  86. devtool: NODE_ENV === 'development' ? 'source-map' : false,
  87. plugins: [
  88. // stylelint 配置
  89. new StyleLintPlugin({
  90. files: ['src/**/*.{vue,html,css,scss}'],
  91. fix: true, // 是否自动修复
  92. failOnError: false,
  93. }),
  94. new CompressionPlugin({
  95. algorithm: 'gzip', // 使用gzip压缩
  96. test: /\.js$|\.html$|\.css$/, // 匹配文件名
  97. minRatio: 0.8, // 压缩率小于0.8才会压缩
  98. threshold: 10240, // 对超过10k的数据压缩
  99. deleteOriginalAssets: false, // 是否删除未压缩的源文件,谨慎设置,如果希望提供非gzip的资源,可不设置或者设置为false(比如删除打包后的gz后还可以加载到原始资源文件)
  100. }),
  101. ],
  102. },
  103. chainWebpack(config) {
  104. // 启用预加载,提高首屏加载速度
  105. config
  106. .plugin('preload')
  107. .use(PreloadPlugin, [
  108. {
  109. rel: 'preload',
  110. include: 'initial',
  111. fileBlacklist: [/\.map$/, /hot-update\.js$/],
  112. as(entry) {
  113. if (/\.css$/.test(entry)) return 'style';
  114. return 'script';
  115. },
  116. },
  117. ])
  118. .after('html');
  119. // 当页面很多时,prefetch 将导致太多无意义的请求,开启这个
  120. config
  121. .plugin('prefetch')
  122. .use(PreloadPlugin, [
  123. {
  124. rel: 'prefetch',
  125. include: 'asyncChunks',
  126. },
  127. ])
  128. .after('html');
  129. // 设置 svg-sprite-loader
  130. config.module.rule('svg').exclude.add(resolve('src/icons')).end();
  131. config.module
  132. .rule('icons')
  133. .test(/\.svg$/)
  134. .include.add(resolve('src/icons'))
  135. .end()
  136. .use('svg-sprite-loader')
  137. .loader('svg-sprite-loader')
  138. .options({
  139. symbolId: 'icon-[name]',
  140. })
  141. .end();
  142. config.when(NODE_ENV !== 'development', () => {
  143. config.optimization.splitChunks({
  144. chunks: 'all',
  145. cacheGroups: {
  146. libs: {
  147. name: 'chunk-libs',
  148. test: /[\\/]node_modules[\\/]/,
  149. priority: 10,
  150. chunks: 'initial', // 仅打包最初依赖的第三方
  151. },
  152. elementUI: {
  153. name: 'chunk-elementUI', // 将elementUI拆分为一个包
  154. priority: 20, // 权重必须大于libs和app,否则将被打包到libs或app中
  155. test: /[\\/]node_modules[\\/]_?element-ui(.*)/, // in order to adapt to cnpm
  156. },
  157. commons: {
  158. name: 'chunk-commons',
  159. test: resolve('src/components'), // 可以自定义规则
  160. minChunks: 3, // 最小公用数
  161. priority: 5,
  162. reuseExistingChunk: true,
  163. },
  164. },
  165. });
  166. // https://webpack.js.org/configuration/optimization/#optimizationruntimechunk
  167. config.optimization.runtimeChunk('single');
  168. });
  169. },
  170. });