vue.config.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. 'use strict'
  2. const path = require('path')
  3. require("babel-polyfill");
  4. const productionGzipExtensions = ['js', 'css'];
  5. const UglifyJsPlugin = require("uglifyjs-webpack-plugin");
  6. const CompressionWebpackPlugin = require('compression-webpack-plugin');
  7. const isProduction = process.env.NODE_ENV === 'production';
  8. function resolve(dir) {
  9. return path.join(__dirname, dir)
  10. }
  11. const port = process.env.port || process.env.npm_config_port || 8080
  12. module.exports = {
  13. publicPath: '/',
  14. outputDir: 'dist',
  15. assetsDir: 'static',
  16. lintOnSave: false,
  17. productionSourceMap: false,
  18. devServer: {
  19. port: port,
  20. open: true,
  21. overlay: {
  22. warnings: false,
  23. errors: true
  24. },
  25. },
  26. configureWebpack: config => {
  27. // 入口文件
  28. config.entry.app = ["babel-polyfill", "./src/main.js"];
  29. config.externals = {
  30. 'vue': 'Vue',
  31. axios: "axios",
  32. 'vue-router': 'VueRouter'
  33. };
  34. // if (isProduction) {
  35. config.plugins.push(new CompressionWebpackPlugin({
  36. algorithm: 'gzip',
  37. test: new RegExp('\\.(' + productionGzipExtensions.join('|') + ')$'),
  38. threshold: 10240,
  39. minRatio: 0.7
  40. }))
  41. // }
  42. let plugins = [
  43. new UglifyJsPlugin({
  44. uglifyOptions: {
  45. compress: {
  46. drop_console: true,
  47. drop_debugger: true
  48. },
  49. output: {
  50. comments: true
  51. }
  52. },
  53. cache: true,
  54. sourceMap: false,
  55. parallel: true
  56. })
  57. ];
  58. // if (isProduction) {
  59. config.plugins = [...config.plugins, ...plugins];
  60. // }
  61. },
  62. chainWebpack(config) {
  63. config.plugin('preload').tap(() => [
  64. {
  65. rel: 'preload',
  66. fileBlacklist: [/\.map$/, /hot-update\.js$/, /runtime\..*\.js$/],
  67. include: 'initial'
  68. }
  69. ])
  70. config.plugins.delete('prefetch')
  71. config.module
  72. .rule('svg')
  73. .exclude.add(resolve('src/icons'))
  74. .end()
  75. config.module
  76. .rule('icons')
  77. .test(/\.svg$/)
  78. .include.add(resolve('src/icons'))
  79. .end()
  80. .use('svg-sprite-loader')
  81. .loader('svg-sprite-loader')
  82. .options({
  83. symbolId: 'icon-[name]'
  84. })
  85. .end()
  86. config
  87. .when(true,
  88. config => {
  89. config
  90. .plugin('ScriptExtHtmlWebpackPlugin')
  91. .after('html')
  92. .use('script-ext-html-webpack-plugin', [{
  93. inline: /runtime\..*\.js$/
  94. }])
  95. .end()
  96. config
  97. .optimization.splitChunks({
  98. chunks: 'all',
  99. cacheGroups: {
  100. libs: {
  101. name: 'chunk-libs',
  102. test: /[\\/]node_modules[\\/]/,
  103. priority: 10,
  104. chunks: 'initial'
  105. },
  106. elementUI: {
  107. name: 'chunk-elementUI',
  108. priority: 20,
  109. test: /[\\/]node_modules[\\/]_?element-ui(.*)/
  110. },
  111. commons: {
  112. name: 'chunk-commons',
  113. test: resolve('src/components'),
  114. minChunks: 3,
  115. priority: 5,
  116. reuseExistingChunk: true
  117. }
  118. }
  119. })
  120. config.optimization.runtimeChunk('single')
  121. }
  122. )
  123. }
  124. }