Skip to content

Commit 24042d7

Browse files
committed
do not bundle snippets code if browser mode
1 parent d02634c commit 24042d7

File tree

2 files changed

+16
-30
lines changed

2 files changed

+16
-30
lines changed

packages/inference/package.json

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,16 @@
3333
"main": "./dist/index.cjs",
3434
"module": "./dist/index.js",
3535
"exports": {
36-
"types": "./dist/src/index.d.ts",
37-
"require": "./dist/index.cjs",
38-
"import": "./dist/index.js"
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": false,
44+
"./dist/index.js": "./dist/browser/index.js",
45+
"./dist/index.mjs": "./dist/browser/index.mjs"
3946
},
4047
"type": "module",
4148
"scripts": {

packages/inference/src/snippets/python.ts

Lines changed: 6 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ import {
1010
} from "@huggingface/tasks";
1111
import type { InferenceProvider } from "../types";
1212
import { Template } from "@huggingface/jinja";
13+
import fs from "fs";
14+
import path from "path";
15+
import { existsSync as pathExists } from "node:fs";
1316

1417
const TOOLS = ["huggingface_hub", "requests", "fal_client", "openai"];
1518

@@ -26,25 +29,7 @@ interface TemplateParams {
2629
}
2730

2831
// Helpers to find + load templates
29-
function isNode(): boolean {
30-
return typeof process !== "undefined" && process.versions != null && process.versions.node != null;
31-
}
32-
33-
function loadNodeModules() {
34-
if (!isNode()) {
35-
throw new Error("Inference snippet generation is only available in Node.js environment");
36-
}
37-
// eslint-disable-next-line @typescript-eslint/no-var-requires
38-
const fs = require("fs");
39-
// eslint-disable-next-line @typescript-eslint/no-var-requires
40-
const path = require("path");
41-
// eslint-disable-next-line @typescript-eslint/no-var-requires
42-
const pathExists = require("fs").existsSync;
43-
return { fs, path, pathExists };
44-
}
45-
4632
const rootDirFinder = (): string => {
47-
const { path, pathExists } = loadNodeModules();
4833
let currentPath = path.normalize(import.meta.url).replace("file:", "");
4934

5035
while (currentPath !== "/") {
@@ -58,17 +43,11 @@ const rootDirFinder = (): string => {
5843
return "/";
5944
};
6045

61-
const templatePath = (tool: string, templateName: string): string => {
62-
const { path } = loadNodeModules();
63-
return path.join(rootDirFinder(), "src", "snippets", "templates", "python", tool, `${templateName}.jinja`);
64-
};
65-
const hasTemplate = (tool: string, templateName: string): boolean => {
66-
const { pathExists } = loadNodeModules();
67-
return pathExists(templatePath(tool, templateName));
68-
};
46+
const templatePath = (tool: string, templateName: string): string =>
47+
path.join(rootDirFinder(), "src", "snippets", "templates", "python", tool, `${templateName}.jinja`);
48+
const hasTemplate = (tool: string, templateName: string): boolean => pathExists(templatePath(tool, templateName));
6949

7050
const loadTemplate = (tool: string, templateName: string): ((data: TemplateParams) => string) => {
71-
const { fs } = loadNodeModules();
7251
const template = fs.readFileSync(templatePath(tool, templateName), "utf8");
7352
return (data: TemplateParams) => new Template(template).render({ ...data });
7453
};

0 commit comments

Comments
 (0)