Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/NotWoods/rollup-plugin-consts
Import build time constants with Rollup
https://github.com/NotWoods/rollup-plugin-consts
constants proxx rollup rollup-plugin
Last synced: 25 days ago
JSON representation
Import build time constants with Rollup
- Host: GitHub
- URL: https://github.com/NotWoods/rollup-plugin-consts
- Owner: NotWoods
- License: apache-2.0
- Created: 2019-08-14T12:54:25.000Z (over 5 years ago)
- Default Branch: main
- Last Pushed: 2024-01-27T06:02:57.000Z (11 months ago)
- Last Synced: 2024-04-27T21:43:15.831Z (8 months ago)
- Topics: constants, proxx, rollup, rollup-plugin
- Language: JavaScript
- Size: 376 KB
- Stars: 10
- Watchers: 2
- Forks: 3
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
- awesome - consts - Import constants at build time. (Plugins / Modules)
README
# rollup-plugin-consts
[![](https://img.shields.io/npm/v/rollup-plugin-consts.svg?style=flat)](https://www.npmjs.com/package/rollup-plugin-consts)
Import build time constants with Rollup.
## Installation
```bash
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](https://github.com/rollup/rollup-plugin-replace),
rollup-plugin-consts doesn't magically replace strings in your script. Instead,
you import them like a module.```js
// 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.```js
// 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.```js
// rollup.config.js
import consts from 'rollup-plugin-consts';export default {
// ...
plugins: [
consts({
environment: 'production',
}),
],
};
```## Options
```js
{
// 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](https://github.com/NotWoods/rollup-plugin-consts/wiki/Usage-with-TypeScript)
to check how to create additional typings files for importing constants.## Credits
rollup-plugin-consts was originally created by
[Jake Archibald](https://github.com/jakearchibald/) for
[PROXX](https://github.com/GoogleChromeLabs/proxx). You can watch his
presentation with [Surma](https://github.com/surma/)
[about Rollup plugins they wrote for PROXX](https://youtu.be/TsTt7Tja30Q).## License
Apache-2.0