vue.config.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. const path = require('path');
  2. const StyleLintPlugin = require('stylelint-webpack-plugin');
  3. const CompressionPlugin = require('compression-webpack-plugin');
  4. const defaultSettings = require('./src/settings.js');
  5. function resolve(dir) {
  6. return path.join(__dirname, './', dir);
  7. }
  8. const name = defaultSettings.title;
  9. const NODE_ENV = process.env.NODE_ENV;
  10. const port = process.env.port || process.env.npm_config_port || 7878;
  11. let proxy = {};
  12. if (NODE_ENV === 'development') {
  13. proxy = {
  14. [process.env.VUE_APP_BASE_API]: {
  15. // target: 'https://gcls.utschool.cn/',
  16. target: 'https://gcls.helxsoft.cn/',
  17. changeOrigin: true,
  18. pathRewrite: {
  19. [`^${process.env.VUE_APP_BASE_API}`]: ''
  20. }
  21. },
  22. [process.env.VUE_APP_PDF]: {
  23. // target: 'https://file-cs.helxsoft.cn',
  24. target: 'https://file-kf.helxsoft.cn/',
  25. changeOrigin: true,
  26. pathRewrite: {
  27. [`^${process.env.VUE_APP_PDF}`]: ''
  28. }
  29. }
  30. };
  31. }
  32. // 配置项说明 https://cli.vuejs.org/config/
  33. module.exports = {
  34. publicPath: NODE_ENV === 'development' ? '/' : './',
  35. outputDir: 'dist',
  36. assetsDir: 'static',
  37. lintOnSave: false,
  38. runtimeCompiler: true,
  39. productionSourceMap: false,
  40. devServer: {
  41. port,
  42. open: true, // 默认false true自动打开网页
  43. overlay: {
  44. warnings: false,
  45. errors: true
  46. },
  47. proxy
  48. },
  49. css: {
  50. loaderOptions: {
  51. scss: {
  52. // 为 scss 配置共享全局变量
  53. additionalData: '@import "./src/styles/variables.scss";'
  54. }
  55. }
  56. },
  57. configureWebpack: {
  58. name,
  59. // 配置路径别名
  60. resolve: {
  61. alias: {
  62. '@': resolve('src')
  63. }
  64. },
  65. devtool: NODE_ENV === 'development' ? 'source-map' : '',
  66. plugins: [
  67. // stylelint 配置
  68. new StyleLintPlugin({
  69. files: ['src/**/*.{vue,html,css,scss}'],
  70. fix: true, // 是否自动修复
  71. failOnError: false
  72. }),
  73. new CompressionPlugin({
  74. algorithm: 'gzip', // 使用gzip压缩
  75. test: /\.js$|\.html$|\.css$/, // 匹配文件名
  76. minRatio: 0.8, // 压缩率小于0.8才会压缩
  77. threshold: 10240, // 对超过10k的数据压缩
  78. deleteOriginalAssets: false // 是否删除未压缩的源文件,谨慎设置,如果希望提供非gzip的资源,可不设置或者设置为false(比如删除打包后的gz后还可以加载到原始资源文件)
  79. })
  80. ]
  81. },
  82. chainWebpack(config) {
  83. // 启用预加载,提高首屏加载速度
  84. config.plugin('preload').tap(() => [
  85. {
  86. rel: 'preload',
  87. // 忽略 runtime.js
  88. // https://github.com/vuejs/vue-cli/blob/dev/packages/@vue/cli-service/lib/config/app.js#L171
  89. fileBlacklist: [/\.map$/, /hot-update\.js$/, /runtime\..*\.js$/],
  90. include: 'initial'
  91. }
  92. ]);
  93. // 当页面很多时,prefetch 将导致太多无意义的请求,开启这个
  94. config.plugins.delete('prefetch');
  95. // 设置 svg-sprite-loader
  96. config.module.rule('svg').exclude.add(resolve('src/icons')).end();
  97. config.module
  98. .rule('icons')
  99. .test(/\.svg$/)
  100. .include.add(resolve('src/icons'))
  101. .end()
  102. .use('svg-sprite-loader')
  103. .loader('svg-sprite-loader')
  104. .options({
  105. symbolId: 'icon-[name]'
  106. })
  107. .end();
  108. config.when(NODE_ENV !== 'development', () => {
  109. config
  110. .plugin('ScriptExtHtmlWebpackPlugin')
  111. .after('html')
  112. .use('script-ext-html-webpack-plugin', [
  113. {
  114. // `runtime`必须与runtimeChunk名称相同。默认是“运行时”
  115. inline: /runtime\..*\.js$/
  116. }
  117. ])
  118. .end();
  119. config.optimization.splitChunks({
  120. chunks: 'all',
  121. cacheGroups: {
  122. libs: {
  123. name: 'chunk-libs',
  124. test: /[\\/]node_modules[\\/]/,
  125. priority: 10,
  126. chunks: 'initial' // 仅打包最初依赖的第三方
  127. },
  128. elementUI: {
  129. name: 'chunk-elementUI', // 将elementUI拆分为一个包
  130. priority: 20, // 权重必须大于libs和app,否则将被打包到libs或app中
  131. test: /[\\/]node_modules[\\/]_?element-ui(.*)/ // in order to adapt to cnpm
  132. },
  133. commons: {
  134. name: 'chunk-commons',
  135. test: resolve('src/components'), // 可以自定义规则
  136. minChunks: 3, // 最小公用数
  137. priority: 5,
  138. reuseExistingChunk: true
  139. }
  140. }
  141. });
  142. // https:// webpack.js.org/configuration/optimization/#optimizationruntimechunk
  143. config.optimization.runtimeChunk('single');
  144. });
  145. }
  146. };