https://github.com/unplugin/unplugin-macros
Macros plugin for bundlers.
https://github.com/unplugin/unplugin-macros
Last synced: 19 days ago
JSON representation
Macros plugin for bundlers.
- Host: GitHub
- URL: https://github.com/unplugin/unplugin-macros
- Owner: unplugin
- License: mit
- Created: 2023-06-01T14:59:57.000Z (over 2 years ago)
- Default Branch: main
- Last Pushed: 2025-12-29T06:10:08.000Z (about 1 month ago)
- Last Synced: 2026-01-01T02:53:13.980Z (about 1 month ago)
- Language: TypeScript
- Homepage: https://jsr.io/@unplugin/macros
- Size: 1.76 MB
- Stars: 264
- Watchers: 2
- Forks: 6
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- Funding: .github/FUNDING.yml
- License: LICENSE
Awesome Lists containing this project
- awesome-rstack - unplugin-macros
README
# unplugin-macros
[![npm version][npm-version-src]][npm-version-href]
[![npm downloads][npm-downloads-src]][npm-downloads-href]
[![JSR][jsr-src]][jsr-href]
[![Unit Test][unit-test-src]][unit-test-href]
> Macros are a mechanism for running JavaScript functions at bundle-time.
> The value returned from these functions or variables are directly inlined into your bundle.
## Installation
```bash
# npm
npm i -D unplugin-macros
# jsr
npx jsr add -D @unplugin/macros
```
Vite
```ts
// vite.config.ts
import Macros from 'unplugin-macros/vite'
export default defineConfig({
plugins: [Macros()],
})
```
Rollup
```ts
// rollup.config.js
import Macros from 'unplugin-macros/rollup'
export default {
plugins: [Macros()],
}
```
esbuild
Requires esbuild >= 0.15
```ts
// esbuild.config.js
import { build } from 'esbuild'
build({
plugins: [require('unplugin-macros/esbuild')()],
})
```
Webpack
```ts
// webpack.config.js
module.exports = {
/* ... */
plugins: [require('unplugin-macros/webpack')()],
}
```
## Usage
```js
// main.js
import { buildTime, getRandom } from './macros' with { type: 'macro' }
getRandom() // Will be replaced with a random number at build time
buildTime // Will be replaced with the timestamp at the build time
```
```js
// macros.js
export function getRandom() {
return Math.random()
}
export const buildTime = Date.now()
```
See more in [Bun Macros](https://bun.sh/blog/bun-macros).
### TypeScript
Import Attributes syntax is supported in TypeScript 5.3 and above.
### ESLint
Import Attributes syntax is supported in ESLint v9.14.0.
## Options
Refer to [docs](https://jsr.io/@unplugin/macros/doc/api/~/Options).
## Thanks
Thanks to [Bun Macros](https://bun.sh/blog/bun-macros).
## Sponsors
## License
[MIT](./LICENSE) License © 2023-PRESENT [Kevin Deng](https://github.com/sxzz)
[npm-version-src]: https://img.shields.io/npm/v/unplugin-macros.svg
[npm-version-href]: https://npmjs.com/package/unplugin-macros
[npm-downloads-src]: https://img.shields.io/npm/dm/unplugin-macros
[npm-downloads-href]: https://www.npmcharts.com/compare/unplugin-macros?interval=30
[jsr-src]: https://jsr.io/badges/@unplugin/macros
[jsr-href]: https://jsr.io/@unplugin/macros
[unit-test-src]: https://github.com/unplugin/unplugin-macros/actions/workflows/unit-test.yml/badge.svg
[unit-test-href]: https://github.com/unplugin/unplugin-macros/actions/workflows/unit-test.yml