Skip to content

NotWoods/rollup-plugin-consts

Repository files navigation

rollup-plugin-consts

Import build time constants with Rollup.

Installation

npm install --save-dev rollup-plugin-consts

Usage

rollup-plugin-consts let you use constants that are replaced at build time, such as inlining your NODE_ENV. Unlike similar plugins such as rollup-plugin-replace, rollup-plugin-consts doesn't magically replace strings in your script. Instead, you import them like a module.

// script.js
import environment from 'consts:environment';

if (environment === 'production') {
    // Production only code ...
} else {
    // Development only code ...
}

All consts modules have the prefix consts: followed by the name of the constant, such as environment or testing. Rollup can reduce simple if statements like the one above.

// script.min.js

// environment == 'production'
{
    // Production only code ...
}

Generally, you need to ensure that rollup-plugin-consts goes before other things (like rollup-plugin-commonjs) in your plugins array, so that those plugins can apply any optimisations such as dead code removal.

// rollup.config.js
import consts from 'rollup-plugin-consts';

export default {
    // ...
    plugins: [
        consts({
            environment: 'production',
        }),
    ],
};

Options

{
    // All options are treated as `string: replacement` replacers...
    testing: false,
    version: '1.0.0',
    environment: 'development',
    // Objects can be used as replacements too!
    config: { names: ['foo', 'bar'] },
}

TypeScript

The consts function is has a typings file. See Usage with TypeScript to check how to create additional typings files for importing constants.

Credits

rollup-plugin-consts was originally created by Jake Archibald for PROXX. You can watch his presentation with Surma about Rollup plugins they wrote for PROXX.

License

Apache-2.0

About

Import build time constants with Rollup

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 3

  •  
  •  
  •