Skip to content

Comments

feat: added support for import.meta.dirname and import.meta.filename#20050

Merged
alexander-akait merged 9 commits intomainfrom
issue-18320
Oct 28, 2025
Merged

feat: added support for import.meta.dirname and import.meta.filename#20050
alexander-akait merged 9 commits intomainfrom
issue-18320

Conversation

@alexander-akait
Copy link
Member

@alexander-akait alexander-akait commented Oct 24, 2025

Summary

Added support to import.meta.dirname and import.meta.filename

Also:

  • fixed problem when developer has __dirname or __filename local variables (we use this names when get __dirname and __filename from import.meta.url), now we use __webpack_dirname__ and __webpack_filename__
  • now __dirname for ES modules has a right value, using ../ is not working to get dirname, now we use regexp

fixes #19990
fixes #18320

What kind of change does this PR introduce?

feature and bugfixes

Did you add tests for your changes?

WIP

Does this PR introduce a breaking change?

No

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

Yes, we need to show that we handle import.meta.dirname and import.meta.filename

@github-actions
Copy link
Contributor

github-actions bot commented Oct 24, 2025

This PR is packaged and the instant preview is available (6d02aea).

Install it locally:

  • npm
npm i -D https://pkg.pr.new/webpack@6d02aea
  • yarn
yarn add -D https://pkg.pr.new/webpack@6d02aea
  • pnpm
pnpm add -D https://pkg.pr.new/webpack@6d02aea

.importMetaDirnameAndFilename
) {
return true;
}
Copy link
Member Author

Choose a reason for hiding this comment

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

Don't replace import.meta.dirname and import.meta.filename when env supports them

return true;
});
}
break;
Copy link
Member Author

Choose a reason for hiding this comment

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

Prevent removing import.meta.filename


return true;
});
}
Copy link
Member Author

Choose a reason for hiding this comment

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

Prevent removing import.meta.dirname

dirname,
(functionName) =>
`${functionName}(${importMetaName}.url + "/..").slice(0, -1)`
`${functionName}(${importMetaName}.url.replace(/\\/(?:[^\\/]*)$/, ""))`,
Copy link
Member Author

Choose a reason for hiding this comment

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

Using regexp to replace the last /file.ext to get a right value for dirname

@codspeed-hq
Copy link

codspeed-hq bot commented Oct 24, 2025

CodSpeed Performance Report

Merging #20050 will degrade performances by 10.17%

Comparing issue-18320 (6d02aea) with main (277fc0b)

Summary

❌ 1 regression
✅ 68 untouched

⚠️ Please fix the performance issues or acknowledge them on CodSpeed.

Benchmarks breakdown

Benchmark BASE HEAD Change
benchmark "react", scenario '{"name":"mode-development-rebuild","mode":"development","watch":true}' 39.5 ms 43.9 ms -10.17%

parser.hooks.expression
.for(expressionName)
.tap(PLUGIN_NAME, (expr) => {
.tap({ name: PLUGIN_NAME, stage: -100 }, (expr) => {
Copy link
Member

Choose a reason for hiding this comment

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

for tap ordering, we should have a constant instead of hard-setting it to a value

@alexander-akait alexander-akait merged commit a576d0f into main Oct 28, 2025
50 of 52 checks passed
@alexander-akait alexander-akait deleted the issue-18320 branch October 28, 2025 14:50
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