HardSourceWebpackPlugin

raw JSON →
0.13.1 verified Sat Apr 25 auth: no javascript deprecated

HardSourceWebpackPlugin is a webpack plugin that provides intermediate caching of module compilation results to disk, drastically speeding up subsequent builds (second build is significantly faster). Version 0.13.1 is the latest stable release, last updated in 2020. It works by caching the output of webpack's module resolution and transformation steps, using a configurable cache directory and hash-based invalidation. Key differentiators: it caches at the module level (not just the entire bundle), supports parallel builds via ParallelModulePlugin, and allows excluding specific modules. However, it is no longer maintained and may have compatibility issues with modern webpack versions (5+), making it less suitable for new projects compared to persistent caching built into webpack 5.

error Module not found: Error: Can't resolve 'hard-source-webpack-plugin'
cause Package not installed or mistaken import path.
fix
Run npm install --save-dev hard-source-webpack-plugin and ensure the require path is correct.
error Error: HardSourceWebpackPlugin is not a constructor
cause Using ES6 import instead of CommonJS require.
fix
Change import HardSourceWebpackPlugin from ... to const HardSourceWebpackPlugin = require('hard-source-webpack-plugin');
error TypeError: Cannot read property 'tap' of undefined
cause Incompatible version of webpack; HardSourceWebpackPlugin may call hooks not present in webpack 5.
fix
Downgrade webpack to version 4, or migrate to webpack 5 persistent cache.
error Build fails silently with no errors after first build (assets not emitted)
cause Known issue with mini-css-extract-plugin where assets are not emitted on repeated builds.
fix
Add ExcludeModulePlugin to exclude mini-css-extract-loader paths. Example: new HardSourceWebpackPlugin.ExcludeModulePlugin([{ test: /mini-css-extract-plugin[\\/]dist[\\/]loader/ }])
deprecated Package is no longer maintained (last release 2020) and may not work with webpack 5+.
fix Use webpack 5's built-in persistent caching (cache: { type: 'filesystem' }) instead. For webpack 4, consider using babel-loader's cacheDirectory or other caching strategies.
breaking HardSourceWebpackPlugin may cause memory leaks or build failures with certain loaders, especially mini-css-extract-plugin.
fix Use ExcludeModulePlugin to exclude problematic loaders, or switch to webpack 5 persistent cache.
gotcha First build is not cached; only subsequent builds benefit. Cache must be manually cleared if build configuration changes unexpectedly.
fix Delete the cache directory or use the configHash and environmentHash options to ensure cache invalidation.
gotcha The package does not support ES module imports (import). Only CommonJS require() works.
fix Use const HardSourceWebpackPlugin = require('hard-source-webpack-plugin');
gotcha ParallelModulePlugin is experimental and may cause issues with webpack-dev-server or hot module replacement.
fix Avoid using ParallelModulePlugin with dev server; consider using webpack's built-in parallelism (webpack 5) or thread-loader.
npm install hard-source-webpack-plugin
yarn add hard-source-webpack-plugin
pnpm add hard-source-webpack-plugin

Shows basic webpack configuration with HardSourceWebpackPlugin for caching modules to disk.

// webpack.config.js
const HardSourceWebpackPlugin = require('hard-source-webpack-plugin');

module.exports = {
  entry: './src/index.js',
  output: { filename: 'bundle.js', path: __dirname + '/dist' },
  plugins: [
    new HardSourceWebpackPlugin({
      cacheDirectory: 'node_modules/.cache/hard-source/[confighash]',
      info: { mode: 'none', level: 'warn' },
    }),
  ],
};