Webpack Build Wrapper
The `webpack-build` package, currently at version 1.0.1, serves as a comprehensive wrapper around Webpack, streamlining its integration into broader build systems. It distinguishes itself by offering a suite of advanced features not native to base Webpack, including the ability to run multiple concurrent Webpack compilers leveraging multiple worker processes, robust persistent caching mechanisms for significantly faster rebuilds, and a built-in build server with HMR (Hot Module Replacement) support. This tool is designed to reduce boilerplate and standardize complex build configurations by providing specific configuration hooks and a dynamic way to construct Webpack configs based on various build flags and options. It allows for overriding standard Webpack `output.path` and `publicPath`, and manages asset paths with `staticRoot` and `staticUrl` options. While its release cadence isn't explicitly stated, its 1.x version implies a stable and mature codebase focused on enhancing the Webpack development experience through structured extensibility and performance optimizations.
Common errors
-
Cannot find module 'webpack-build'
cause The `webpack-build` package is not installed or not resolvable in the current environment.fixnpm install webpack-build -
Error: Cannot find module 'webpack' from '/path/to/node_modules/webpack-build'
cause The `webpack` peer dependency is missing from your project's `node_modules`.fixnpm install webpack -
TypeError: config is not a function
cause Your Webpack configuration file (`config` option) exports a static object instead of a function.fixModify your webpack config file to `module.exports = function(opts) { return { /* webpack config */ }; };`
Warnings
- gotcha This package requires `webpack` as a peer dependency. Ensure it is installed in your project, compatible with `webpack-build`'s expected version range (>= 1.9 for v1.x of `webpack-build`).
- gotcha Webpack configuration files provided to `webpack-build` must export a function that accepts an `options` object, rather than a direct Webpack configuration object. This function allows dynamic configuration based on build options.
- gotcha The `buildHash` provided in the `data` object upon build completion is an internal identifier used by `webpack-build` for caching and HMR. It is distinct from and unrelated to any content hashes generated by Webpack itself.
- gotcha To pass arbitrary data into your webpack configuration function, use the `context` property within the options object passed to `build()`. This data will be available on `opts.context` inside your config function.
Install
-
npm install webpack-build -
yarn add webpack-build -
pnpm add webpack-build
Imports
- build
import build from 'webpack-build';
const build = require('webpack-build'); - build.options.defaults
import { options } from 'webpack-build';const build = require('webpack-build'); build.options.defaults = { /* ... */ };
Quickstart
const build = require('webpack-build');
const path = require('path');
const fs = require('fs');
// Create a dummy webpack.config.js for demonstration
const configPath = path.join(__dirname, 'webpack.config.js');
const dummyConfigContent = `
module.exports = function(opts) {
return {
entry: './src/index.js',
output: {
path: opts.outputPath || path.join(__dirname, 'dist'),
filename: 'bundle.js'
},
mode: 'development' // Webpack 4+ needs mode
};
};
`;
// Create a dummy entry file
const srcDir = path.join(__dirname, 'src');
if (!fs.existsSync(srcDir)) fs.mkdirSync(srcDir);
fs.writeFileSync(path.join(srcDir, 'index.js'), 'console.log("Hello from webpack-build!");');
fs.writeFileSync(configPath, dummyConfigContent);
console.log('Starting webpack-build...');
build({
config: configPath,
watch: false, // Set to true to enable watching
outputPath: path.join(__dirname, 'output'),
context: {
debug: true
}
}, function(err, data) {
if (err) {
console.error('Build failed:', err);
return;
}
console.log('Build successful!');
console.log('Generated assets:', data.assets);
// Clean up dummy files
fs.unlinkSync(path.join(srcDir, 'index.js'));
fs.rmdirSync(srcDir);
fs.unlinkSync(configPath);
fs.rmdirSync(path.join(__dirname, 'output'), { recursive: true });
});