-
Notifications
You must be signed in to change notification settings - Fork 30.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 0a01441
Showing
4 changed files
with
79 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
25 changes: 15 additions & 10 deletions
25
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,22 @@ | ||
const DATA_URL_PATTERN = /^data:application\/json(?:[^,]*?)(;base64)?,([\s\S]*)$/; | ||
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 } = context; | ||
|
||
export function load(url, context, next) { | ||
if (context.importAssertions.type == null && | ||
if (importAssertions.type == null && | ||
(DATA_URL_PATTERN.test(url) || JSON_URL_PATTERN.test(url))) { | ||
const { importAssertions } = context; | ||
importAssertions.type = 'json'; | ||
|
||
// Mutation from resolve hook should be discarded. | ||
context.importAssertions.type = 'whatever'; | ||
|
||
// 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, importAssertions: newImportAssertions }); | ||
result.importAssertions = newImportAssertions; | ||
return result; | ||
} | ||
return next(url); | ||
|
||
return next(url, context); | ||
} |