Skip to content
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 is empty #2249

Closed
davidmz opened this issue May 13, 2022 · 3 comments
Closed

import.meta is empty #2249

davidmz opened this issue May 13, 2022 · 3 comments

Comments

@davidmz
Copy link

davidmz commented May 13, 2022

I'm trying to use the gifsicle package in one of my projects, and have an import.meta.url issue. Here is a simple repo that reproduces the problem: https://github.com/davidmz/gifsicle-esbuild-test

Try to compile code with the npm run build command and then run it with npm run run. You will see the ERR_INVALID_URL error. This line of gifsicle code uses the import.meta.url:

const pkg = JSON.parse(fs.readFileSync(new URL('../package.json', import.meta.url)));

and esbuild compiled it to the:

var import_meta = {};
var pkg = JSON.parse(import_node_fs.default.readFileSync(new URL("../package.json", import_meta.url)));

What do I (or, maybe, the gifsicle authors) wrong?

@hyrious
Copy link

hyrious commented May 13, 2022

You have to make the code work:

fs.readFileSync(new URL('../package.json', import.meta.url))

In 2 ways,

  • Don't bundle that library. You can add --external:gifsicle to your build command so that this code will not change and it will work if your node runtime supports import.meta.
  • Modify this code to be bundle-able. You can write a plugin to replace this line with what it expects (the contents of the package.json).

Why is import.meta.url not bundle-able? Because this is a runtime feature, it only exists in ESM context. So esbuild will add var import_meta = {} in non-ESM formats (like CJS/IIFE) to make it not a syntax error. In which case not all users want this result. However doing polyfill about this thing is also not always correct. I'll show you why in your example:

import.meta.url is path/to/node_modules/gifsicle/lib/index.js when it is in the source code. However, when it gets bundled, it becomes path/to/out.js. See the difference?

@evanw
Copy link
Owner

evanw commented May 15, 2022

Thanks for the comment. I agree that making gifsicle external makes the most sense here, as it's not easy to bundle. Gifsicle also appears to invoke a native binary executable so bundling it also doesn't make sense for that reason. Even if you were to bundle it somehow, you would still need to keep the node_modules file around anyway since I assume gifsicle wouldn't work without the binary executable.

Closing as "working as intended."

@evanw evanw closed this as completed May 15, 2022
@davidmz
Copy link
Author

davidmz commented May 16, 2022

Thank you so much for the detailed explanations!

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

No branches or pull requests

3 participants