webpack.dev.conf.js 3.2 KB

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