vue.config.js 4.8 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 isLibBuild = process.argv.some((arg, index, argv) => {
  11. if (arg === '--target') {
  12. return argv[index + 1] === 'lib';
  13. }
  14. return arg === '--target=lib';
  15. });
  16. const port = process.env.port || 9564;
  17. const eepApiUrlList = {
  18. '': '',
  19. '/eep': 'http://eep.helxsoft.cn/',
  20. };
  21. const proxy = {};
  22. let name = '智慧梧桐数字教材编辑器';
  23. if (NODE_ENV === 'development') {
  24. proxy[process.env.VUE_APP_EEP] = {
  25. target: eepApiUrlList[process.env.VUE_APP_EEP],
  26. changeOrigin: true,
  27. pathRewrite: {
  28. [`^${process.env.VUE_APP_EEP}`]: '',
  29. },
  30. };
  31. }
  32. module.exports = defineConfig({
  33. // 仅在库构建时转译 node_modules,避免外部项目解析 eep-ui.common.js 时报 ?. / ?? 语法错误
  34. transpileDependencies: isLibBuild ? true : [],
  35. publicPath: NODE_ENV === 'development' ? '/' : './',
  36. outputDir: 'dist',
  37. assetsDir: 'static',
  38. lintOnSave: false,
  39. runtimeCompiler: true,
  40. productionSourceMap: false,
  41. devServer: {
  42. port,
  43. open: {
  44. target: [`http://localhost:${port}`],
  45. },
  46. client: {
  47. overlay: {
  48. warnings: false,
  49. errors: true,
  50. },
  51. },
  52. proxy,
  53. },
  54. css: {
  55. loaderOptions: {
  56. sass: {
  57. additionalData: '@use "@/styles/variables.scss" as *;',
  58. },
  59. },
  60. },
  61. configureWebpack: {
  62. name,
  63. // 在库模式下,入口输出文件名是固定的(例如 eep-ui.umd.js)。
  64. // 使用唯一的区块文件名以防止异步区块发生冲突。
  65. output: isLibBuild ? { chunkFilename: 'eep-ui.[name].[contenthash:8].js' } : {},
  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' && !isLibBuild, () => {
  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. });