webpack.dev.conf.js 2.4 KB

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