-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
import.meta
behavior inconsistency between targets
#2294
Comments
ac97be7 |
Thanks. I know the reason. I think the output of // sub.mjs
var import_meta = {};
import_meta.url = "sub";
var sub = () => {
console.log(import_meta.url);
};
// main.mjs
import_meta.url = "main";
var main = () => {
console.log(import_meta.url);
};
main();
sub(); |
Oh, sorry, I see |
There is an existing workaround in tsup/cjs_shims.js by injecting
// cjs_shims.js
var getImportMetaUrl = () => typeof document === "undefined" ? new URL("file:" + __filename).href : document.currentScript && document.currentScript.src || new URL("main.js", document.baseURI).href;
var importMetaUrl = /* @__PURE__ */ getImportMetaUrl();
// sub.mjs
importMetaUrl = "sub";
var sub = () => {
console.log(importMetaUrl);
};
// main.mjs
importMetaUrl = "main";
var main = () => {
console.log(importMetaUrl);
};
main();
sub(); But in fact |
import.meta
has different instances whentarget
is belowes2020
. But it has same instance iftarget
is same or abovees2020
.If it is intented to be unmodified (#208 (comment)) (
import.meta
's scope is the output file), I think it needs to point the same instance.current behaviors
Without bundle
main.mjs
sub.mjs
When I run
node main.mjs
, it outputs:With bundle (
es2020
)After I bundle this with
esbuild --bundle --format=esm --target=es2020 main.mjs
, it becomes:When I run this, it outputs:
With bundle (
es2015
)After I bundle this with
esbuild --bundle --format=esm --target=es2015 main.mjs
, it becomes:When I run this, it outputs:
The text was updated successfully, but these errors were encountered: