webpack.dev.conf.js 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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
  35. ? { warnings: false, errors: true }
  36. : false,
  37. publicPath: config.dev.assetsPublicPath,
  38. proxy: config.dev.proxyTable,
  39. quiet: true, // necessary for FriendlyErrorsPlugin
  40. watchOptions: {
  41. poll: config.dev.poll,
  42. }
  43. },
  44. plugins: [
  45. new webpack.DefinePlugin({
  46. 'process.env': require('../config/dev.env')
  47. }),
  48. new webpack.HotModuleReplacementPlugin(),
  49. new webpack.NamedModulesPlugin(), // HMR shows correct file names in console on update.
  50. new webpack.NoEmitOnErrorsPlugin(),
  51. // https://github.com/ampedandwired/html-webpack-plugin
  52. new HtmlWebpackPlugin({
  53. filename: 'index.html',
  54. template: 'index.html',
  55. inject: true,
  56. favicon: path.resolve('src/assets/favicon.ico')
  57. }),
  58. // copy custom static assets
  59. new CopyWebpackPlugin([
  60. {
  61. from: path.resolve(__dirname, '../static'),
  62. to: config.dev.assetsSubDirectory,
  63. ignore: ['.*']
  64. }
  65. ])
  66. ]
  67. })
  68. module.exports = new Promise((resolve, reject) => {
  69. portfinder.basePort = process.env.PORT || config.dev.port
  70. portfinder.getPort((err, port) => {
  71. if (err) {
  72. reject(err)
  73. } else {
  74. // publish the new Port, necessary for e2e tests
  75. process.env.PORT = port
  76. // add port to devServer config
  77. devWebpackConfig.devServer.port = port
  78. // Add FriendlyErrorsPlugin
  79. devWebpackConfig.plugins.push(new FriendlyErrorsPlugin({
  80. compilationSuccessInfo: {
  81. messages: [`Your application is running here: http://${devWebpackConfig.devServer.host}:${port}`],
  82. },
  83. onErrors: config.dev.notifyOnErrors
  84. ? utils.createNotifierCallback()
  85. : undefined
  86. }))
  87. resolve(devWebpackConfig)
  88. }
  89. })
  90. })