vue.config.js 4.6 KB

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