webpack.dev.conf.js 3.0 KB

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