-
Notifications
You must be signed in to change notification settings - Fork 29.6k
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 349d054
Showing
4 changed files
with
72 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
26 changes: 16 additions & 10 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,23 @@ | ||
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))) { | ||
result.importAssertions = { | ||
...(result.importAssertions ?? context.importAssertions), | ||
type: 'json', | ||
}; | ||
} | ||
return next(url); | ||
|
||
return result; | ||
} |