vue.config.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. 'use strict'
  2. const path = require('path')
  3. const defaultSettings = require('./src/settings.js')
  4. function resolve(dir) {
  5. return path.join(__dirname, dir)
  6. }
  7. // 项目名称
  8. const name = defaultSettings.title
  9. // 开发环境
  10. const isDev = process.env.NODE_ENV === 'development'
  11. // 运行端口
  12. const port = process.env.port || process.env.npm_config_port || 9527 // dev port
  13. module.exports = {
  14. publicPath: '/',
  15. outputDir: 'dist',
  16. assetsDir: 'static',
  17. // 开发模式实时校验
  18. lintOnSave: isDev,
  19. productionSourceMap: false,
  20. devServer: {
  21. port: port,
  22. open: true,
  23. overlay: {
  24. warnings: false,
  25. errors: true
  26. }
  27. },
  28. configureWebpack: {
  29. name: name,
  30. resolve: {
  31. alias: {
  32. '@': resolve('src')
  33. }
  34. }
  35. },
  36. chainWebpack(config) {
  37. config.plugins.delete('preload') // TODO: need test
  38. config.plugins.delete('prefetch') // TODO: need test
  39. // set svg-sprite-loader
  40. config.module
  41. .rule('svg')
  42. .exclude.add(resolve('src/icons'))
  43. .end()
  44. config.module
  45. .rule('icons')
  46. .test(/\.svg$/)
  47. .include.add(resolve('src/icons'))
  48. .end()
  49. .use('svg-sprite-loader')
  50. .loader('svg-sprite-loader')
  51. .options({
  52. symbolId: 'icon-[name]'
  53. })
  54. .end()
  55. // set preserveWhitespace
  56. config.module
  57. .rule('vue')
  58. .use('vue-loader')
  59. .loader('vue-loader')
  60. .tap(options => {
  61. options.compilerOptions.preserveWhitespace = true
  62. return options
  63. })
  64. .end()
  65. // 开发环境
  66. config
  67. // https://webpack.js.org/configuration/devtool/#development
  68. .when(isDev,
  69. config => config.devtool('source-map')
  70. )
  71. // 生产环境
  72. config
  73. .when(!isDev,
  74. config => {
  75. config
  76. .plugin('ScriptExtHtmlWebpackPlugin')
  77. .after('html')
  78. .use('script-ext-html-webpack-plugin', [{
  79. // `runtime` must same as runtimeChunk name. default is `runtime`
  80. inline: /runtime\..*\.js$/
  81. }])
  82. .end()
  83. config
  84. .optimization.splitChunks({
  85. chunks: 'all',
  86. cacheGroups: {
  87. libs: {
  88. name: 'chunk-libs',
  89. test: /[\\/]node_modules[\\/]/,
  90. priority: 10,
  91. chunks: 'initial' // only package third parties that are initially dependent
  92. },
  93. elementUI: {
  94. name: 'chunk-elementUI', // split elementUI into a single package
  95. priority: 20, // the weight needs to be larger than libs and app or it will be packaged into libs or app
  96. test: /[\\/]node_modules[\\/]_?element-ui(.*)/ // in order to adapt to cnpm
  97. },
  98. commons: {
  99. name: 'chunk-commons',
  100. test: resolve('src/components'), // can customize your rules
  101. minChunks: 3, // minimum common number
  102. priority: 5,
  103. reuseExistingChunk: true
  104. }
  105. }
  106. })
  107. config.optimization.runtimeChunk('single')
  108. }
  109. )
  110. }
  111. }