-
Notifications
You must be signed in to change notification settings - Fork 29.7k
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 246694f
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 function resolve(url, context, next) { | ||
// Mutation from resolve hook should be discarded. | ||
context.importAssertions.type = 'whatever'; | ||
return next(url); | ||
} | ||
export async function resolve(url, context, next) { | ||
const { importAssertions: { type } } = context; | ||
|
||
export function load(url, context, next) { | ||
if (context.importAssertions.type == null && | ||
if (type == null && | ||
(DATA_URL_PATTERN.test(url) || JSON_URL_PATTERN.test(url))) { | ||
const { importAssertions } = context; | ||
importAssertions.type = 'json'; | ||
// Clean new import assertions object to ensure that this test isn't passing due to mutation | ||
const newImportAssertions = { type: 'json' }; | ||
|
||
const result = await next(url, context); | ||
result.importAssertions = newImportAssertions; | ||
return result; | ||
} | ||
return next(url); | ||
|
||
// Mutation from resolve hook should be discarded. | ||
context.importAssertions.type = 'whatever'; | ||
|
||
return next(url, context); | ||
} |