vue.config.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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. externals: isLibBuild ? { vue: 'vue', 'vue-router': 'vue-router' } : {},
  64. // 在库模式下,入口输出文件名是固定的(例如 eep-ui.umd.js)。
  65. // 使用唯一的区块文件名以防止异步区块发生冲突。
  66. output: isLibBuild ? { chunkFilename: 'eep-ui.[name].[contenthash:8].js' } : {},
  67. // 配置路径别名
  68. resolve: {
  69. alias: {
  70. '@': resolve('src'),
  71. },
  72. },
  73. devtool: NODE_ENV === 'development' ? 'source-map' : false,
  74. plugins: [
  75. // stylelint 配置
  76. new StyleLintPlugin({
  77. files: ['src/**/*.{vue,html,css,scss}'],
  78. fix: true, // 是否自动修复
  79. failOnError: false,
  80. }),
  81. new CompressionPlugin({
  82. algorithm: 'gzip', // 使用gzip压缩
  83. test: /\.js$|\.html$|\.css$/, // 匹配文件名
  84. minRatio: 0.8, // 压缩率小于0.8才会压缩
  85. threshold: 10240, // 对超过10k的数据压缩
  86. deleteOriginalAssets: false, // 是否删除未压缩的源文件,谨慎设置,如果希望提供非gzip的资源,可不设置或者设置为false(比如删除打包后的gz后还可以加载到原始资源文件)
  87. }),
  88. ],
  89. },
  90. chainWebpack(config) {
  91. // 启用预加载,提高首屏加载速度
  92. config
  93. .plugin('preload')
  94. .use(PreloadPlugin, [
  95. {
  96. rel: 'preload',
  97. include: 'initial',
  98. fileBlacklist: [/\.map$/, /hot-update\.js$/],
  99. as(entry) {
  100. if (/\.css$/.test(entry)) return 'style';
  101. return 'script';
  102. },
  103. },
  104. ])
  105. .after('html');
  106. // 当页面很多时,prefetch 将导致太多无意义的请求,开启这个
  107. config
  108. .plugin('prefetch')
  109. .use(PreloadPlugin, [
  110. {
  111. rel: 'prefetch',
  112. include: 'asyncChunks',
  113. },
  114. ])
  115. .after('html');
  116. // 设置 svg-sprite-loader
  117. config.module.rule('svg').exclude.add(resolve('src/icons')).end();
  118. config.module
  119. .rule('icons')
  120. .test(/\.svg$/)
  121. .include.add(resolve('src/icons'))
  122. .end()
  123. .use('svg-sprite-loader')
  124. .loader('svg-sprite-loader')
  125. .options({
  126. symbolId: 'icon-[name]',
  127. })
  128. .end();
  129. config.when(NODE_ENV !== 'development' && !isLibBuild, () => {
  130. config.optimization.splitChunks({
  131. chunks: 'all',
  132. cacheGroups: {
  133. libs: {
  134. name: 'chunk-libs',
  135. test: /[\\/]node_modules[\\/]/,
  136. priority: 10,
  137. chunks: 'initial', // 仅打包最初依赖的第三方
  138. },
  139. elementUI: {
  140. name: 'chunk-elementUI', // 将elementUI拆分为一个包
  141. priority: 20, // 权重必须大于libs和app,否则将被打包到libs或app中
  142. test: /[\\/]node_modules[\\/]_?element-ui(.*)/, // in order to adapt to cnpm
  143. },
  144. commons: {
  145. name: 'chunk-commons',
  146. test: resolve('src/components'), // 可以自定义规则
  147. minChunks: 3, // 最小公用数
  148. priority: 5,
  149. reuseExistingChunk: true,
  150. },
  151. },
  152. });
  153. // https://webpack.js.org/configuration/optimization/#optimizationruntimechunk
  154. config.optimization.runtimeChunk('single');
  155. });
  156. },
  157. });