vue.config.js 4.5 KB

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