webpack.dev.conf.js 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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. devtool: config.dev.devtool,
  19. devServer: {
  20. clientLogLevel: 'warning',
  21. historyApiFallback: {
  22. rewrites: [
  23. { from: /.*/, to: path.posix.join(config.dev.assetsPublicPath, 'index.html') },
  24. ],
  25. },
  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,
  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(),
  47. new webpack.NoEmitOnErrorsPlugin(),
  48. new HtmlWebpackPlugin({
  49. filename: 'index.html',
  50. template: 'index.html',
  51. inject: true
  52. }),
  53. new CopyWebpackPlugin([
  54. {
  55. from: path.resolve(__dirname, '../static'),
  56. to: config.dev.assetsSubDirectory,
  57. ignore: ['.*']
  58. }
  59. ])
  60. ]
  61. })
  62. module.exports = new Promise((resolve, reject) => {
  63. portfinder.basePort = process.env.PORT || config.dev.port
  64. portfinder.getPort((err, port) => {
  65. if (err) {
  66. reject(err)
  67. } else {
  68. process.env.PORT = port
  69. devWebpackConfig.devServer.port = port
  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()
  76. : undefined
  77. }))
  78. resolve(devWebpackConfig)
  79. }
  80. })
  81. })