Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
muslnode 18–226 runs
build_error
glibcnode 18–226 runs
build_error
Code
Verified usage
Verified import paths — ran on the pinned version, not inferred.
compile
✓ import { compile } from 'webpack-build-utils'
✗ const { compile } = require('webpack-build-utils')
The library primarily uses ES module syntax for imports. CommonJS require() is not officially supported and may not work as expected with all utilities.
createLoader
✓ import { createLoader } from 'webpack-build-utils'
✗ import createLoader from 'webpack-build-utils/createLoader'
All public utilities are exposed as named exports from the main package entry point.
getMemfsCompiler5
✓ import { getMemfsCompiler5 } from 'webpack-build-utils'
✗ import { getMemfsCompiler } from 'webpack-build-utils'
Ensure you use the correct version-specific function (e.g., `getMemfsCompiler5` for Webpack 5) as signatures or availability might differ.
Demonstrates how to use `webpack-build-utils` to programmatically compile a webpack configuration, inspect its assets, and extract compilation errors and warnings for testing or analysis.
import webpack from 'webpack';
import {
compile,
createLoader,
getErrors,
getMemfsCompiler5,
getWarnings,
readAssets
} from 'webpack-build-utils';
// In a real test environment, 'expect' would be globally available or imported from a test runner like Jest.
// For this example, we'll log outputs directly.
async function runWebpackBuildUtilsExample() {
const webpackConfig = {
mode: 'development',
entry: './src/index.js', // Ensure this file exists for a real run
output: {
filename: 'bundle.js',
path: '/tmp/test-output', // A temporary path for output (e.g., for memfs or fs-based compilation)
},
module: {
rules: [
{
test: /\.js$/,
use: createLoader(function (source, map, meta) {
// This is a simple identity loader; in a real scenario, it would transform source.
console.log(`Loader invoked for a JS module. Source length: ${source.length}`);
return source;
})
}
]
},
plugins: [
new webpack.DefinePlugin({
'process.env.NODE_ENV': JSON.stringify('development')
})
]
};
// To run this, you'll typically have 'webpack' installed and import it.
// Example for a filesystem-based compiler:
const compiler = webpack(webpackConfig);
// Example for an in-memory filesystem compiler (Webpack 5+ specific):
// const compiler = getMemfsCompiler5(webpackConfig);
try {
console.log("Starting webpack compilation using webpack-build-utils...");
const stats = await compile(compiler);
if (stats) {
// Log compiled assets
console.log('\n--- Compiled Assets ---');
const assets = readAssets(compiler, stats);
Object.entries(assets).forEach(([name, content]) => {
console.log(`Asset: ${name} (length: ${content.length})`);
// console.log(content.substring(0, Math.min(content.length, 100)) + '...'); // Log first 100 chars
});
// Log errors
console.log('\n--- Compilation Errors ---');
const errors = getErrors(stats);
if (errors.length > 0) {
errors.forEach((err, i) => console.error(`Error ${i + 1}:`, err));
} else {
console.log('No compilation errors.');
}
// Log warnings
console.log('\n--- Compilation Warnings ---');
const warnings = getWarnings(stats);
if (warnings.length > 0) {
warnings.forEach((warn, i) => console.warn(`Warning ${i + 1}:`, warn));
} else {
console.log('No compilation warnings.');
}
} else {
console.error("Compilation failed and returned no stats.");
}
} catch (error) {
console.error("An error occurred during compilation:", error);
}
}
runWebpackBuildUtilsExample();
Errors
Common errors & fixes
Cannot find module 'webpack-build-utils'
The package has not been installed or is incorrectly referenced in your project's dependencies.
fixRun `npm install --save-dev webpack-build-utils` or `yarn add -D webpack-build-utils` to install it as a dev dependency.
TypeError: compiler.run is not a function
The `webpack` package is either not installed, or the provided 'compiler' object is not a valid webpack compiler instance.
fixEnsure `webpack` is installed (`npm install webpack`) and that you are passing an actual webpack compiler instance (e.g., `webpack(webpackConfig)`) to the utility functions like `compile`.
Audit
Dependencies
webpackrequiredRequired for all core functionality; this library operates on webpack compiler instances and configurations. It's often installed as a dev dependency in projects that use webpack.