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