Copy assets in webpack memory
npm install copy-asset-in-memory-webpack-plugin




> Copy assets in webpack memory
``bash`
npm i -D copy-asset-in-memory-webpack-pluginor
yarn add --dev copy-asset-in-memory-webpack-plugin
`js
// webpack.config.js
const CopyAssetInMemoryPlugin = require("copy-asset-in-memory-webpack-plugin");
module.exports = {
plugins: [
new CopyAssetInMemoryPlugin({
test: /.js$/,
to: "js",
}),
],
};
`
#### test
Include all assets that pass test assertion
Type: String|RegExp|Array. Default: undefined
#### include
Include all assets matching any of these conditions
Type: String|RegExp|Array. Default: undefined
#### exclude
Exclude all assets matching any of these conditions
Type: String|RegExp|Array. Default: undefiend
#### stage
Refer https://webpack.js.org/api/compilation-hooks/#processassets for more info.
`js
// webpack.config.js
const { Compilation } = require("webpack");
const CopyAssetInMemoryPlugin = require("copy-asset-in-memory-webpack-plugin");
module.exports = {
plugins: [
new CopyAssetInMemoryPlugin({
test: /.js$/,
stage: Compilation.PROCESS_ASSETS_STAGE_SUMMARIZE, // Default
to: (fileName) => ${js}/filename,`
}),
],
};
#### to
Asset destination, allows to modify the file path.
Type: Function | string. Default: undefined
`js
// webpack.config.js
const CopyAssetInMemoryPlugin = require("copy-asset-in-memory-webpack-plugin");
module.exports = {
plugins: [
new CopyAssetInMemoryPlugin({
test: /.js$/,
to: (fileName) => ${js}/filename, // copies all files into js folderassets
}),
new CopyAssetInMemoryPlugin({
test: /.svg$/,
to: "assets", // copies all files into folder`
}),
],
};
#### transform
Allows to modify the file contents.
Type: Function. Default: undefined
`js
// webpack.config.js
const CopyAssetInMemoryPlugin = require("copy-asset-in-memory-webpack-plugin");
module.exports = {
plugins:
new CopyAssetInMemoryPlugin({
test: /.js$/,
to: (fileName) => ${js}/filename,content
// The argument is a [Buffer object
// it could be converted to a String to be processed using content.toString()`
transform: (content) => "newContent",
}),
],
};
#### deleteOriginalAssets
Whether to delete the original assets or not.
Type: boolean. Default: false
- assets cannot be copied outside output directory
- deleting an asset via deleteOriginalAssets` will also delete its sourcemap