# @wahyubucil/vite-plugin-compile-time

> Do some compile time work in your Vite project

Latest version **0.2.9** (published 2023-06-09) · MIT license · 0 weekly downloads

## Install

```sh
npm install @wahyubucil/vite-plugin-compile-time
pnpm add @wahyubucil/vite-plugin-compile-time
yarn add @wahyubucil/vite-plugin-compile-time
bun add @wahyubucil/vite-plugin-compile-time
```

## Health

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

Positive: has types; esm support; no vulnerabilities; high quality score.

Warnings: low downloads; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.2.9 |
| Published | 2023-06-09 |
| First published | 2023-06-05 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM |
| Node | >=16 |
| Dependencies | 4 |
| Unpacked size | 8.1 KB |
| Known vulnerabilities | 0 (+6 in 2 direct dependencies) |
| Install scripts | no |
| Maintainers | wahyubucil |

## Links

- npm: https://www.npmjs.com/package/@wahyubucil/vite-plugin-compile-time
- npm.io page: https://npm.io/package/@wahyubucil/vite-plugin-compile-time

## Dependencies (4)

- [devalue](https://npm.io/package/devalue.md) ^4.3.2
- [esbuild](https://npm.io/package/esbuild.md) ^0.17.19
- [magic-string](https://npm.io/package/magic-string.md) ^0.30.0
- [bundle-require](https://npm.io/package/bundle-require.md) ^4.0.1

## Recent versions

- 0.2.9 (latest) — 2023-06-09
- 0.2.8 — 2023-06-06
- 0.2.7 — 2023-06-06
- 0.2.6 — 2023-06-06
- 0.2.5 — 2023-06-05
- 0.2.4 — 2023-06-05

## README

**💛 You can help the author become a full-time open-source maintainer by [sponsoring him on GitHub](https://github.com/sponsors/egoist).**

---

# vite-plugin-compile-time

[![npm version](https://badgen.net/npm/v/@wahyubucil/vite-plugin-compile-time?v=2)](https://npm.im/@wahyubucil/vite-plugin-compile-time) [![npm downloads](https://badgen.net/npm/dm/@wahyubucil/vite-plugin-compile-time?v=2)](https://npm.im/@wahyubucil/vite-plugin-compile-time)

Use this plugin to generate code at compile time or get data at compile time in your Vite projects.

This is a forked version with updated dependencies. If the [original package](https://github.com/egoist/@wahyubucil/vite-plugin-compile-time) is updated, use it!

This package is ESM-only. Read more on how to use [Pure ESM Package](https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c).

## Install

```bash
npm i @wahyubucil/vite-plugin-compile-time -D
```

```ts
import { defineConfig } from "vite"
import compileTime from "@wahyubucil/vite-plugin-compile-time"

export default defineConfig({
  plugins: [compileTime()],
})
```

## Usage

Compile-time data:

```ts
// get-data.ts
import fs from "fs"

export default async () => {
  const post = await fs.promises.readFile("./post.md", "utf8")
  return {
    data: { post },
  }
}

// get the data at compile time
const data = import.meta.compileTime("./get-data.ts")
assert.deepEqual(data, { post: "....." })
```

Compile-time code:

```ts
// generate-code.ts
export default async () => {
  return {
    code: `count++`,
  }
}

// insert the generated code at compile time
let count = 0
import.meta.compileTime("./generate-code.ts")
assert.equal(count, 1)
```

## API

Use `import.meta.compileTime` to get compile-time data or code.

```ts
declare interface ImportMeta {
  compileTime: <T>(file: string) => T
}
```

You should return a default export with object containing `code` or `data` property:

```ts
import {
  CompileTimeFunctionArgs,
  CompileTimeFunctionResult,
} from "@wahyubucil/vite-plugin-compile-time"

export default async (
  args: CompileTimeFunctionArgs,
): CompileTimeFunctionResult => {
  return {
    data: {
      hello: "world",
    },
    // Trigger rebuild when watched files change
    watchFiles: ["/absolute/path"],
  }
}
```

See the type docs on [paka.dev](https://paka.dev/npm/@wahyubucil/vite-plugin-compile-time#module-index-export-CompileTimeFunction).

## Sponsors

[![sponsors](https://sponsors-images.egoist.sh/sponsors.svg)](https://github.com/sponsors/egoist)

## License

MIT &copy; [EGOIST](https://github.com/sponsors/egoist)

---
_Source: https://npm.io/package/@wahyubucil/vite-plugin-compile-time · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
