Skip to content

Comments

feat(import.meta): add dirname and filename#19990

Closed
avivkeller wants to merge 1 commit intowebpack:mainfrom
forked-by-avivkeller:import-meta-dirname-filename
Closed

feat(import.meta): add dirname and filename#19990
avivkeller wants to merge 1 commit intowebpack:mainfrom
forked-by-avivkeller:import-meta-dirname-filename

Conversation

@avivkeller
Copy link
Member

@avivkeller avivkeller commented Oct 7, 2025

Summary

Fixes #18320 by adding the requested import.meta properties.

What kind of change does this PR introduce?

This PR adds support for import.meta.{dirname,filename}, having them return the identical data that __dirname and __filename return.

Did you add tests for your changes?

Yes. I verified that import.meta.dirname + [filename] === import.meta.filename === an existing file.
Does this PR introduce a breaking change?

No, this is a semver-minor change.

If relevant, what needs to be documented once your changes are merged or what have you already documented?

Once merged, the additional properties must be documented accordingly.

Copy link
Member

@alexander-akait alexander-akait left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It should not work like here, import.meta.dirname and import.meta.filename arenot supported by browsers (and maybe other envs), we should do the same as for __dirname and __filename, look at NdeStuffPlugin.js and we should do the same

Developer can use fs.readFileSync(import.meta.filename) and after bundling the path will be broken

@codspeed-hq
Copy link

codspeed-hq bot commented Oct 7, 2025

CodSpeed Performance Report

Merging #19990 will not alter performance

Comparing forked-by-avivkeller:import-meta-dirname-filename (06dcb6c) with main (0f84d1e)

Summary

✅ 42 untouched

@avivkeller
Copy link
Member Author

It should not work like here, import.meta.dirname and import.meta.filename arenot supported by browsers (and maybe other envs), we should do the same as for __dirname and __filename, look at NdeStuffPlugin.js and we should do the same

Developer can use fs.readFileSync(import.meta.filename) and after bundling the path will be broken

You mean like a variable response depending on the value of node?

@alexander-akait
Copy link
Member

@avivkeller Yeah, we should duplicate logic, but for such things, and improve node-module output for commonjs when import.meta.dirname and import.meta.filename are supported (look at lib/config/target.js), also make sense to introduce node-commonjs value when you want to bundle ES modules to commonjs.

Feel free to split these steps into multiple commits/PRs

@avivkeller avivkeller marked this pull request as draft October 7, 2025 16:43
@avivkeller avivkeller changed the title feat(import.meta): add dirname and filename [WIP] feat(import.meta): add dirname and filename Oct 8, 2025
@avivkeller avivkeller force-pushed the import-meta-dirname-filename branch 3 times, most recently from 1394761 to c9364e1 Compare October 10, 2025 20:33
@avivkeller avivkeller marked this pull request as ready for review October 10, 2025 20:52
@avivkeller avivkeller changed the title [WIP] feat(import.meta): add dirname and filename feat(import.meta): add dirname and filename Oct 10, 2025
@avivkeller avivkeller force-pushed the import-meta-dirname-filename branch from 3d3ec97 to 06dcb6c Compare October 12, 2025 20:06
@avivkeller
Copy link
Member Author

Bump @alexander-akait

@alexander-akait
Copy link
Member

@avivkeller Yeah, in my roadmap to review (will be part of the next minor release)

if (!parser.state.module) return;
const resource = parseResource(parser.state.module.resource);
return evaluateToString(resource.path)(expr);
if (isCJS) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

global supported in ES modules and CommonJS, so we should always handle this

valueGetter(parser.state.module),
/** @type {Range} */ (expr.range),
[RuntimeGlobals.global]
/** @type {EXPECTED_ANY} */ (prop)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't use EXPECTED_ANY, we have types here

Copy link
Member

@alexander-akait alexander-akait left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you avoid refactoring here and implement only import.meta.__dirname and import.meta.__filename support and then we will do refactor in separate PR, hard to review and a lot of not related to the issue changes

@avivkeller
Copy link
Member Author

The slight refactor makes it easier to keep the code DRY, since logic has to be repeated for ESM and CJS implementations.

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

Successfully merging this pull request may close these issues.

support import.meta.dirname and import.meta.filename

2 participants