Giter Site home page Giter Site logo

webpack-variations-plugin's Introduction

webpack-variations-plugin

This plugin makes it possible to output multiple variations of a single project. In each output script, there is a unique object available as a global constant. It is similar to the webpack DefinePlugin but also copies the project files to the output directory.

Install

npm i -D @mediamonks/webpack-variations-plugin

or

yarn add -D @mediamonks/webpack-variations-plugin

Usage

To use this plugin, you first have to require it. Secondly, you have to add an instance of it to the plugin array in the webpack.config.js. You have to pass the options object to it.

Options

You have to pass the following options to the plugin.

variations: <Object> (required)

The variations of the build output. Each item in the object defines one output variation. The key of each variation defines the name (and folder name) of the variation. The value of the variation will be included in the build as a global variable called CONFIG. The variation object may also include a _defaultVariation key with a variation name as string value. This variation is then used as a default in development mode.

rename: <Array> (optional)

An array containing rename instructions as objects. Each instruction object has a test string and a rename string property. The test string is a glob pattern and should match the base name of all files which should be renamed. Two tests should not match the same files. If two tests match the same file, only the first one is used. The rename string is the new base name of the file. This filename may include the following placeholders:

  • %variationName% - The name of the variation, as specified in the variations object.
  • %name% - The original name of the file without a path and without the file extension (see the example here).
  • %base% - The original filename of the file including the extension (see the example here).
  • %extension% - The original file extension of the file. The placeholders are replaced with the corresponding values. Invalid placeholders are left in the filename.

Current limitation: Renaming only works in the root directory. Renaming the webpack output is also not possible.

ignore: <Array> (optional)

An array containing GLOB strings which are ignored during the copy process.

constantName: <String> (optional)

The name of the global constant in which the object will be saved. This defaults to ‘CONFIG’.

Example

webpack.config.js

const WebpackVariations = require('@mediamonks/webpack-variations-plugin');

module.exports = {
  // ...
  plugins: [
    new WebpackVariations({
      variations: {
        tokio: {
          country: 'Japan',
        },
        berlin: {
          country: 'Germany',
        },
        rio: {
          country: 'Brazil',
        },
        _defaultVariation: 'tokio',
      },
      rename: [
        {
          test: 'town-info.html',
          rename: '%variationName%-info.html',
        },
      ],
      ignore: ['glob/**/index.js', 'node_modules', '.git'],
      constantName: 'CONFIG',
    }),
  ],
};

In this example, there will be three folders in the build directory called tokio, berlin and rio. In each folder, there will be the project files from the root project directory without the node_modules and the .git folder. Also, all index.js files in all subfolders of ‘glob’ won’t be copied into the variation build folders. Each variation build folder will also include a webpack output file (e.g. main.bundle.js). Each of the webpack output files will include the unique object specified in the config. Apart from this, the town-info.html file in the project folder will be renamed according to the varation name (e.g. in the berlin folder to 'berlin-info.html').

The final directory structure will look something like this:

├── build
│   ├── tokio
│   │   ├── build
│   │   │   └── main.bundle.js
│   │   ├── tokio-info.html
│   │   └── someOtherFile.txt
│   ├── berlin
│   │   ├── build
│   │   │   └── main.bundle.js
│   │   ├── berlin-info.html
│   │   └── someOtherFile.txt
│   ├── rio
│   │   ├── build
│   │   │   └── main.bundle.js
│   │   ├── rio-info.html
│   │   └── someOtherFile.txt
├── node_modules
├── .git
├── src
│   ├── script.js
│   └── someModule.js
├── town-info.html
└── someOtherFile.txt

The build/tokio/build/main.bundle.js file will look something like this:

const CONFIG = {"country":"Japan"};
// ...webpack output

webpack-variations-plugin's People

Contributors

timstruthoff avatar

Stargazers

Averyanov Oleg avatar Dave Lenz avatar

Watchers

Mient-jan Stelling avatar James Cloos avatar Utopia avatar Arjan van Wijk avatar Robert avatar  avatar Elmar avatar Robert Slootjes 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.