Skip to content

feat: support import.meta.resolve() for asset url resolution - #20568

Closed
aryanraj45 wants to merge 3 commits into
webpack:mainfrom
aryanraj45:feat/import-meta-resolve
Closed

aryanraj45 wants to merge 3 commits into
webpack:mainfrom
aryanraj45:feat/import-meta-resolve

Conversation

@aryanraj45

Copy link
Copy Markdown
Member

Fixes #16693

Summary

import.meta.resolve() is a browser-standard API that returns the resolved URL of a module or asset as a string. Webpack didn't handle this at all — it would either pass it through unchanged or warn. This PR teaches webpack to recognize and process it.

The implementation hooks into the existing URLDependency infrastructure that already handles new URL("./foo", import.meta.url). When the parser sees import.meta.resolve("./foo"), it creates a URLDependency for "./foo" with a new resolveUrl: true flag. In the dependency template, that flag changes the output from new URL(..., __webpack_require__.b) (a URL object) to new URL(..., __webpack_require__.b).href (a plain string), which matches what import.meta.resolve is supposed to return per the spec.

The parser side registers two hooks: one to tell webpack that import.meta.resolve is a known identifier (so it doesn't bail out early), and one to intercept the actual call expression, evaluate the first argument as a static string, and emit the dependency.

What kind of change does this PR introduce?

feat

Did you add tests for your changes?

Yes, added under test/configCases/url/import-meta-resolve/.

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?

Document that import.meta.resolve("./foo") is now supported and behaves like new URL("./foo", import.meta.url).href for asset URLs.

Use of AI

No, Just for editing the PR description

@changeset-bot

changeset-bot Bot commented Mar 3, 2026 •

Copy link
Copy Markdown

⚠️ No Changeset found

Latest commit: bb6edb8

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

@alexander-akait

Copy link
Copy Markdown
Member

This should be under the option and disabled by default

@alexander-akait

Copy link
Copy Markdown
Member

Please start a discussion in the future. Firstly, things like this haven't been implemented, not because they're difficult, most packages use import.meta.resolve for dynamic things, the second we need to be align with other tools here (and it is still not resolved)

@aryanraj45
aryanraj45 force-pushed the feat/import-meta-resolve branch 2 times, most recently from 7b03253 to 703ecdf Compare March 3, 2026 15:56
@aryanraj45

Copy link
Copy Markdown
Member Author

@alexander-akait sure I'll keep this thing on my mind to start a discussion before working on a PR also actually I did read through the existing thread on the issue before writing the PR, RReverser and lgarron had already aligned on the [new URL] approach so I went ahead in that direction only,Won't skip this step from next time

@aryanraj45

Copy link
Copy Markdown
Member Author

This should be under the option and disabled by default

I have Updated it to put this behind [importMetaResolve] option, disabled by default, as you suggested.
Thanks

@aryanraj45
aryanraj45 force-pushed the feat/import-meta-resolve branch 2 times, most recently from c1cebca to 0bbeb85 Compare March 3, 2026 16:10

@alexander-akait alexander-akait left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

All code should be in file ImportMetaPlugin, not in URLDependecy or URLParserPlugin, they are for new URL(...) construction

@aryanraj45
aryanraj45 force-pushed the feat/import-meta-resolve branch 2 times, most recently from 308d32b to 0bbeb85 Compare March 3, 2026 18:07
@aryanraj45

aryanraj45 commented Mar 4, 2026 •

Copy link
Copy Markdown
Member Author

All code should be in file ImportMetaPlugin, not in URLDependecy or URLParserPlugin, they are for new URL(...) construction

@alexander-akait, I have moved all import.meta.resolve() logic to [ImportMetaPlugin] .Can u please take a look
Thanks!

parser.state.current.addDependency(dep);
return true;
});
}

@alexander-akait alexander-akait Mar 4, 2026 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

You forgot:

parser.hooks.typeof
						.for("import.meta.resolve")
						.tap(
							PLUGIN_NAME,
							toConstantDependency(parser, JSON.stringify("function"))
						);

Also you don't have it here - https://github.com/aryanraj45/webpack/blob/700b19b7792de50df00766cd56e833dd53622309/lib/dependencies/ImportMetaPlugin.js#L251

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

And evaluateTypeof

() => ["resolve"],
true
)
);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

What is this?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I Kept evaluateIdentifier as walkCallExpression evaluates the callee first, and if it doesn't resolve to a known identifier, isIdentifier() returns false and the call hook never fires. Without it the parser just walks import.meta as a bare expression and the critical dependency warning shows up, I have also Confirmed by removing it and running the test.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Please add a test case where we have import.meta.resolve(variable) (where we use value from let/const/var) and import.meta.resolve(unknown), both should work and keep logic import.meta.resolve call for ECMA output, for non ECMA output they should be behave like require.resolve in commonjs

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

i.e. we should keep import.meta.resolve with call as is for ECMA output and emulate it using dependency only for non ECMA output

@aryanraj45

Copy link
Copy Markdown
Member Author

@alexander-akait updated with adding typeof and evalutetypeof hooks and case "resolve" as suggested please take a look Thanks for the suggestion

Comment thread lib/dependencies/ImportMetaPlugin.js Outdated
importMetaResolve
? "import.meta.resolve"
: importMetaUnknownProperty(["resolve"])
},`;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Only when option is enabled, it it should work, not to importMetaUnknownProperty

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Fixed. Falls through to [default]when option is disabled, no separate call needed there.

Comment thread lib/dependencies/ImportMetaPlugin.js Outdated
parser.hooks.call
.for("import.meta.resolve")
.tap(PLUGIN_NAME, (_expr) => {
const expr = /** @type {CallExpression} */ (_expr);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Why do you use CallExpression here?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Actually I thought [parser.hooks.call] passed a generic Expression and needed narrowing but I checked it turns out it's already typed as [SyncBailHook<[CallExpression]>] so the cast was just not needed and I removed it now. Thanks!

@aryanraj45
aryanraj45 force-pushed the feat/import-meta-resolve branch from ff3acc2 to d9810b3 Compare March 5, 2026 17:43
@aryanraj45
aryanraj45 force-pushed the feat/import-meta-resolve branch from d9810b3 to bb6edb8 Compare March 5, 2026 17:46
@aryanraj45

Copy link
Copy Markdown
Member Author

@alexander-akait I have the Added test cases as requested import-meta-resolve-esm covers import.meta.resolve(variable) and import.meta.resolve(unknownExpr), both with ECMA output. Static string args go through the dependency in both modes, dynamic args pass through as native import.meta.resolve for ECMA output. Also fixed the destructuring fallthrough and dropped the unnecessary cast. Please take a look, thanks!

@aryanraj45

Copy link
Copy Markdown
Member Author

@alexander-akait can u please check this again whenever u have a moment I have addressed all the changes which was been requested! Thanks

@alexander-akait

Copy link
Copy Markdown
Member

Fixed in the main, anyway thanks for the pr

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.resolve

2 participants