Production build functionality with webpack compilation, asset optimization, and comprehensive error handling for deployment-ready applications.
Execute production build with webpack compilation and comprehensive error handling.
/**
* Execute production build with webpack
* @param opts - Build configuration options
*/
function build(opts: {
webpackConfig: object | object[];
cwd?: string;
onSuccess?: (result: { stats: object }) => void;
onFail?: (error: { err?: Error; stats?: object; rawStats?: object }) => void;
}): void;Parameters:
webpackConfig (object | object[]): Webpack configuration object or array of configurationscwd (string, optional): Current working directory (defaults to process.cwd())onSuccess (function, optional): Callback executed on successful buildonFail (function, optional): Callback executed on build failureUsage Examples:
const build = require('af-webpack/build');
// Basic production build
build({
webpackConfig: {
entry: './src/index.js',
output: {
path: './dist',
filename: 'bundle.js'
}
}
});
// Build with callbacks
build({
webpackConfig,
cwd: process.cwd(),
onSuccess: ({ stats }) => {
console.log('Build completed successfully');
console.log('Assets:', stats.toJson().assets);
},
onFail: ({ err, stats, rawStats }) => {
console.error('Build failed:', err);
if (stats && stats.hasErrors()) {
console.error('Compilation errors:', stats.compilation.errors);
}
}
});
// Multi-configuration build
build({
webpackConfig: [
// Client configuration
{
entry: './src/client.js',
output: { path: './dist/client' }
},
// Server configuration
{
entry: './src/server.js',
output: { path: './dist/server' }
}
]
});The build system includes:
CLEAR_OUTPUT environment variable)Build errors are provided to the onFail callback with detailed information:
interface BuildError {
err?: Error; // Primary error object
stats?: object; // Webpack stats for single config
rawStats?: object; // Raw webpack stats (useful for multi-config)
}CLEAR_OUTPUT: Set to none to disable output directory cleaningUMI_TEST: Prevents process exit on build failure during testingThe build system automatically displays file sizes after successful compilation: