Registry / testing / webpack-build-utils

webpack-build-utils

JSON →
library0.0.7jsnpmunverified

The `webpack-build-utils` package, currently at version 0.0.7, serves as a specialized toolkit for developers working with webpack configurations, custom loaders, and build testing environments. It offers a set of pragmatic utilities to programmatically interact with webpack, such as `compile` for executing a webpack compiler instance, `getMemfsCompiler5` for obtaining an in-memory filesystem compiler (specifically for Webpack 5), and functions like `getErrors` and `getWarnings` to parse compilation statistics. Additionally, it simplifies asset inspection with `readAsset` and `readAssets`. A notable feature is `createLoader`, which provides a concise API for rapidly prototyping or testing webpack loaders. Due to its very early stage of development (version 0.0.7), the package does not yet have a defined release cadence, and its API should be considered experimental and subject to change. It differentiates itself by focusing specifically on the programmatic testing and inspection aspects of webpack builds rather than general webpack configuration.

npm install webpack-build-utils
INSTALL
IMPORT
SIG · WEBPACK-BUILD-UTIL
W
webpack-build-utils
testingjavascriptv0.0.7
Install
Import
Disk
Pass rate
0/ 6
Env Coverage0 / 6
glibc
1822
musl
1822
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
musl
node 18226 runs
build_error
glibc
node 18226 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();
Debug
Known issues
breakingThe package is in a very early development stage (v0.0.7), meaning its API is highly unstable and subject to frequent breaking changes without prior major version bumps. Semantic Versioning is not strictly followed before v1.0.0.
fix
Refer to the latest GitHub README and source code for up-to-date API usage. Consider pinning to exact patch versions if stability is critical.
affects: <1.0.0
gotchaThe `getMemfsCompiler5` utility is specifically designed for Webpack 5. Using it with other Webpack versions (e.g., Webpack 4 or 6) may lead to unexpected behavior or errors due to API differences.
fix
Ensure your project explicitly uses Webpack 5 when utilizing `getMemfsCompiler5`. For other Webpack versions, you might need to use standard webpack compilation or find alternative in-memory compiler solutions.
affects: all
gotchaThis library is primarily designed for Node.js environments and build-time operations with webpack. Its utilities are not intended for use in client-side browser contexts.
fix
Ensure `webpack-build-utils` is only used in Node.js-based build scripts, test environments, or CI/CD pipelines, not directly in front-end application code.
affects: all
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.
fix
Run `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.
fix
Ensure `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`.
Upgrade
Version history
0.0.7latest on npm
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.
Agent activity
15 hits · last 30 days
node
12
Resources
webpack-build-utils — npm install webpack-build-utils · libregistry