npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@rogalski/webpack-file-manager

v1.0.1

Published

A file-manager plugin for Webpack. It let's you define create/copy/move/delete operations on files and folders that will be executed before or after webpack's compiler.

Downloads

60

Readme

Webpack-File-Manager

Simple plugin to manage files before and after webpack compilation.

Installation

Use npm like allways:

npm install @rogalski/webpack-file-manager --save-dev

Usage

First import plugin:

const { FileManagerPlugin } = require("@rogalski/webpack-file-manager")

Then add it to your configuration:

const config = {
	//...
	plugins: [
		new WebpackFileManager(options)
			.copy(srouce, target)
			.delete(target)
			.move(source, target)
			.copy(source, target, options)
			.move(source, target, /\.html$/)
			.delete(target, /\.js$/, options)
	]
	//...
}

Configuration

First, when creating new instance of a plugin, you can pass optional 'options' object to overwrite default setting:

{
  // create not existing target directories while movig/copying?
  createDir?: boolean = true,
  // overwrite existing target files while creating/moving/copying?
  overwrite?: boolean = true,
  // increment content of existing target files while creating/moving/copying?
  increment?: boolean = false,
  // run action after compilation?
  execute?: 'BEFORE' | 'AFTER' = 'BEFORE',
  // more logs in console!
  verbose?: boolean = false
  // no logs in the console!
  silent?: boolean = false
  // colorize logs?
  colors?: boolean = true
  // stop on error?
  throwError?: boolean = false
}

Actions

Use create/move/copy/delete functions to define and chain actions. Each action can have target, actions like 'move' or 'copy' also have source object. Both source and target are strings, either absolute or relative to webpack's context. You can pass 'options' object while defining action to define custom configuration for your action. Configuration object used for defining action is subset one used while creating new instance and is optional. Copy/Move/Delete actions that points to directories can have specified regular expression that will change their behaviour. Those actions will look for files that match passed reg-exp instead of working on whole directories.

Create

Let's you create file or directory.

const config = {
	// .create( target: string )
	// .create( target: string, options )
	// .create( target: string, content: string )
	// .create( target: string, content: string, options )
	// .create( target: string, content: string, options )

	//...
	plugins: [
		new WebpackFileManager(options)
			// create a directory
			.create("C:/test")
			// create a file
			.create("C:/test/test1.txt", "Hello Webpack!")
			// create a file and set custom config
			.create("C:/test/test2.txt", "Hello Webpack!", { execute: "AFTER" })
	]
	//...
}

While creating new element plugin will check if content is passed. If so, it will create a file, otherwise it will create a directory. To create empty file simply put empty string ("") as content.

Move / Copy

Let's you move/copy file or directory. Below examplex are same for Move and Copy actions:

const config = {

  // .move( source: string, target: string )
  // .move( source: string, target: string, options )
  // .move( source: string, target: string, regex )
  // .move( source: string, target: string, regex, options )

  // .copy( source: string, target: string )
  // .copy( source: string, target: string, options )
  // .copy( source: string, target: string, regex )
  // .copy( source: string, target: string, regex, options )

  // ...
  plugins: [
    new WebpackFileManager(options)
      // move a directory - content of source will be moved/copied to target
      .move("C:/test", "D:/test")
      // move a file - source file will be moved/copied to target directory
      .move("C:/test/test.txt", "D:/test")
      // move a file - source file will be moved/copied to target directory and renamed
      .move("C:/test/test1.txt", "D:/test/test2.txt")
      .move("C:/test/test3.txt", "D:/test", {execute: 'AFTER'})
      .move("C:/test", "D:/test", /\.(css|html)$/)
      .move("C:/test", "D:/test", /\.(css|html)$/, {
        overwrite: false
        execute: 'AFTER'
      })
  ],
  //...
};

There are few scenarios while moving or copying data:

  • move/copy directory - content of source directory will be moved/copied to another directory, if regex is defined, it will be used match file names.
  • move/copy file to directory - file will be moved/copied to target directory
  • move/copy file to file - file will be moved/copied to target file

Regex will be used only while dealing with directories.

Delete

Let's you delete file or directory.

const config = {
	// .delete( target: string )
	// .delete( target: string, options )
	// .delete( target: string, regex )
	// .delete( target: string, regex, options )

	//...
	plugins: [
		new WebpackFileManager(options)
			// delete a directory
			.delete("C:/test")
			// delete a file
			.delete("C:/test/test1.txt")
			// delete a file and set custom config
			.delete("C:/test/test2.txt", { execute: "AFTER" })
			// delete files matching reg-exp inside target directory
			.create("C:/test", /\.js$/)
			// delete files matching reg-exp inside target directory and set options
			.create("C:/test", /\.js$/, { execute: "AFTER" })
	]
	//...
}

While deleting files regex will not take effect. While deleting a directory you can specify regex. This will keep directory but remove content matching regex. If regex is not specified whole directory will be removed.

Examples

  1. First remove dst/public directory, then create empty dst/public directory:
const config = {
	//...
	plugins: [
		new WebpackFileManager(options)
			.delete("./dst/public")
			.create("./dst/public")
	]
}
  1. Copy content of src/public directory to dst/public directory:
const config = {
	//...
	plugins: [
		new WebpackFileManager(options).copy("./src/public", "./dst/public")
	]
}
  1. Copy content of src/public/scripts/test.js file to dst/public/scripts/test.js file by incrementing if file target file exists:
const config = {
	//...
	plugins: [
		new WebpackFileManager(options).copy(
			"./src/public/scripts/test.js",
			"dst/public/scripts/test.js",
			{
				increment: true // if set to true, content will be added to existing files
			}
		)
	]
}
  1. Delete all .js files in public directory:
const config = {
	//...
	plugins: [new WebpackFileManager(options).delete("./public", /\.js$/)]
}
  1. Delete temp directory:
const config = {
	//...
	plugins: [new WebpackFileManager(options).delete("./temp")]
}

Contributing

Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.

Please make sure to update tests as appropriate.

License

MIT