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

feature request: dirname & co #16

Open
nlambert opened this issue Nov 9, 2024 · 2 comments
Open

feature request: dirname & co #16

nlambert opened this issue Nov 9, 2024 · 2 comments

Comments

@nlambert
Copy link

nlambert commented Nov 9, 2024

Howdy! The documentation was clear, but I realize I inferred a bit too much from the plugin name and expected everything under import.meta to be transpiled.

Node.js has extended import.meta with properties like dirname as outlined here:

And for browsers I think the properties are just url and resolve.

Babel and this plugin seem like the best way to keep ES code and CJS hack free.

I’m not sure about the feasibility, but I wonder if it’s possible for the plugin to transpile each of these properties. Thanks for all the work you put into this plugin!

@javiertury
Copy link
Owner

It looks doable and I am open to PRs (with tests). The implementation seems straightforward

  • import.meta.dirname: Alias for __dirname
  • import.meta.filename: Alias for __filename
  • import.meta.resolve: In CommonJs, it wraps global require.resolve() with url.pathToFileURL(). In ES6, it seems equivalent to
import { createRequire } from 'module';
import url from 'url';

const require = createRequire(url.pathToFileURL(__filename).toString());
const output = url.pathToFileURL(require.resolve('<input specifier>'));

However I won't have time to code it myself.

@javiertury
Copy link
Owner

The branch new_meta_apis is a quick draft for the desired functionality. If someone is really interested in those apis, they could work on a PR starting from that branch. To make it production ready, someone should make sure that the transformed output makes sense and complies with the import.meta specifications, test coverage should increase and documentation should be updated.

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

2 participants