vue.config.js 3.8 KB

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