-
Notifications
You must be signed in to change notification settings - Fork 29.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
esm: allow resolve to return import assertions
PR-URL: #46153 Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com> Reviewed-By: Michaël Zasso <targos@protonmail.com> Reviewed-By: Jacob Smith <jacob@frende.me>
- Loading branch information
1 parent
5eb5be8
commit 18f0398
Showing
4 changed files
with
71 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
27 changes: 17 additions & 10 deletions
27
test/fixtures/es-module-loaders/assertionless-json-import.mjs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,24 @@ | ||
const DATA_URL_PATTERN = /^data:application\/json(?:[^,]*?)(;base64)?,([\s\S]*)$/; | ||
const JSON_URL_PATTERN = /\.json(\?[^#]*)?(#.*)?$/; | ||
const JSON_URL_PATTERN = /^[^?]+\.json(\?[^#]*)?(#.*)?$/; | ||
|
||
export async function resolve(specifier, context, next) { | ||
const noAssertionSpecified = context.importAssertions.type == null; | ||
|
||
export function resolve(url, context, next) { | ||
// Mutation from resolve hook should be discarded. | ||
context.importAssertions.type = 'whatever'; | ||
return next(url); | ||
} | ||
|
||
export function load(url, context, next) { | ||
if (context.importAssertions.type == null && | ||
(DATA_URL_PATTERN.test(url) || JSON_URL_PATTERN.test(url))) { | ||
const { importAssertions } = context; | ||
importAssertions.type = 'json'; | ||
// This fixture assumes that no other resolve hooks in the chain will error on invalid import assertions | ||
// (as defaultResolve doesn't). | ||
const result = await next(specifier, context); | ||
|
||
if (noAssertionSpecified && | ||
(DATA_URL_PATTERN.test(result.url) || JSON_URL_PATTERN.test(result.url))) { | ||
// Clean new import assertions object to ensure that this test isn't passing due to mutation. | ||
result.importAssertions = { | ||
...(result.importAssertions ?? context.importAssertions), | ||
type: 'json', | ||
}; | ||
} | ||
return next(url); | ||
|
||
return result; | ||
} |