Both copy-webpack-plugin
and clean-webpack-plugin
recreate existing native functionality so why not use those?
Note: clean
can be replaced with Webpack's output.clean
webpack.config.js
const FsWebpackPlugin = require('fs-webpack-plugin');
module.exports = {
plugins: [
new FsWebpackPlugin([{
// Delete folder `build` recursively
type: 'delete',
files: ['build'] // process.cwd() + build
}, {
// Delete file `build/index.test.js`
type: 'delete',
files: ['build/index.test.js'] // process.cwd() + build/index.test.js
}, {
// Delete file `build/index.test.js`,
type: 'delete',
files: ['index.test.js'],
root: path.resolve(__dirname, 'build') // Must be absolute
}, {
// Delete file `build/index.test.js` and folder `build/test`
type: 'delete',
files: [
'index.test.js',
'test'
],
root: path.resolve(__dirname, 'build')
}, {
// Copy folder `assets` recursively to `build/assets`
type: 'copy',
files: [{ from: 'assets', to: 'build' }]
}, {
// Copy file `assets/image.png` to `build/image.png`
type: 'copy',
files: [{ from: 'assets/image.png', to: 'build' }]
}])
]
}
new FsWebpackPlugin(actions, options)
actions (Action[])
- Array of action objectsoptions.verbose (Boolean)
- Enable logging (defaulttrue
)options.strict (Boolean)
- Should throw errors instead of logging them (defaultfalse
)options.dry (Boolean)
- Enable dry run (defaultfalse
). Please note thatoptions.dry
will not output to console ifoptions.verbose
isfalse
Action
type (String)
- Action type, must be one ofcopy
,delete
files (String[]|{ from: String, to: String}[]
- Files or directorys to delete.copy
only accepts{ from, to }
. If paths are relative, usesroot
root (String)
- Absolute path used byfiles
, defaults toprocess.cwd()
hooks (String[])
- Webpack hooks to run action on, defaults to['beforeRun']