binding_web: replace dynamic require with import#4304
Merged
amaanq merged 1 commit intotree-sitter:masterfrom Apr 19, 2025
Merged
binding_web: replace dynamic require with import#4304amaanq merged 1 commit intotree-sitter:masterfrom
amaanq merged 1 commit intotree-sitter:masterfrom
Conversation
Dynamic `require` is not allowed inside ES modules. As a result, loading `web-tree-sitter` was partially broken when loaded using `import`, causing calls to `Language.load(...)` to fail. The `require` call was replaced with `import`, making it work both as a ES module and as a CJS module.
HerringtonDarkholme
approved these changes
Apr 18, 2025
HerringtonDarkholme
left a comment
There was a problem hiding this comment.
thanks! this looks good!
amaanq
approved these changes
Apr 19, 2025
Member
|
thanks! |
|
Successfully created backport PR for |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Dynamic
requireis not allowed inside ES modules.As a result, loading
web-tree-sitterwas partially broken when loadedusing
import, causing calls toLanguage.load(...)to fail.The
requirecall was replaced withimport, making it work bothas a ES module and as a CJS module.
This seems to be most easily demonstrated by trying to run
web-tree-sitterundervitestwhen installing it from npm.It fails when using
import { Parser, Language } from "web-tree-sitter", but succeeds withconst { Language, Parser } = require("web-tree-sitter").My test to reproduce this was minimal - just
Language.load(...).After after making the change in this PR, I ran the tree-sitter tests, as well as trying to run my minimal test with both import methods (ESM and CJS), and it worked ok.
That said, this is my first foray into the JS/TS build-pipeline shenanigans, and I don't feel confident enough to say it doesn't break other usecases.