1
0

webpack.dev.conf.js 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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. mode: 'development',
  16. module: {
  17. rules: utils.styleLoaders({ sourceMap: config.dev.cssSourceMap, usePostCSS: true })
  18. },
  19. // cheap-module-eval-source-map is faster for development
  20. devtool: config.dev.devtool,
  21. // these devServer options should be customized in /config/index.js
  22. devServer: {
  23. clientLogLevel: 'warning',
  24. historyApiFallback: {
  25. rewrites: [
  26. { from: /.*/, to: path.posix.join(config.dev.assetsPublicPath, 'index.html') },
  27. ],
  28. },
  29. hot: true,
  30. contentBase: false, // since we use CopyWebpackPlugin.
  31. compress: true,
  32. host: HOST || config.dev.host,
  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. },
  45. plugins: [
  46. new webpack.DefinePlugin({
  47. 'process.env': require('../config/dev.env')
  48. }),
  49. new webpack.HotModuleReplacementPlugin(),
  50. // https://github.com/ampedandwired/html-webpack-plugin
  51. new HtmlWebpackPlugin({
  52. filename: 'index.html',
  53. template: 'index.html',
  54. inject: true
  55. }),
  56. // copy custom static assets
  57. new CopyWebpackPlugin([
  58. {
  59. from: path.resolve(__dirname, '../static'),
  60. to: config.dev.assetsSubDirectory,
  61. ignore: ['.*']
  62. }
  63. ])
  64. ],
  65. optimization: {
  66. namedModules: true,
  67. noEmitOnErrors: true,
  68. },
  69. })
  70. module.exports = new Promise((resolve, reject) => {
  71. portfinder.basePort = process.env.PORT || config.dev.port
  72. portfinder.getPort((err, port) => {
  73. if (err) {
  74. reject(err)
  75. } else {
  76. // publish the new Port, necessary for e2e tests
  77. process.env.PORT = port
  78. // add port to devServer config
  79. devWebpackConfig.devServer.port = port
  80. // Add FriendlyErrorsPlugin
  81. devWebpackConfig.plugins.push(new FriendlyErrorsPlugin({
  82. compilationSuccessInfo: {
  83. messages: [`Your application is running here: http://${devWebpackConfig.devServer.host}:${port}`],
  84. },
  85. onErrors: config.dev.notifyOnErrors
  86. ? utils.createNotifierCallback()
  87. : undefined
  88. }))
  89. resolve(devWebpackConfig)
  90. }
  91. })
  92. })