Skip to content

Commit

Permalink
feat: new compileTime api
Browse files Browse the repository at this point in the history
  • Loading branch information
egoist committed Dec 22, 2024
1 parent 402e84a commit ec18017
Show file tree
Hide file tree
Showing 21 changed files with 623 additions and 272 deletions.
12 changes: 12 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# EditorConfig is awesome: https://EditorConfig.org

# top-most EditorConfig file
root = true

[*]
indent_style = space
indent_size = 2
end_of_line = lf
charset = utf-8
trim_trailing_whitespace = false
insert_final_newline = false
5 changes: 3 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,13 @@ on:

jobs:
test:
if: "!contains(github.event.head_commit.message, 'ci skip')"
if: >-
!contains(github.event.head_commit.message, 'ci skip')
strategy:
matrix:
os: [ubuntu-latest, windows-latest]
node-version: [18.x]
node-version: [22.x]

runs-on: ${{ matrix.os }}

Expand Down
66 changes: 9 additions & 57 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

[![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.
Use this plugin to get data at compile time in your Vite projects.

## Install

Expand All @@ -27,12 +27,12 @@ export default defineConfig({

In **tsconfig.json**:

```json5
```json
{
"compilerOptions": {
// ...
"types": [
// ...,
// ...,
"vite-plugin-compile-time/client"
]
}
Expand All @@ -44,73 +44,25 @@ In **tsconfig.json**:
Compile-time data:

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

export default async () => {
const post = compileTime(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
}
assert.equal(post, "...the content of the post...")
```

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"],
}
}
```
## Caveats

See the type docs on [paka.dev](https://paka.dev/npm/vite-plugin-compile-time#module-index-export-CompileTimeFunction).
The files where you call `compileTime` will be evaluated at build time in Node.js environment, which means you should avoid calling browser APIs on the top level. It's recommended to use `compileTime` in a separate file and import it in your app.

## Sponsors

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

## License

Expand Down
6 changes: 3 additions & 3 deletions client.d.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
declare interface ImportMeta {
compileTime: <T>(id: string) => T
}
type MaybePromise<T> = T | Promise<T>

declare const compileTime: <T>(fn: () => MaybePromise<T>) => T
20 changes: 20 additions & 0 deletions example/a.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import fs from "fs"
import path from "path"
import { sum } from "./dep"

export const a = compileTime(async () => {
const message = await fs.promises.readFile(
path.join(__dirname, "message.txt"),
"utf-8",
)
return {
message,
count: sum(1, 25),
}
})

const another = compileTime(async () => {
return "another"
})

console.log("build", another)
1 change: 1 addition & 0 deletions example/b.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const b = "b"
2 changes: 1 addition & 1 deletion example/dep.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export const sum = (a: number, b: number) => a + b
export const sum = (a: number, b: number) => a + b + 1
5 changes: 0 additions & 5 deletions example/generate-code.ts

This file was deleted.

10 changes: 0 additions & 10 deletions example/get-data.ts

This file was deleted.

9 changes: 3 additions & 6 deletions example/main.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
const data = import.meta.compileTime("./get-data.ts")
import { a } from "./a"
import { b } from "./b"

let count = 40

import.meta.compileTime("./generate-code.ts")

console.log(data, count)
console.log(a, b)
1 change: 1 addition & 0 deletions example/message.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
hello world!
1 change: 1 addition & 0 deletions inject.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const compileTime = (fn) => fn()
21 changes: 14 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,20 +1,22 @@
{
"name": "vite-plugin-compile-time",
"version": "0.3.2",
"type": "module",
"description": "Do some compile time work in your Vite project",
"publishConfig": {
"access": "public"
},
"files": [
"dist",
"/client.d.ts"
"/client.d.ts",
"/inject.js"
],
"main": "./dist/index.js",
"module": "./dist/index.mjs",
"main": "./dist/index.cjs",
"module": "./dist/index.js",
"exports": {
".": {
"require": "./dist/index.js",
"import": "./dist/index.mjs"
"require": "./dist/index.cjs",
"import": "./dist/index.js"
},
"./client": {
"types": "./client.d.ts"
Expand All @@ -30,16 +32,21 @@
"license": "MIT",
"devDependencies": {
"@egoist/prettier-config": "1.0.0",
"@types/babel__generator": "^7.6.8",
"@types/babel__traverse": "^7.20.6",
"prettier": "3.0.1",
"sucrase": "3.34.0",
"tsup": "8.3.5",
"typescript": "5.7.2",
"vite": "6.0.1",
"vite-tsconfig-paths": "^5.1.3",
"vitest": "2.1.6"
},
"dependencies": {
"bundle-require": "^5.0.0",
"@babel/generator": "^7.26.3",
"@babel/parser": "^7.26.3",
"@babel/traverse": "^7.26.4",
"babel-dead-code-elimination": "^1.0.8",
"bundle-require": "^5.1.0",
"devalue": "^5.1.1",
"esbuild": "^0.24.0",
"magic-string": "^0.30.14"
Expand Down
Loading

0 comments on commit ec18017

Please sign in to comment.