https://github.com/swordjs/esbuild-plugin-condition-comment-macro
This is an esbuild plugin, which can be given a condition that will filter out unqualified macro comment blocks from the code. This is usually used for conditional builds
https://github.com/swordjs/esbuild-plugin-condition-comment-macro
Last synced: about 1 year ago
JSON representation
This is an esbuild plugin, which can be given a condition that will filter out unqualified macro comment blocks from the code. This is usually used for conditional builds
- Host: GitHub
- URL: https://github.com/swordjs/esbuild-plugin-condition-comment-macro
- Owner: swordjs
- Created: 2023-02-09T14:36:35.000Z (over 3 years ago)
- Default Branch: main
- Last Pushed: 2023-02-11T13:14:24.000Z (over 3 years ago)
- Last Synced: 2025-04-22T10:53:25.856Z (about 1 year ago)
- Language: TypeScript
- Size: 50.8 KB
- Stars: 6
- Watchers: 1
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# esbuild-plugin-condition-comment-macro
This is an esbuild plugin, which can be given a condition that will filter out unqualified macro comment blocks from the code. This is usually used for conditional builds
## Installation
```bash
npm install @swordjs/esbuild-plugin-condition-comment-macro -D
```
Or use yarn / pnpm
## Usage
```js
import * as esbuild from 'esbuild'
import { ConditionCommentMacroPlugin } from "@swordjs/esbuild-plugin-condition-comment-macro"
const condition = "aliyun"
await esbuild.build({
entryPoints: ['src/index.ts'],
outfile: 'dist/out.js',
plugins: [ConditionCommentMacroPlugin({
prefix: "@",
startMacro: {
ifdef: ({ match, args, reg }) => args.includes(condition) ? match : match.replace(reg, ''),
ifndef: ({ match, args, reg }) => args.includes(condition) ? match.replace(reg, '') : match,
},
endMacro: "endif"
})()]
})
```
Just like `build.js` in the `test/fixture` folder, when using esbuild, just pass some necessary parameters to the plugin
In the above code, we simply implement a conditional compilation function, we set the condition to `aliyun`, and then write the following code in the code
```js
// @ifdef server || woker || aliyun
const test = 1
// @endif
// @ifdef aliyun
const a = 1
// @endif
// @ifndef aliyun || server
const b = 1
// @endif
```
Then esbuild will output this code content
```js
"use strict";
const test = 1;
const a = 1;
```
## Options
```ts
interface Options {
// Macro prefix, we recommend using @
prefix: string,
// A custom action on the start macro, which returns the current matching fragment, along with the macro parameters and the regular being matched
startMacro: Record string>,
// End macro
endMacro: string,
}
```