vue.config.js 4.5 KB

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