vue.config.js 5.2 KB

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