How do you configure webpack-dev-server?

Beginner

Answer

module.exports = {
  devServer: {
    static: './dist',
    port: 3000,
    open: true,
    hot: true,
    compress: true,
    historyApiFallback: true, // For SPA routing
    proxy: {
      '/api': {
        target: 'http://localhost:8080',
        changeOrigin: true
      }
    }
  }
};

Key options:

  • static: Serve static files from directory
  • hot: Enable HMR
  • proxy: Proxy API requests to another server
  • historyApiFallback: Handle client-side routing