vue.config.js 3.2 KB

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