Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

rollup-plugin-conditional

AgronKabashi4.8kMIT3.1.2

Conditionally execute rollup plugins

rollup, rollup-plugin, es2015

readme

rollup-plugin-conditional

A proxy plugin for conditionally executing rollup plugins.
NOTE: This plugin has entered maintenance only mode, meaning that only bugs will be fixed. See But do I really need it section to accomplish the same thing without a plugin.

Why

There are times when you only want to run a plugin if certain conditions are met. This plugin aims to simplify that setup.

But do I really need it?

Not really, in relatively newer versions on rollup you can accomplish the same thing using a simple spread mechanic:

export default {
  ...
  plugins: [
    ...isProduction ? [
      licence(),
      strip(),
      uglify(),
      gzip()
    ] : []
  ]
};

In the end, this syntax is better because:

  • It reduces the cost of overhead and increases performance slightly
  • It reduces your dependencies by one

Installation

npm install rollup-plugin-conditional --save-dev

Usage

import conditional from "rollup-plugin-conditional";
// import other plugins

const isProduction = process.env.buildTarget === "PROD";

export default {
  ...
  plugins: [
    ...
    conditional(isProduction, [
      licence(),
      strip(),
      uglify(),
      gzip()
    ]),

    conditional(!isProduction, [
      filesize()
    ])
  ]
})

It's also possible to nest conditionals but the recommendation is to keep the plugins as flat as possible:

export default {
  ...
  plugins: [
    conditional(!isProduction, [
      conditional(isLocalBuild, [
        eslint()
      ]),
      watch()
    ])
  ]
};

Special cases

Unfortunately some plugins, like rollup-plugin-serve, always assume that they will be executed so they perform some premature tasks before any of the life cycle hooks are called:

import serve from "rollup-plugin-serve";

export default {
  ...
  plugins: [
    conditional(false, [ // false here will prevent any of the plugins' life cycle hooks from being executed
      // rollup-plugin-serve will however instantiate a http(s)-server immediately and outside any of the life cycle hooks
      // resulting in the http-server running even though we don't want it to.
      serve()
    ])
  ]
};

In order to work around this we need to defer the initialisation by simply providing a callback method that returns the plugins:

import serve from "rollup-plugin-serve";

export default {
  ...
  plugins: [
    conditional(false, () => [ // Notice the arrow function.
      serve()
    ])
  ]
};

Backwards Compatibility Table

Rollup Version Plugin Version
0.57+ 3.x (Recommended)
0.62 - 0.68.2 2.x
< 62 1.x

Versioning

This project uses semantic versioning

changelog

Changelog

Version 3.1.2

2019-01-23

  • Updated readme. Plugin is now in maintenance mode, only bugs will be addressed.

Version 3.1.1

2019-01-14

  • Decreased rollup peer-dependency version.
  • Added backwards compatbility map to readme.
  • Updated rollup dependency to v1.1

Version 3.1.0

2019-01-14

  • Rewrote internals in order remove overhead and better conform to future rollup API changes.
  • Increased performance and reduced plugin size.

Version 3.0.0

2019-01-01

  • Compatibility with rollup v1.0. Contains several under the hood changes such as removal of deprecated options and new API hooks.
  • Tests reworked to compare results against rollup itself. Should detect breaking changes much easier.

Version 2.2.0

2018-11-20

  • Added support for new hooks, renderStart and renderError.

Version 2.1.1

2018-08-13

  • Patch: API hook transformBundle wasn't removed as suggested by the docs and is still supported, though deprecated. This patch adds backwards compatibility until rollup v1.0 is released. Changes will be reverted in #4.

Version 2.1.0

2018-07-26

  • Added support for deferred initialization of rollup plugins. Prevents plugins from causing side effects.
    • Special thanks to chambo-e for making it possible.

Version 2.0.0

2018-07-16

  • Conforms to latest rollup API (0.62). Make sure you use this version or higher.
  • Properly chains transform and transformChunk. This was broken in 1.1.1 since it only processed the first occurrence.

Version 1.1.1

2017-02-01

  • Minor publication script fix

Version 1.1.0

2017-02-01

  • Simplified the syntax for initializing conditional. The older syntax is now deprecated.
// Before
plugins: [
  conditional({
    condition: process.env.buildTarget === "PROD",
    plugins: [
      plugin1(),
      plugin2(),
      ...
    ]
  })
]

// After
plugins: [
  conditional(process.env.buildTarget === "PROD", [
    plugin1(),
    plugin2(),
    ...
  ])
]

Version 1.0.1

2017-01-31

  • Minor hotpatch that updates the build files

Version 1.0.0

2017-01-31

  • plugin property is replaced with plugins to simplify the setup where you need to run many plugins based on the same condition ` // Before plugins: [ conditional({
    condition: process.env.buildTarget === "PROD",
    plugin: uglify()
    }), conditional({
    condition: process.env.buildTarget === "PROD",
    plugin: filesize()
    }) ]

// After plugins: [ conditional({ condition: process.env.buildTarget === "PROD", plugins: [ uglify(), filesize() ] }) ] `

Version 0.1.3

2016-09-25

  • Unit tests added

Version 0.1.2

2016-09-22

  • Removed mutation

Version 0.1.1

2016-09-22

  • Initial public release