webpack.dev.conf.js 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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. }),
  57. // copy custom static assets
  58. new CopyWebpackPlugin([
  59. {
  60. from: path.resolve(__dirname, '../static'),
  61. to: config.dev.assetsSubDirectory,
  62. ignore: ['.*']
  63. }
  64. ])
  65. ]
  66. })
  67. module.exports = new Promise((resolve, reject) => {
  68. portfinder.basePort = process.env.PORT || config.dev.port
  69. portfinder.getPort((err, port) => {
  70. if (err) {
  71. reject(err)
  72. } else {
  73. // publish the new Port, necessary for e2e tests
  74. process.env.PORT = port
  75. // add port to devServer config
  76. devWebpackConfig.devServer.port = port
  77. // Add FriendlyErrorsPlugin
  78. devWebpackConfig.plugins.push(new FriendlyErrorsPlugin({
  79. compilationSuccessInfo: {
  80. messages: [`Your application is running here: http://${devWebpackConfig.devServer.host}:${port}`],
  81. },
  82. onErrors: config.dev.notifyOnErrors
  83. ? utils.createNotifierCallback()
  84. : undefined
  85. }))
  86. resolve(devWebpackConfig)
  87. }
  88. })
  89. })