Registry / devops / prebuild-webpack-plugin

prebuild-webpack-plugin

JSON →
library1.1.1jsnpmunverified

Prebuild Webpack Plugin is a utility designed to execute file processing and I/O operations before a webpack build commences. It currently stands at stable version 1.1.1, with a history of steady, active development and releases addressing issues and improving performance. This plugin distinguishes itself by offering dedicated `build` and `watch` hooks that fire before the initial webpack compilation and on subsequent rebuilds, respectively. It supports file matching via minimatch patterns, allowing for targeted pre-processing. A key feature is the ability to explicitly add matched files to webpack's dependency tree and specific handling for complex build environments like Next.js through the `compilationNameFilter` option, enabling users to optimize for client-side or server-side compilations. It is particularly useful for scenarios requiring data transformation or external resource fetching prior to bundling, providing a flexible interface for such tasks.

npm install prebuild-webpack-plugin
INSTALL
IMPORT
SIG · PREBUILD-WEBPACK-P
P
prebuild-webpack-plugin
devopsjavascriptv1.1.1
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.

PrebuildPlugin
import PrebuildPlugin from 'prebuild-webpack-plugin';
import { PrebuildWebpackPlugin } from 'prebuild-webpack-plugin';
Since v1.0.0, the plugin is the default export. Prior to v1.0.0, it was a named export `PrebuildWebpackPlugin` from the scoped package `@hashicorp/prebuild-webpack-plugin`. Ensure you are importing the default export directly.
PrebuildPlugin (CommonJS)
const PrebuildPlugin = require('prebuild-webpack-plugin');
const { PrebuildWebpackPlugin } = require('@hashicorp/prebuild-webpack-plugin');
For CommonJS, the default export of `prebuild-webpack-plugin` is directly assigned. The old `@hashicorp` scoped package and named export are deprecated since v1.0.0.

This quickstart demonstrates how to integrate `PrebuildPlugin` into a webpack configuration. It sets up `build` and `watch` hooks to process markdown files in the `src` directory before the initial build and upon subsequent changes, logging their content. It also configures file matching and adds matched files as webpack dependencies.

const PrebuildPlugin = require('prebuild-webpack-plugin'); const path = require('path'); const fs = require('fs/promises'); module.exports = { // ... webpack config ... plugins: [ new PrebuildPlugin({ build: async (compiler, compilation, matchedFiles) => { console.log('Prebuild phase started!'); // Example: Read and log content of matched markdown files for (const file of matchedFiles) { const content = await fs.readFile(file, 'utf8'); console.log(`Prebuilt content of ${path.basename(file)}:\n${content.substring(0, 100)}...`); } console.log('Prebuild phase completed!'); }, watch: async (compiler, compilation, changedFile) => { console.log(`File changed in watch mode: ${changedFile}`); // Example: Process the changed file on each rebuild if (changedFile.endsWith('.md')) { const content = await fs.readFile(changedFile, 'utf8'); console.log(`Watch-mode processing for ${path.basename(changedFile)}:\n${content.substring(0, 50)}...`); } }, files: { pattern: '**/*.md', options: { cwd: path.resolve(__dirname, 'src') }, addFilesAsDependencies: true }, clearCacheOnUpdate: false }), ], };
Debug
Known issues
breakingThe npm package name changed from `@hashicorp/prebuild-webpack-plugin` to `prebuild-webpack-plugin`. Projects using the old package name will fail to resolve the module.
fix
Update your `package.json` dependencies and `import`/`require` statements to use the new package name: `prebuild-webpack-plugin`.
affects: >=1.0.0
breakingThe plugin's export method changed from a named export (`PrebuildWebpackPlugin`) to a default export. Incorrect import statements will lead to errors like `TypeError: PrebuildPlugin is not a constructor`.
fix
Change your import statement from `import { PrebuildWebpackPlugin } from '...'` to `import PrebuildPlugin from '...'` for ESM, or `const PrebuildPlugin = require('...')` for CommonJS.
affects: >=1.0.0
gotchaWhen using symlinks with the `files` matching option, unusual behavior could occur prior to v1.1.0, specifically regarding how file paths were resolved.
fix
Upgrade to v1.1.0 or later. If you need to explicitly replicate the old behavior (i.e., resolve the original path of a symlink), pass `realpath: true` to `files.options`.
affects: <1.1.0
gotchaEnabling the `clearCacheOnUpdate` option to handle newly added files in watch mode incurs a 'reasonably large performance penalty' because it forces a re-read of the entire file tree on every watch event.
fix
Use `clearCacheOnUpdate: true` only when strictly necessary for dynamic file additions in development. For static file sets or less frequent additions, it's more performant to restart the build process.
affects: >=1.0.0
gotchaIn multi-compilation environments like Next.js, the plugin might execute its logic multiple times (e.g., once for the client and once for the server) leading to redundant work.
fix
Use the `compilationNameFilter` option to specify which webpack compilation should execute the plugin's logic. For Next.js, you might pass `'client'` to run only for the client-side build.
affects: >=0.0.4
Errors
Common errors & fixes
TypeError: (0, prebuild_webpack_plugin_1.PrebuildWebpackPlugin) is not a constructor
Attempting to import `PrebuildWebpackPlugin` as a named export after v1.0.0, when it became a default export.
fix
Update your import statement to `import PrebuildPlugin from 'prebuild-webpack-plugin';` for ESM or `const PrebuildPlugin = require('prebuild-webpack-plugin');` for CommonJS.
Error: Cannot find module '@hashicorp/prebuild-webpack-plugin'
The package name changed in v1.0.0, but the old scoped package name is still being referenced.
fix
Update your `package.json` to use `prebuild-webpack-plugin` and ensure all import/require statements also use the new package name.
Files added to the watched directory are not being detected by the 'watch' hook, or 'matchedFiles' in 'build' is missing new entries after adding new files without restarting.
By default, the plugin caches the file tree from initial bootup. Newly added files are not automatically detected in subsequent watch runs unless `clearCacheOnUpdate` is enabled.
fix
For development workflows where new files are frequently added, set `clearCacheOnUpdate: true` in the plugin options. Be aware of the potential performance impact. For production builds or less dynamic scenarios, restart the webpack process after adding new files.
Upgrade
Version history
1.1.1latest on npm
Audit
Dependencies
webpackrequiredAs a webpack plugin, webpack itself is a fundamental peer dependency for its operation. It is expected to be provided by the consuming project to avoid duplicate installations and ensure compatibility.
Agent activity
6 hits · last 30 days
node
6
Resources
prebuild-webpack-plugin — npm install prebuild-webpack-plugin · libregistry