# rollup-plugin-codefilter

> A plugin for rollup/vite to filter code by dotenv variables

Latest version **1.0.0-beta** (published 2021-05-23) · MIT license · 0 weekly downloads

## Install

```sh
npm install rollup-plugin-codefilter
pnpm add rollup-plugin-codefilter
yarn add rollup-plugin-codefilter
bun add rollup-plugin-codefilter
```

## Health

**Score 20/100 (F)** — status: abandoned.

Positive: esm support; no vulnerabilities.

Warnings: low downloads; no types.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.0-beta |
| Published | 2021-05-23 |
| First published | 2021-05-23 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | ESM + CommonJS |
| Dependencies | 4 |
| Unpacked size | 11.7 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Maintainers | cary-mao |
| Keywords | rollup-plugin, vite-plugin |

## Links

- npm: https://www.npmjs.com/package/rollup-plugin-codefilter
- Repository: https://github.com/cary-mao/rollup-plugin-codefilter
- Homepage: https://github.com/cary-mao/rollup-plugin-codefilter#readme
- Issues: https://github.com/cary-mao/rollup-plugin-codefilter/issues
- npm.io page: https://npm.io/package/rollup-plugin-codefilter

## Dependencies (4)

- [chalk](https://npm.io/package/chalk.md) ^4.1.1
- [dotenv](https://npm.io/package/dotenv.md) ^10.0.0
- [parse-json](https://npm.io/package/parse-json.md) ^5.2.0
- [@rollup/pluginutils](https://npm.io/package/@rollup/pluginutils.md) ^4.1.0

## Alternatives

- [raw-loader](https://npm.io/package/raw-loader.md) — 4.3M weekly downloads
- [plop](https://npm.io/package/plop.md) — 1.4M weekly downloads
- [webpack-deadcode-plugin](https://npm.io/package/webpack-deadcode-plugin.md) — 80.3K weekly downloads
- [@storybook/preact-vite](https://npm.io/package/@storybook/preact-vite.md) — 54.2K weekly downloads
- [vite-plugin-transform](https://npm.io/package/vite-plugin-transform.md) — 2.4K weekly downloads

## Recent versions

- 1.0.0-beta (latest) — 2021-05-23

## README

# rollup-plugin-codefilter
This is a plugin for rollup/vite that filter code by dotenv variables.

# install
```bash
npm install rollup-plugin-codefilter --save-dev
```

# quick start
create a `rollup.config.js` [configuration file](https://www.rollupjs.org/guide/en/#configuration-files) and import the plugin:
```js
import codeFilter from 'rollup-plugin-codefilter'

module.exports = {
  input: 'index.js',
  output: {
    file: 'bundle.js',
    format: 'iife',
    name: 'test'
  },
  plugins: [
    codeFilter()
  ]
}
```
create a `.env` [configuration file](https://github.com/motdotla/dotenv#usage) and define some true or false variables.
```bash
keep = true
discard = false
```
create `index.js`:
```js
function log () {
  // ifdef keep
  console.log('hello world!')
  // endif

  // ifdef discard
  console.log('it will be discarded.')
  // endif
}

export default log
```
Finally, run `rollup -c rollup.config.js`.  
The file will be generated in folder `dist`:
```js
var test = (function () {
  'use strict';

  function log () {
    console.log('hello world!');
  }

  return log;

}());
```

# options
### `ext`

Type: `Array<string>`
Default: `[js, json]`

Only handle the module which match the provided extension. Currently only support `js` and `json`.

### `dotenv`

Refer to dotenv's [options](https://www.npmjs.com/package/dotenv#options)

### `exclude`

Type: `String` | `Array<String>`<br>
Default: `null`

A [minimatch pattern](https://github.com/isaacs/minimatch), or array of patterns, which specifies the files in the build the plugin should _ignore_. By default no files are ignored.

### `include`

Type: `String` | `Array<String>`<br>
Default: `null`

A [minimatch pattern](https://github.com/isaacs/minimatch), or array of patterns, which specifies the files in the build the plugin should operate on. By default all files are targeted.

---
The options below are the same as [@rollup/plugin-json](https://github.com/rollup/plugins/tree/master/packages/json)'s.

> To be careful, you can't use `@rollup/plugin-json` when this plugin injected.

### `indent`

Type: `String`<br>
Default: `'\t'`

Specifies the indentation for the generated default export.

### `namedExports`

Type: `Boolean`<br>
Default: `true`

If `true`, instructs the plugin to generate a named export for every property of the JSON object.

### `preferConst`

Type: `Boolean`<br>
Default: `false`

If `true`, instructs the plugin to declare properties as variables, using either `var` or `const`. This pertains to tree-shaking.

---
_Source: https://npm.io/package/rollup-plugin-codefilter · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
