webpack.dev.conf.js 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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. mode: 'development',
  18. optimization: {
  19. removeAvailableModules: false
  20. },
  21. module: {
  22. rules: utils.styleLoaders({
  23. sourceMap: config.dev.cssSourceMap,
  24. usePostCSS: true
  25. })
  26. },
  27. // cheap-module-eval-source-map is faster for development
  28. devtool: config.dev.devtool,
  29. // these devServer options should be customized in /config/index.js
  30. devServer: {
  31. clientLogLevel: 'warning',
  32. historyApiFallback: true,
  33. hot: true,
  34. compress: true,
  35. host: HOST || config.dev.host,
  36. port: PORT || config.dev.port,
  37. open: config.dev.autoOpenBrowser,
  38. overlay: config.dev.errorOverlay
  39. ? { warnings: false, errors: true }
  40. : false,
  41. publicPath: config.dev.assetsPublicPath,
  42. proxy: config.dev.proxyTable,
  43. quiet: true, // necessary for FriendlyErrorsPlugin
  44. watchOptions: {
  45. poll: config.dev.poll
  46. }
  47. },
  48. plugins: [
  49. new webpack.DefinePlugin({
  50. 'process.env': require('../config/dev.env')
  51. }),
  52. new webpack.HotModuleReplacementPlugin(),
  53. // https://github.com/ampedandwired/html-webpack-plugin
  54. new HtmlWebpackPlugin({
  55. filename: 'index.html',
  56. template: 'index.html',
  57. inject: true,
  58. favicon: resolve('favicon.ico'),
  59. title: 'wukong'
  60. })
  61. ]
  62. })
  63. module.exports = new Promise((resolve, reject) => {
  64. portfinder.basePort = process.env.PORT || config.dev.port
  65. portfinder.getPort((err, port) => {
  66. if (err) {
  67. reject(err)
  68. } else {
  69. // publish the new Port, necessary for e2e tests
  70. process.env.PORT = port
  71. // add port to devServer config
  72. devWebpackConfig.devServer.port = port
  73. // Add FriendlyErrorsPlugin
  74. devWebpackConfig.plugins.push(
  75. new FriendlyErrorsPlugin({
  76. compilationSuccessInfo: {
  77. messages: [
  78. `Your application is running here: http://${
  79. devWebpackConfig.devServer.host
  80. }:${port}`
  81. ]
  82. },
  83. onErrors: config.dev.notifyOnErrors
  84. ? utils.createNotifierCallback()
  85. : undefined
  86. })
  87. )
  88. resolve(devWebpackConfig)
  89. }
  90. })
  91. })