3.1.2 • Published 5 years ago

rollup-plugin-conditional v3.1.2

Weekly downloads
197
License
MIT
Repository
github
Last release
5 years ago

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 VersionPlugin Version
0.57+3.x (Recommended)
0.62 - 0.68.22.x
< 621.x

Versioning

This project uses semantic versioning

3.1.2

5 years ago

3.1.1

5 years ago

3.1.0

5 years ago

3.0.0

5 years ago

2.2.0

5 years ago

2.1.1

6 years ago

2.1.0

6 years ago

2.0.0

6 years ago

2.0.0-beta.3

6 years ago

2.0.0-beta.2

6 years ago

2.0.0-beta.1

6 years ago

2.0.0-beta.0

6 years ago

1.1.1

7 years ago

1.1.0

7 years ago

1.0.1

7 years ago

1.0.0

7 years ago

0.1.4

8 years ago

0.1.3

8 years ago

0.1.2

8 years ago

0.1.1

8 years ago

0.1.0

8 years ago