Skip to content
18 changes: 15 additions & 3 deletions packages/inference/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,21 @@
"main": "./dist/index.cjs",
"module": "./dist/index.js",
"exports": {
"types": "./dist/src/index.d.ts",
"require": "./dist/index.cjs",
"import": "./dist/index.js"
".": {
"types": "./dist/src/index.d.ts",
"require": "./dist/index.cjs",
"import": "./dist/index.js"
},
"./snippets": {
"types": "./dist/snippets/index.d.ts",
"require": "./dist/snippets/index.cjs",
"import": "./dist/snippets/index.js"
}
},
"browser": {
"./src/snippets": false,
"./dist/index.js": "./dist/browser/index.js",
"./dist/index.mjs": "./dist/browser/index.mjs"
},
"type": "module",
"scripts": {
Expand Down
5 changes: 4 additions & 1 deletion packages/inference/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,8 @@ export { InferenceOutputError } from "./lib/InferenceOutputError";
export * from "./types";
export * from "./tasks";

import * as snippets from "./snippets/index.js";
let snippets = {};
if (typeof window === "undefined") {
snippets = import("./snippets").then((mod) => mod.default);
}
export { snippets };
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@coyotte508 what would be the best way to do a conditional import here? I haven't found any convincing solution. In @huggingface/hub it's not really a problem as everything is included using export * from "./lib"; (here).

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure there's an issue? Is there an error when just keeping the code as before?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it felt weird to do import * as snippets from "./snippets/index.js"; when we know ./snippets/index.js might not be in the final package. But looks like tooling is done the correct way so perfect 😄

2 changes: 1 addition & 1 deletion packages/inference/src/snippets/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ import * as curl from "./curl.js";
import * as python from "./python.js";
import * as js from "./js.js";

export { curl, python, js };
export default { curl, python, js };
1 change: 0 additions & 1 deletion packages/inference/src/snippets/python.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ interface TemplateParams {
}

// Helpers to find + load templates

const rootDirFinder = (): string => {
let currentPath = path.normalize(import.meta.url).replace("file:", "");

Expand Down
23 changes: 23 additions & 0 deletions packages/inference/tsup.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import type { Options } from "tsup";

const baseConfig: Options = {
entry: ["./index.ts"],
format: ["cjs", "esm"],
outDir: "dist",
clean: true,
};

const nodeConfig: Options = {
...baseConfig,
platform: "node",
};

const browserConfig: Options = {
...baseConfig,
platform: "browser",
target: "es2018",
splitting: true,
outDir: "dist/browser",
};

export default [nodeConfig, browserConfig];
2 changes: 1 addition & 1 deletion packages/tasks-gen/scripts/generate-snippets-fixtures.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import { existsSync as pathExists } from "node:fs";
import * as fs from "node:fs/promises";
import * as path from "node:path/posix";

import { snippets } from "@huggingface/inference";
import { snippets } from "@huggingface/inference/snippets";
import type { SnippetInferenceProvider, InferenceSnippet, ModelDataMinimal } from "@huggingface/tasks";

type LANGUAGE = "sh" | "js" | "py";
Expand Down
Loading