webpack.dev.conf.js 3.1 KB

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