Repository navigation
feat: support import.meta.resolve() for asset url resolution - #20568
aryanraj45 wants to merge 3 commits into
Conversation
|
|
This should be under the option and disabled by default |
|
Please start a discussion in the future. Firstly, things like this haven't been implemented, not because they're difficult, most packages use |
7b03253 to
703ecdf
Compare
|
@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 |
I have Updated it to put this behind [importMetaResolve] option, disabled by default, as you suggested. |
c1cebca to
0bbeb85
Compare
alexander-akait
left a comment
There was a problem hiding this comment.
All code should be in file ImportMetaPlugin, not in URLDependecy or URLParserPlugin, they are for new URL(...) construction
308d32b to
0bbeb85
Compare
@alexander-akait, I have moved all import.meta.resolve() logic to [ImportMetaPlugin] .Can u please take a look |
| parser.state.current.addDependency(dep); | ||
| return true; | ||
| }); | ||
| } |
There was a problem hiding this comment.
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
| () => ["resolve"], | ||
| true | ||
| ) | ||
| ); |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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
|
@alexander-akait updated with adding typeof and evalutetypeof hooks and case "resolve" as suggested please take a look Thanks for the suggestion |
| importMetaResolve | ||
| ? "import.meta.resolve" | ||
| : importMetaUnknownProperty(["resolve"]) | ||
| },`; |
There was a problem hiding this comment.
Only when option is enabled, it it should work, not to importMetaUnknownProperty
There was a problem hiding this comment.
Fixed. Falls through to [default]when option is disabled, no separate call needed there.
| parser.hooks.call | ||
| .for("import.meta.resolve") | ||
| .tap(PLUGIN_NAME, (_expr) => { | ||
| const expr = /** @type {CallExpression} */ (_expr); |
There was a problem hiding this comment.
Why do you use CallExpression here?
There was a problem hiding this comment.
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!
ff3acc2 to
d9810b3
Compare
…ng for import.meta.resolve
d9810b3 to
bb6edb8
Compare
|
@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! |
|
@alexander-akait can u please check this again whenever u have a moment I have addressed all the changes which was been requested! Thanks |
|
Fixed in the main, anyway thanks for the pr |
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
URLDependencyinfrastructure that already handlesnew URL("./foo", import.meta.url). When the parser seesimport.meta.resolve("./foo"), it creates aURLDependencyfor"./foo"with a newresolveUrl: trueflag. In the dependency template, that flag changes the output fromnew URL(..., __webpack_require__.b)(a URL object) tonew URL(..., __webpack_require__.b).href(a plain string), which matches whatimport.meta.resolveis supposed to return per the spec.The parser side registers two hooks: one to tell webpack that
import.meta.resolveis 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 likenew URL("./foo", import.meta.url).hreffor asset URLs.Use of AI
No, Just for editing the PR description