webpack.dev.conf.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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 PORT = process.env.PORT && Number(process.env.PORT)
  13. const devWebpackConfig = merge(baseWebpackConfig, {
  14. module: {
  15. rules: utils.styleLoaders({ sourceMap: config.dev.cssSourceMap, usePostCSS: true })
  16. },
  17. // cheap-module-eval-source-map is faster for development
  18. devtool: config.dev.devtool,
  19. // these devServer options should be customized in /config/index.js
  20. devServer: {
  21. clientLogLevel: 'warning',
  22. historyApiFallback: {
  23. rewrites: [
  24. { from: /.*/, to: path.posix.join(config.dev.assetsPublicPath, 'index.html') },
  25. ],
  26. },
  27. hot: true,
  28. contentBase: false, // since we use CopyWebpackPlugin.
  29. compress: true,
  30. host: config.dev.host,
  31. port: config.dev.port,
  32. open: config.dev.autoOpenBrowser,
  33. overlay: config.dev.errorOverlay
  34. ? { warnings: false, errors: true }
  35. : false,
  36. publicPath: config.dev.assetsPublicPath,
  37. proxy: config.dev.proxyTable,
  38. quiet: true, // necessary for FriendlyErrorsPlugin
  39. watchOptions: {
  40. poll: config.dev.poll,
  41. }
  42. },
  43. plugins: [
  44. new webpack.DefinePlugin({
  45. 'process.env': require('../config/dev.env')
  46. }),
  47. new webpack.HotModuleReplacementPlugin(),
  48. new webpack.NamedModulesPlugin(), // HMR shows correct file names in console on update.
  49. new webpack.NoEmitOnErrorsPlugin(),
  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. new CopyWebpackPlugin([
  65. { from: 'node_modules/@liveqing/liveplayer/dist/component/crossdomain.xml'},
  66. { from: 'node_modules/@liveqing/liveplayer/dist/component/liveplayer.swf'},
  67. { from: 'node_modules/@liveqing/liveplayer/dist/component/liveplayer-lib.min.js', to: config.build.assetsSubDirectory + '/js/'},
  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. })