Development server with hot module replacement, middleware support, and error overlay for optimal development experience.
Start development server with hot reloading and comprehensive development features.
/**
* Start development server with hot reloading
* @param opts - Development server configuration
*/
function dev(opts: {
webpackConfig: object | object[];
_beforeServerWithApp?: (app: object) => void;
beforeMiddlewares?: Function[];
afterMiddlewares?: Function[];
beforeServer?: (server: object) => void;
afterServer?: (server: object, port: number) => void;
contentBase?: string;
onCompileDone?: (result: DevCompileResult) => void;
onFail?: (error: { stats: object }) => void;
proxy?: object;
port: number;
history?: object;
base?: string;
serverConfig?: object;
}): void;
interface DevCompileResult {
port: number;
isFirstCompile: boolean;
stats: object;
server: object;
urls: {
local: string; // Local terminal URL
lan: string; // LAN terminal URL
rawLocal: string; // Raw local browser URL
rawLanUrl: string; // Raw LAN URL
};
}Parameters:
webpackConfig (object | object[]): Webpack configuration object or array_beforeServerWithApp (function, optional): Internal callback for proxy middleware setupbeforeMiddlewares (Function[], optional): Express middlewares to run before built-in middlewaresafterMiddlewares (Function[], optional): Express middlewares to run after built-in middlewaresbeforeServer (function, optional): Callback executed before server starts listeningafterServer (function, optional): Callback executed after server starts listeningcontentBase (string, optional): Directory for static content servingonCompileDone (function, optional): Callback executed after successful compilationonFail (function, optional): Callback executed on compilation failureproxy (object, optional): Proxy configuration for API requestsport (number, required): Port number for the development serverhistory (object, optional): History API configurationbase (string, optional): Base path for the applicationserverConfig (object, optional): Additional webpack-dev-server configurationUsage Examples:
const dev = require('af-webpack/dev');
// Basic development server
dev({
webpackConfig: {
entry: './src/index.js',
output: {
path: './dist',
publicPath: '/'
}
},
port: 8000
});
// Advanced development server with middleware
dev({
webpackConfig,
port: 3000,
beforeMiddlewares: [
// Custom authentication middleware
(req, res, next) => {
console.log('Request:', req.url);
next();
}
],
afterMiddlewares: [
// Custom API routes
express.static('public')
],
proxy: {
'/api': {
target: 'http://localhost:5000',
changeOrigin: true
}
},
onCompileDone: ({ port, isFirstCompile, stats, server, urls }) => {
if (isFirstCompile) {
console.log(`Server running at ${urls.local}`);
}
console.log('Compilation completed in', stats.endTime - stats.startTime, 'ms');
},
onFail: ({ stats }) => {
console.error('Compilation failed');
stats.compilation.errors.forEach(err => console.error(err));
}
});The development server provides:
Default server configuration includes:
interface ServerConfig {
disableHostCheck: boolean; // Disable host header check
compress: boolean; // Enable gzip compression
clientLogLevel: string; // Client-side logging level
hot: boolean; // Enable hot module replacement
quiet: boolean; // Suppress webpack output
headers: object; // Custom response headers
publicPath: string; // Webpack public path
watchOptions: object; // File watching configuration
historyApiFallback: boolean; // History API fallback
overlay: boolean; // Error overlay display
host: string; // Server host
proxy: object; // Proxy configuration
https: boolean; // HTTPS enable
cert?: Buffer; // HTTPS certificate
key?: Buffer; // HTTPS private key
contentBase?: string; // Static content directory
}HOST: Server host (default: 0.0.0.0)HTTPS: Enable HTTPS serverCERT: Path to HTTPS certificate fileKEY: Path to HTTPS private key fileSYSTEM_BELL: Set to none to disable error sound notificationsSILENT: Suppress startup messagesCI: Continuous integration mode flagCONTENT_BASE: Static content directoryDevelopment server errors are handled through:
The server generates multiple URL formats for different use cases: