Skip to content

Commit 516a96c

Browse files
committed
Export jinja files as TS module at build time
1 parent b6f3c06 commit 516a96c

File tree

4 files changed

+63
-35
lines changed

4 files changed

+63
-35
lines changed

packages/inference/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
src/snippets/templates.generated.ts

packages/inference/package.json

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -26,26 +26,21 @@
2626
},
2727
"files": [
2828
"dist",
29-
"src"
29+
"src",
30+
"!src/snippets/templates/**/*.jinja"
3031
],
3132
"source": "src/index.ts",
3233
"types": "./dist/src/index.d.ts",
3334
"main": "./dist/index.cjs",
3435
"module": "./dist/index.js",
3536
"exports": {
36-
".": {
37-
"types": "./dist/src/index.d.ts",
38-
"require": "./dist/index.cjs",
39-
"import": "./dist/index.js"
40-
}
41-
},
42-
"browser": {
43-
"./src/snippets/index.js": false,
44-
"./dist/index.js": "./dist/browser/index.js",
45-
"./dist/index.mjs": "./dist/browser/index.mjs"
37+
"types": "./dist/src/index.d.ts",
38+
"require": "./dist/index.cjs",
39+
"import": "./dist/index.js"
4640
},
4741
"type": "module",
4842
"scripts": {
43+
"prebuild": "tsx scripts/generate-templates.ts",
4944
"build": "tsup src/index.ts --format cjs,esm --clean && tsc --emitDeclarationOnly --declaration",
5045
"dts": "tsx scripts/generate-dts.ts && tsc --noEmit dist/index.d.ts",
5146
"lint": "eslint --quiet --fix --ext .cjs,.ts .",
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import { readFileSync, writeFileSync, readdirSync } from "node:fs";
2+
import path from "node:path";
3+
4+
const TEMPLATES_DIR = path.join(process.cwd(), "src", "snippets", "templates");
5+
6+
function generateTemplatesCode(): string {
7+
/// language -> client -> templateName -> templateContent
8+
const templates: Record<string, Record<string, Record<string, string>>> = {};
9+
10+
// Read all language directories
11+
const languages = readdirSync(TEMPLATES_DIR);
12+
for (const language of languages) {
13+
const languagePath = path.join(TEMPLATES_DIR, language);
14+
if (!readdirSync(languagePath).length) continue;
15+
16+
templates[language] = {};
17+
18+
// Read all client directories
19+
const clients = readdirSync(languagePath);
20+
for (const client of clients) {
21+
const clientPath = path.join(languagePath, client);
22+
if (!readdirSync(clientPath).length) continue;
23+
24+
templates[language][client] = {};
25+
26+
// Read all template files
27+
const files = readdirSync(clientPath);
28+
for (const file of files) {
29+
if (!file.endsWith(".jinja")) continue;
30+
31+
const templatePath = path.join(clientPath, file);
32+
const templateContent = readFileSync(templatePath, "utf8");
33+
const templateName = path.basename(file, ".jinja");
34+
35+
templates[language][client][templateName] = templateContent;
36+
}
37+
}
38+
}
39+
40+
const templatesJson = JSON.stringify(templates, null, 2);
41+
return `// Generated file - do not edit directly
42+
export const templates: Record<string, Record<string, Record<string, string>>> = ${templatesJson} as const;
43+
`;
44+
}
45+
46+
// Generate and write the templates file
47+
const output = generateTemplatesCode();
48+
const outputPath = path.join(process.cwd(), "src", "snippets", "templates.generated.ts")
49+
writeFileSync(outputPath, output);
50+
console.log("Templates generated successfully! 🚀");

packages/inference/src/snippets/getInferenceSnippets.ts

Lines changed: 6 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,7 @@ import {
1010
import type { InferenceProvider, InferenceTask, RequestArgs } from "../types";
1111
import { Template } from "@huggingface/jinja";
1212
import { makeRequestOptionsFromResolvedModel } from "../lib/makeRequestOptions";
13-
import fs from "fs";
14-
import path from "path";
15-
import { existsSync as pathExists } from "node:fs";
13+
import { templates } from "./templates.generated";
1614

1715
const PYTHON_CLIENTS = ["huggingface_hub", "fal_client", "requests", "openai"] as const;
1816
const JS_CLIENTS = ["fetch", "huggingface.js", "openai"] as const;
@@ -44,34 +42,18 @@ interface TemplateParams {
4442

4543
// Helpers to find + load templates
4644

47-
const rootDirFinder = (): string => {
48-
let currentPath =
49-
typeof import.meta !== "undefined" && import.meta.url
50-
? path.normalize(new URL(import.meta.url).pathname) /// for ESM
51-
: __dirname; /// for CJS
52-
53-
while (currentPath !== "/") {
54-
if (pathExists(path.join(currentPath, "package.json"))) {
55-
return currentPath;
56-
}
57-
58-
currentPath = path.normalize(path.join(currentPath, ".."));
59-
}
60-
61-
return "/";
62-
};
63-
64-
const templatePath = (language: InferenceSnippetLanguage, client: Client, templateName: string): string =>
65-
path.join(rootDirFinder(), "src", "snippets", "templates", language, client, `${templateName}.jinja`);
6645
const hasTemplate = (language: InferenceSnippetLanguage, client: Client, templateName: string): boolean =>
67-
pathExists(templatePath(language, client, templateName));
46+
templates[language]?.[client]?.[templateName] !== undefined;
6847

6948
const loadTemplate = (
7049
language: InferenceSnippetLanguage,
7150
client: Client,
7251
templateName: string
7352
): ((data: TemplateParams) => string) => {
74-
const template = fs.readFileSync(templatePath(language, client, templateName), "utf8");
53+
const template = templates[language]?.[client]?.[templateName];
54+
if (!template) {
55+
throw new Error(`Template not found: ${language}/${client}/${templateName}`);
56+
}
7557
return (data: TemplateParams) => new Template(template).render({ ...data });
7658
};
7759

0 commit comments

Comments
 (0)