webpack.dev.conf.js 3.2 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. 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. 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. // Add FriendlyErrorsPlugin
  75. devWebpackConfig.plugins.push(new FriendlyErrorsPlugin({
  76. compilationSuccessInfo: {
  77. messages: [`Your application is running here: http://${devWebpackConfig.devServer.host}:${port}`],
  78. },
  79. onErrors: config.dev.notifyOnErrors ?
  80. utils.createNotifierCallback() : undefined
  81. }))
  82. resolve(devWebpackConfig)
  83. }
  84. })
  85. })