webpack.dev.conf.js 3.1 KB

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