Giter Club home page Giter Club logo

webpack-plugin-boilerplate's Introduction

Webpack Plugin Boilerplate

A webpack plugin boilerplate built for webpack 4.

Table of Contents

Features

Why use this boilerplate? It comes with a lot of features out of the box that will let you focus on writing webpack plugins.

  • testable: built with jest installed and ready to go. Also provides code coverage.
  • compatible: write cutting-edge javascript and Babel will compile it to a version that older browsers support
  • consistent: uses webpack's eslint config and lint-staged to run eslint on any .js files you commit.
  • extensible: simply replace boilerplate defaults and you have an npm package just waiting to be written.

Usage

This example code is already written in src/index.js. To see the demo plugin in action, you shouldn't need to write any new code. This is just a walkthrough of how it was written.

Writing The Plugin

Let's say you want to create a webpack plugin called DemoPlugin that prints "Hello World" once webpack compilation is done (super useful!).

To do this, we will need to make sure our code fulfills a few important properties:

  1. It should be a class that is named the same as your plugin. In this case we will use DemoPlugin.
  2. It should have a class method apply that takes a compiler parameter. This compiler parameter is described in more details in the compiler hook documentation.
  3. Inside the apply method, we will hook into the done lifecycle hook and do our console.log.

Your plugin's main file will be the src/index.js file. It should look something like this:

export default class DemoPlugin {
  constructor(options) {
    this.options = options;
  }

  apply(compiler) {
    compiler.hooks.done.tap('DemoPlugin', () => {
      console.log('\nHello world\n');
    });
  }
}

Notice we have used compiler.hooks.done.tap to tap into the done lifecycle hook. Our callback will now be called when webpack compilation step has been completed.

Building The Plugin

Now we need to compile our plugin.

npm run build

We now have our built plugin files in the /dist directory. The main one we want to import is located in dist/cjs.js. When you publish your NPM module, this will be the file that gets imported since it's specified in our package.json file as our main file.

For local development, you'll have to directly import the cjs.js to test your local changes.

Including The Plugin In Webpack

Now, inside of your webpack configuration for your project, you can import and add your plugin to your plugins list.

const DemoPlugin = require('./path/to/DemoPlugin/dist/cjs');

module.exports = {
  mode: 'development',
  plugins: [
    new DemoPlugin({}),
  ],
};

The final step is to build your webpack project. In the output, you should see that the DemoPlugin was called after webpack finished compilation and printed "Hello World" to the console.

And that's it! You now can work on adding some useful functionality to your plugin.

Important: Don't forget to rebuild your plugin each time you make a change!

Documentation

See the webpack plugin API Docs for a full description of webpack plugin API.

Installation

  1. Clone the repository into your "DemoPlugin" directory (replace DemoPlugin with your plugin name).

    git clone https://github.com/ctaylo21/webpack-plugin-boilerplate.git DemoPlugin && cd DemoPlugin
  2. Remove the git repository, and then initialize a new one

    rm -rf .git && git init
  3. Remove README and replace with your own

    rm README.md && touch README.md
  4. Update package.json and install dependencies

    npm init && npm install

    Don't forget to update any relevant fields in the package.json file!

  5. Start coding!

Support

If you find any problems or bugs, please open a new issue.

webpack-plugin-boilerplate's People

Contributors

ctaylo21 avatar dependabot[bot] avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.