webpack.dev.conf.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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 baseWebpackConfig = require('./webpack.base.conf')
  7. const HtmlWebpackPlugin = require('html-webpack-plugin')
  8. const FriendlyErrorsPlugin = require('friendly-errors-webpack-plugin')
  9. const portfinder = require('portfinder')
  10. const appData = require('../data.json')
  11. const seller = appData.seller
  12. const goods = appData.goods
  13. const ratings = appData.ratings
  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. before(app) {
  23. app.get('/api/seller', function(req, res) {
  24. res.json({
  25. errno: 0,
  26. data: seller
  27. })
  28. });
  29. app.get('/api/goods', function(req, res) {
  30. res.json({
  31. errno: 0,
  32. data: goods
  33. })
  34. });
  35. app.get('/api/ratings', function(req, res) {
  36. res.json({
  37. errno: 0,
  38. data: ratings
  39. })
  40. });
  41. },
  42. clientLogLevel: 'warning',
  43. historyApiFallback: true,
  44. hot: true,
  45. host: process.env.HOST || config.dev.host,
  46. port: process.env.PORT || config.dev.port,
  47. open: config.dev.autoOpenBrowser,
  48. overlay: config.dev.errorOverlay ? {
  49. warnings: false,
  50. errors: true,
  51. } : false,
  52. publicPath: config.dev.assetsPublicPath,
  53. proxy: config.dev.proxyTable,
  54. quiet: true, // necessary for FriendlyErrorsPlugin
  55. watchOptions: {
  56. poll: config.dev.poll,
  57. }
  58. },
  59. plugins: [
  60. new webpack.DefinePlugin({
  61. 'process.env': require('../config/dev.env')
  62. }),
  63. new webpack.HotModuleReplacementPlugin(),
  64. new webpack.NamedModulesPlugin(), // HMR shows correct file names in console on update.
  65. new webpack.NoEmitOnErrorsPlugin(),
  66. // https://github.com/ampedandwired/html-webpack-plugin
  67. new HtmlWebpackPlugin({
  68. filename: 'index.html',
  69. template: 'index.html',
  70. inject: true
  71. }),
  72. ]
  73. })
  74. module.exports = new Promise((resolve, reject) => {
  75. portfinder.basePort = process.env.PORT || config.dev.port
  76. portfinder.getPort((err, port) => {
  77. if (err) {
  78. reject(err)
  79. } else {
  80. // publish the new Port, necessary for e2e tests
  81. process.env.PORT = port
  82. // add port to devServer config
  83. devWebpackConfig.devServer.port = port
  84. // Add FriendlyErrorsPlugin
  85. devWebpackConfig.plugins.push(new FriendlyErrorsPlugin({
  86. compilationSuccessInfo: {
  87. messages: [`Your application is running here: http://${config.dev.host}:${port}`],
  88. },
  89. onErrors: config.dev.notifyOnErrors
  90. ? utils.createNotifierCallback()
  91. : undefined
  92. }))
  93. resolve(devWebpackConfig)
  94. }
  95. })
  96. })