vue.config.js 4.0 KB

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