fs-webpack-plugin

File system methods bundled in a webpack plugin package

Usage no npm install needed!

<script type="module">
  import fsWebpackPlugin from 'https://cdn.skypack.dev/fs-webpack-plugin';
</script>

README

fs-webpack-plugin

File system methods bundled in a webpack plugin package

Why?

Both copy-webpack-plugin and clean-webpack-plugin are very large packages for the functionality they provide (91.3kb and 15.3kb minzipped respectively).

Both of these packages rely on fs, so why not bundle them together whilst also minifiying the bundle size?

Usage

webpack.config.js

const FsWebpackPlugin = require('fs-webpack-plugin');

module.exports = {
  plugins: [
    new FsWebpackPlugin([{
      // Delete folder `build` recursively
      type: 'delete',
      files: 'build'
    }, {
      // Delete file `build/index.test.js`
      type: 'delete',
      files: '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' }
    }])
  ]
}

Options

new FsWebpackPlugin(actions, options)

  • actions (Action[]) - Array of action objects
  • options.verbose (Boolean) - Enable logging (default false)
  • options.strict (Boolean) - Should throw errors instead of logging them (default false)
  • options.dry (Boolean) - Enable dry run (default false). Please note that options.dry will not output to console if options.verbose is false

Action

  • type (String) - Action type, must be one of copy, delete
  • files (String|String[]|{ from: String, to: String}|{ from: String, to: String}[] - File(s) or directory(s) to delete. copy only accepts { from, to }. Paths are relative to root
  • root (String) - Absolute path used by files, defaults to process.cwd()
  • hooks (String[]) - Webpack hooks to run action on, defaults to ['beforeRun']