Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/egoist/vite-plugin-compile-time
Some compile-time magic for your Vite project
https://github.com/egoist/vite-plugin-compile-time
vite
Last synced: about 2 months ago
JSON representation
Some compile-time magic for your Vite project
- Host: GitHub
- URL: https://github.com/egoist/vite-plugin-compile-time
- Owner: egoist
- License: mit
- Created: 2021-12-29T04:06:35.000Z (almost 3 years ago)
- Default Branch: main
- Last Pushed: 2023-08-11T16:35:19.000Z (over 1 year ago)
- Last Synced: 2024-10-23T06:07:23.575Z (2 months ago)
- Topics: vite
- Language: TypeScript
- Homepage:
- Size: 84 KB
- Stars: 133
- Watchers: 2
- Forks: 5
- Open Issues: 15
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
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/vite-plugin-compile-time?v=2)](https://npm.im/vite-plugin-compile-time) [![npm downloads](https://badgen.net/npm/dm/vite-plugin-compile-time?v=2)](https://npm.im/vite-plugin-compile-time)
Use this plugin to generate code at compile time or get data at compile time in your Vite projects.
## Install
```bash
npm i vite-plugin-compile-time -D
```In **vite.config.ts**:
```ts
import { defineConfig } from "vite"
import compileTime from "vite-plugin-compile-time"export default defineConfig({
plugins: [compileTime()],
})
```In **tsconfig.json**:
```json5
{
"compilerOptions": {
// ...
"types": [
// ...,
"vite-plugin-compile-time/client"
]
}
}
```## 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: (file: string) => T
}
```You should return a default export with object containing `code` or `data` property:
```ts
import {
CompileTimeFunctionArgs,
CompileTimeFunctionResult,
} from "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/vite-plugin-compile-time#module-index-export-CompileTimeFunction).
## Sponsors
[![sponsors](https://sponsors-images.egoist.sh/sponsors.svg)](https://github.com/sponsors/egoist)
## License
MIT © [EGOIST](https://github.com/sponsors/egoist)