copy-asset-in-memory-webpack-plugin

raw JSON →
4.0.1 verified Sat Apr 25 auth: no javascript

Webpack 5 plugin to copy processed assets while the compilation is still in memory, before they are written to disk. Version 4.0.1 is the latest stable, with a history of breaking changes aligning with webpack 5. It allows filtering by test/include/exclude, renaming via `to`, transforming content, and optionally deleting original assets. Unlike copy-webpack-plugin which copies from filesystem to output, this plugin copies assets already in webpack's compilation graph. Requires webpack ^5.20.0 and Node >=12.13. Ships TypeScript types.

error Cannot find module 'copy-asset-in-memory-webpack-plugin'
cause Package not installed or webpack config not resolving node_modules correctly.
fix
Run npm install -D copy-asset-in-memory-webpack-plugin and ensure your webpack config is in a directory where node_modules can be resolved.
error TypeError: CopyAssetInMemoryPlugin is not a constructor
cause Incorrect import style; likely using ESM default import in a CJS context, or named import where default was expected.
fix
Use const CopyAssetInMemoryPlugin = require('copy-asset-in-memory-webpack-plugin'); in CommonJS, or import CopyAssetInMemoryPlugin from 'copy-asset-in-memory-webpack-plugin'; in ESM (with appropriate configuration).
error Error: Hook is not defined for compilation stage
cause Invalid `stage` value; webpack removed or renamed the stage constant.
fix
Use Compilation.PROCESS_ASSETS_STAGE_SUMMARIZE (default) or another valid stage from webpack's Compilation object.
error Webpack 5 requires node >=10.13.0 but copy-asset-in-memory-webpack-plugin requires >=12.13
cause Incompatible Node.js version installed (too low).
fix
Upgrade Node.js to version >=12.13.
breaking v4.0.0 dropped support for webpack 4. Upgrade to webpack 5 to use this version.
fix Upgrade webpack to ^5.20.0. If stuck on webpack 4, use v3.x.
gotcha Assets cannot be copied outside the output directory. Attempting to do so will silently fail or cause undefined behavior.
fix Ensure the `to` option produces paths relative to the output directory. Do not use absolute paths or '..'.
gotcha deleteOriginalAssets: true deletes the asset's source map as well, because source maps are separate assets that share the same base filename.
fix If you need source maps, either disable deleteOriginalAssets or manually copy the source map before deletion.
deprecated The `stage` option default is Compilation.PROCESS_ASSETS_STAGE_SUMMARIZE. In future webpack versions, this stage might be removed or behave differently.
fix Explicitly set stage to Compilation.PROCESS_ASSETS_STAGE_ADDITIONAL for forward compatibility.
npm install copy-asset-in-memory-webpack-plugin
yarn add copy-asset-in-memory-webpack-plugin
pnpm add copy-asset-in-memory-webpack-plugin

Shows CommonJS usage in webpack.config.js, copying .js assets to a 'scripts' subfolder in memory.

// webpack.config.js
const CopyAssetInMemoryPlugin = require('copy-asset-in-memory-webpack-plugin');

module.exports = {
  entry: './src/index.js',
  output: {
    path: path.resolve(__dirname, 'dist'),
    filename: 'bundle.js',
  },
  plugins: [
    new CopyAssetInMemoryPlugin({
      test: /\.js$/,
      to: (fileName) => `scripts/${fileName}`,
    }),
  ],
};