# esbuild-plugin-conditional-build

> A plugin for esbuild that allows for conditional inclusion of javascript based on build time constants.

Latest version **1.0.2** (published 2021-12-07) · MIT license · 0 weekly downloads

## Install

```sh
npm install esbuild-plugin-conditional-build
pnpm add esbuild-plugin-conditional-build
yarn add esbuild-plugin-conditional-build
bun add esbuild-plugin-conditional-build
```

## 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.2 |
| Published | 2021-12-07 |
| First published | 2021-11-27 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | ESM + CommonJS |
| Dependencies | 0 |
| Unpacked size | 7.9 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 2 |
| Author | AndrewPerson |
| Maintainers | andrewperson |

## Links

- npm: https://www.npmjs.com/package/esbuild-plugin-conditional-build
- Repository: https://github.com/AndrewPerson/Conditional-Build
- Issues: https://github.com/AndrewPerson/Conditional-Build/issues
- npm.io page: https://npm.io/package/esbuild-plugin-conditional-build

## Recent versions

- 1.0.2 (latest) — 2021-12-07
- 1.0.1 — 2021-11-27
- 1.0.0 — 2021-11-27

## README

# Conditional Build

## Description

This plugin (for esbuild) allows you conditionally exclude or include parts of javascript (or typescript) files at build time. You use `//#if`, `//#else`, and `//#endif` to do this.

## Setup

To install through NPM run this command:
```
npm i esbuild-plugin-conditional-build
```

You will need to set the `type` of your package to `"module"` in `package.json`.
Any files you use this plugin in will also need to have the extension `.mjs` instead of `.js`.

```json
{
    "name": "...",
    "version": "1.0.0",
    "description": "...",
    "type": "module",
    "main": "index.mjs",
}
```

## Usage

```js
//build.js

//Import the conditional build
import conditionalBuild from "esbuild-plugin-conditional-build";

//Setup esbuild
import { build } from "esbuild";

build({
    entryPoints: ["src/foo.js"],
    outdir: "dist",
    plugins: [
        //Define the DEVELOPMENT constant
        conditionalBuild(["DEVELOPMENT"])
    ]
})
```

```js
//src/foo.js
//#conditional

//#if DEVELOPMENT
console.log("Development!");
//#else
console.log("Production.");
//#endif
```

**Note that you must have `//#conditional` at the very start of your file.**

After running `node build.js` `dist/foo.js` will look like this:
```js
console.log("Development!");
```

The comments and the production code have been stripped.
Conversely, if we ran `build.js` without `"DEVELOPMENT"` being passed into `conditionalBuild`, the development code would be stripped and the production code would remain.

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