-
Notifications
You must be signed in to change notification settings - Fork 30.1k
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
- Loading branch information
1 parent
e8db11c
commit c3a2e8b
Showing
4 changed files
with
71 additions
and
44 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
26 changes: 15 additions & 11 deletions
26
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,21 @@ | ||
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); | ||
} | ||
context.importAssertions.type ??= 'whatever'; | ||
|
||
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 = { type: 'json' }; | ||
} | ||
return next(url); | ||
|
||
return result; | ||
} |