vue.config.js 4.9 KB

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