-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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
Implement import.meta.resolve() #3873
Conversation
This implements and tests `import.meta.resolve()` a way to get the path to a module the same way ESM and/or `require` will do. This doesn't read or load the file in question from the file. This also showed that we currently always parse code as ECMAScript modules. Even when the parsed/compiled file was to be wrapped as CommonJS. This likely had no other side effects as all other ESM specific syntax is also either not implemented in k6 or forces CommonJS wrapping disabled. Closes #3856
js/bundle.go
Outdated
@@ -451,6 +451,16 @@ func (b *Bundle) setInitGlobals(rt *sobek.Runtime, vu *moduleVUImpl, modSys *mod | |||
} | |||
warnAboutModuleMixing("module") | |||
warnAboutModuleMixing("exports") | |||
|
|||
rt.SetFinalImportMeta(func(o *sobek.Object, mr sobek.ModuleRecord) { | |||
_ = o.Set("resolve", func(specifier string) (string, error) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Shouldn't we do something with the error in case there's any? If it isn't supposed to happen, at all, I'm generally in favor of panicking.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I left a couple of minor comments, but it looks generally good!
Co-authored-by: Joan López de la Franca Beltran <5459617+joanlopez@users.noreply.github.com>
What?
This implements and tests
import.meta.resolve()
a way to get the path to a module the same way ESM and/orrequire
will do.This doesn't read or load the file in question from the file.
This also showed that we currently always parse code as ECMAScript modules. Even when the parsed/compiled file was to be wrapped as CommonJS.
This likely had no other side effects as all other ESM specific syntax is also either not implemented in k6 or forces CommonJS wrapping disabled.
Why?
This lets people use
open
,require
,experimental/fs.open
, etc. and have a way to always be relative to the module it is written in.The fixes to compiler were as otherwise CommonJS modules don't error on
import.meta
and doesn't just isn't supported in the standard.Checklist
make lint
) and all checks pass.make tests
) and all tests pass.Related PR(s)/Issue(s)
#3856