webpack.dev.conf.js 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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.optimize.CommonsChunkPlugin({ // new added
  40. // names: ['vendor', 'manifest']
  41. // }),
  42. new webpack.HotModuleReplacementPlugin(),
  43. new webpack.NamedModulesPlugin(), // HMR shows correct file names in console on update.
  44. new webpack.NoEmitOnErrorsPlugin(),
  45. // https://github.com/ampedandwired/html-webpack-plugin
  46. new HtmlWebpackPlugin({
  47. filename: 'index.html',
  48. template: 'index.html',
  49. inject: true
  50. }),
  51. ]
  52. })
  53. module.exports = new Promise((resolve, reject) => {
  54. portfinder.basePort = process.env.PORT || config.dev.port
  55. portfinder.getPort((err, port) => {
  56. if (err) {
  57. reject(err)
  58. } else {
  59. // publish the new Port, necessary for e2e tests
  60. process.env.PORT = port
  61. // add port to devServer config
  62. devWebpackConfig.devServer.port = port
  63. console.log(port);
  64. // Add FriendlyErrorsPlugin
  65. devWebpackConfig.plugins.push(new FriendlyErrorsPlugin({
  66. compilationSuccessInfo: {
  67. messages: [`Your application is running here: http://${config.dev.host}:${port}`],
  68. },
  69. onErrors: config.dev.notifyOnErrors
  70. ? utils.createNotifierCallback()
  71. : undefined
  72. }))
  73. resolve(devWebpackConfig)
  74. }
  75. })
  76. })