Skip to content

Do not export ./snippets module in browser mode #1259

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 12 commits into from
Mar 11, 2025

Conversation

Wauplin
Copy link
Contributor

@Wauplin Wauplin commented Mar 7, 2025

⚠️ PR opened on top of #1255

Related to this thread #1255 (comment).

@coyotte508 @SBrandeis @julien-c WDYT? 🙈

for the record, I'm looking for a solution where:

  1. we keep this public/open-source
  2. we keep the structure of jinja templates
  3. ideally no new package in hf.js
  4. ideally no new tooling (e.g. to translate jinja into TS code)

Solution here is simply to gracefully raise an error if environment not supported.
EDIT: based on #1259 (comment), goal is to not package the ./snippets module in browser mode.

@coyotte508
Copy link
Member

coyotte508 commented Mar 7, 2025

hm you should go the @huggingface/hub way, for functions like download-file-to-cache-dir

They have this section:

"browser": {
"./src/utils/sha256-node.ts": false,
"./src/utils/FileBlob.ts": false,
"./src/lib/cache-management.ts": false,
"./src/lib/download-file-to-cache-dir.ts": false,
"./src/lib/snapshot-download.ts": false,
"./dist/index.js": "./dist/browser/index.js",
"./dist/index.mjs": "./dist/browser/index.mjs"
},
and the tsup config https://github.com/huggingface/huggingface.js/blob/5cd51b9901f173d286148531a41cd1193f76b74a/packages/hub/tsup.config.ts#L1-L24

Then you can export snippets only for non-browser context

I think otherwise you'll still have issues importing node packages even if you don't use them, for versions of the lib released on the CDNs

@Wauplin
Copy link
Contributor Author

Wauplin commented Mar 7, 2025

I'll look into it, thanks @coyotte508 !

@HuggingFaceDocBuilderDev

The docs for this PR live here. All of your documentation changes will be reflected on that endpoint. The docs are available until 30 days after the last update.

@Wauplin Wauplin changed the title Raise if not node Do not export ./snippets module in browser mode Mar 10, 2025
Comment on lines 6 to 10
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 😄

@Wauplin
Copy link
Contributor Author

Wauplin commented Mar 11, 2025

Thanks for fixing things @coyotte508 ! Ok to merge it now? (into #1255 I mean)

@coyotte508
Copy link
Member

yes

@Wauplin Wauplin merged commit d2579ab into templated-inference-python-snippets Mar 11, 2025
4 of 5 checks passed
@Wauplin Wauplin deleted the raise-if-not-node branch March 11, 2025 10:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants