vue.config.js 4.5 KB

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