vue.config.js 4.3 KB

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