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

@mediamonks/webpack-variations

v0.3.0

Published

Webpack plugin for creating multiple variations of a build.

Downloads

9

Readme

webpack-variations

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

or

yarn add -D @mediamonks/webpack-variations

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');

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