webpack.dev.conf.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. 'use strict'
  2. const path = require('path')
  3. const utils = require('./utils')
  4. const webpack = require('webpack')
  5. const config = require('../config')
  6. const merge = require('webpack-merge')
  7. const baseWebpackConfig = require('./webpack.base.conf')
  8. const HtmlWebpackPlugin = require('html-webpack-plugin')
  9. const FriendlyErrorsPlugin = require('friendly-errors-webpack-plugin')
  10. const portfinder = require('portfinder')
  11. function resolve (dir) {
  12. return path.join(__dirname, '..', dir)
  13. }
  14. const HOST = process.env.HOST
  15. const PORT = process.env.PORT && Number(process.env.PORT)
  16. const devWebpackConfig = merge(baseWebpackConfig, {
  17. module: {
  18. rules: utils.styleLoaders({ sourceMap: config.dev.cssSourceMap, usePostCSS: true })
  19. },
  20. // cheap-module-eval-source-map is faster for development
  21. devtool: config.dev.devtool,
  22. // these devServer options should be customized in /config/index.js
  23. devServer: {
  24. clientLogLevel: 'warning',
  25. historyApiFallback: true,
  26. hot: true,
  27. compress: true,
  28. host: HOST || config.dev.host,
  29. port: PORT || config.dev.port,
  30. open: config.dev.autoOpenBrowser,
  31. overlay: config.dev.errorOverlay
  32. ? { warnings: false, errors: true }
  33. : false,
  34. publicPath: config.dev.assetsPublicPath,
  35. proxy: config.dev.proxyTable,
  36. quiet: true, // necessary for FriendlyErrorsPlugin
  37. watchOptions: {
  38. poll: config.dev.poll,
  39. }
  40. },
  41. plugins: [
  42. new webpack.DefinePlugin({
  43. 'process.env': require('../config/dev.env')
  44. }),
  45. new webpack.HotModuleReplacementPlugin(),
  46. new webpack.NamedModulesPlugin(), // HMR shows correct file names in console on update.
  47. new webpack.NoEmitOnErrorsPlugin(),
  48. // https://github.com/ampedandwired/html-webpack-plugin
  49. new HtmlWebpackPlugin({
  50. filename: 'index.html',
  51. template: 'index.html',
  52. inject: true,
  53. favicon: resolve('favicon.ico'),
  54. title: 'vue-element-admin'
  55. }),
  56. ]
  57. })
  58. module.exports = new Promise((resolve, reject) => {
  59. portfinder.basePort = process.env.PORT || config.dev.port
  60. portfinder.getPort((err, port) => {
  61. if (err) {
  62. reject(err)
  63. } else {
  64. // publish the new Port, necessary for e2e tests
  65. process.env.PORT = port
  66. // add port to devServer config
  67. devWebpackConfig.devServer.port = port
  68. // Add FriendlyErrorsPlugin
  69. devWebpackConfig.plugins.push(new FriendlyErrorsPlugin({
  70. compilationSuccessInfo: {
  71. messages: [`Your application is running here: http://${devWebpackConfig.devServer.host}:${port}`],
  72. },
  73. onErrors: config.dev.notifyOnErrors
  74. ? utils.createNotifierCallback()
  75. : undefined
  76. }))
  77. resolve(devWebpackConfig)
  78. }
  79. })
  80. })