Reputation: 419
i have a custom --loader module, where i want to assign the import assertion type to json modules to maintain backwards compatibility:
export async function resolve(specifier,context,defaultResolve)
{let {importAssertions:assert}=context||{};
if(assert&&/\.json$/.test(specifier)&&assert.type!=="json")
Object.assign(assert,{type:"json"});
if(defaultResolve instanceof Function)
return defaultResolve(specifier,context,defaultResolve);
[...[]]
}
but the error seems to be thrown before entering the resolve step in case of static imports (as if during parsing the import expression), and regardless of the assignment in case of dynamic imports (i manage to log the resolve context in their case, despite the errors never trace back to the import expression):
TypeError [ERR_IMPORT_ASSERTION_TYPE_MISSING]: Module needs an import assertion of type "json"
I've read somewhere that there was planned to be a flag to disable import assertions - this way i could define my own rule to assert types even beyond the default mechanism of checking ".json" file extensions, and resort to declaration only in exceptional cases, eg. static rest api endpoints - but i don't find it. How else would I customize them? Or am I just mutating the import context the wrong way? Am i mistaken in static json imports failing before even entering the resolve step? Are declararive assertion types really such absolutely necessary? Thanks!
Upvotes: 3
Views: 773
Reputation: 419
Since i posted this, i had figured out that the context.importAssertions object is not mutable in the resolve() step, but it is in the load() step. So in that hook you can do context.importAssertions.type="json"
eg. if /\.json$/.test(specifier)
.
Upvotes: 0