{"id":15634762,"url":"https://github.com/egoist/vite-plugin-compile-time","last_synced_at":"2025-10-09T00:21:29.054Z","repository":{"id":42851152,"uuid":"442655918","full_name":"egoist/vite-plugin-compile-time","owner":"egoist","description":"Some compile-time magic for your Vite project","archived":false,"fork":false,"pushed_at":"2024-12-30T17:56:43.000Z","size":132,"stargazers_count":280,"open_issues_count":17,"forks_count":8,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-04-03T21:11:22.197Z","etag":null,"topics":["vite"],"latest_commit_sha":null,"homepage":"","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/egoist.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null},"funding":{"github":"egoist"}},"created_at":"2021-12-29T04:06:35.000Z","updated_at":"2025-04-01T14:04:44.000Z","dependencies_parsed_at":"2025-02-19T00:10:54.655Z","dependency_job_id":"cd600a2e-13d2-433c-8714-204daa869447","html_url":"https://github.com/egoist/vite-plugin-compile-time","commit_stats":null,"previous_names":[],"tags_count":17,"template":false,"template_full_name":"egoist/ts-lib-starter","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/egoist%2Fvite-plugin-compile-time","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/egoist%2Fvite-plugin-compile-time/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/egoist%2Fvite-plugin-compile-time/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/egoist%2Fvite-plugin-compile-time/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/egoist","download_url":"https://codeload.github.com/egoist/vite-plugin-compile-time/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248616091,"owners_count":21133998,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["vite"],"created_at":"2024-10-03T10:56:36.951Z","updated_at":"2025-10-09T00:21:24.012Z","avatar_url":"https://github.com/egoist.png","language":"TypeScript","funding_links":["https://github.com/sponsors/egoist","https://github.com/sponsors/egoist)."],"categories":["TypeScript"],"sub_categories":[],"readme":"**💛 You can help the author become a full-time open-source maintainer by [sponsoring him on GitHub](https://github.com/sponsors/egoist).**\n\n---\n\n# vite-plugin-compile-time\n\n[![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)\n\n## Why\n\nUse this plugin to run code at compile time and inline the return data in your Vite projects.\n\n## Install\n\n```bash\nnpm i vite-plugin-compile-time -D\n```\n\nIn **vite.config.ts**:\n\n```ts\nimport { defineConfig } from \"vite\"\nimport compileTime from \"vite-plugin-compile-time\"\n\nexport default defineConfig({\n  plugins: [compileTime()],\n})\n```\n\nAdd a `shims.d.ts` with the following code:\n\n```ts\n/// \u003creference types=\"vite-plugin-compile-time/client\" /\u003e\n```\n\n## Usage\n\nYou can use `compileTime` anywhere as long as it's called on the top-level, i.e. not inside a closure:\n\n```ts\nimport fs from \"fs\"\n\n// ✅\nconst content = compileTime(fs.readFileSync(\"./post.md\", \"utf8\"))\n\n// ❌\nconst content = () =\u003e compileTime(fs.readFileSync(\"./post.md\", \"utf8\"))\n```\n\nFor more complex more you can use a function instead:\n\n```ts\nimport fs from \"fs\"\n\n// it also accepts async function\nconst post = compileTime(async () =\u003e {\n  const content = await fs.promises.readFile(\"./post.md\", \"utf8\")\n  const frontmatter = getFrontmatter(content)\n  return { frontmatter, content }\n})\n```\n\n### Standalone `.compile.ts` files\n\nOr `.compile.js`\n\nAlternatively, you can use a standalone file to evaluate at compile time:\n\n```ts\n// post.compile.ts\nexport const content = fs.readFileSync(\"./post.md\", \"utf8\")\n\nexport const fetchContent = async () =\u003e {\n  return fetch(\"https://example.com\")\n}\n\n// main.ts\nimport { content, fetchContent } from \"./post.compile\"\n\ncontent //=\u003e Buffer\n\nawait fetchContent() //=\u003e Response\n```\n\nIf you export an async function, you can actually call it without using `await` because it's pre-evaluated before you use it, for type-safe purpose you can wrap the function with `compileTime`:\n\n```ts\nexport const content = compileTime(async () =\u003e {\n  return fetch(\"https://example.com\")\n})\n\n// Now available as a value\n// You don't even need to call it\ncontent //=\u003e Response\n```\n\nHowever `compileTime` is optional in `.compile.ts` files, you only need it if you don't want to `await` and want type-safety.\n\n### Supported data types\n\n- JSON-serializable types like `string`, `number`, `boolean`\n- regular expressions\n- dates\n- Map and Set\n- BigInt\n- ArrayBuffer and Typed Arrays\n- Response\n- Buffer\n\n## Caveats\n\nThe 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.\n\n## Sponsors\n\n[![sponsors](https://sponsors-images.egoist.dev/sponsors.svg)](https://github.com/sponsors/egoist)\n\n## License\n\nMIT \u0026copy; [EGOIST](https://github.com/sponsors/egoist)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fegoist%2Fvite-plugin-compile-time","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fegoist%2Fvite-plugin-compile-time","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fegoist%2Fvite-plugin-compile-time/lists"}