0.2.9 • Published 12 months ago

@wahyubucil/vite-plugin-compile-time v0.2.9

Weekly downloads
-
License
MIT
Repository
-
Last release
12 months ago

💛 You can help the author become a full-time open-source maintainer by sponsoring him on GitHub.


vite-plugin-compile-time

npm version npm downloads

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 is updated, use it!

This package is ESM-only. Read more on how to use Pure ESM Package.

Install

npm i @wahyubucil/vite-plugin-compile-time -D
import { defineConfig } from "vite"
import compileTime from "@wahyubucil/vite-plugin-compile-time"

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

Usage

Compile-time data:

// 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:

// 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.

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

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

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.

Sponsors

sponsors

License

MIT © EGOIST

0.2.9

12 months ago

0.2.8

12 months ago

0.2.7

12 months ago

0.2.6

12 months ago

0.2.5

12 months ago

0.2.4

12 months ago