Skip to content

Import assertion type: 'javascript' #7342

@GeoffreyBooth

Description

@GeoffreyBooth

Hi, I’m implementing support for import assertions in Node.js. I’m trying to make Node’s support for the feature align with the HTML spec for the module types that both Node and browsers support (JavaScript, JSON, potentially WebAssembly). I’m also trying to match some of the requirements of the HTML spec, such as including the assertion type in the module cache key, so that Node behaves similarly to browsers with regard to race conditions and module identity.

While reading the spec, I saw language such as this:

If moduleRequest.[[Assertions]] has a Record entry such that entry.[[Key]] is "type", then let module type be entry.[[Value]]. Otherwise let module type be "javascript".

And this:

If module type is not "javascript", "css", or "json", then return false.

And I assumed that this meant that the spec permitted javascript as a module type. In other words, that import './file.js' assert { type: 'javascript' } should be allowed and treated equivalently as import './file.js', and both imports would map to the same entry in the module map, because they have the same type. I was so certain of this reading of the spec that I opened tc39/proposal-import-attributes#114 to suggest upstreaming this “treat the lack of a declared type as if type: 'javascript' was specified” behavior into the import assertions proposal. I opened https://github.com/nodejs/node/discussions/40790 and an accompanying PR to propose adding this behavior to Node, and when someone there pointed out that Chrome errors on type: 'javascript' I opened https://bugs.chromium.org/p/chromium/issues/detail?id=1270635 to report what I assumed was a bug in Chrome.

But . . . am I misreading the spec? Is there some intent to disallow an explicit assert { type: 'javascript' }? @dandclark seemed to think so when he pushed https://chromium-review.googlesource.com/c/chromium/src/+/2751188 to add tests to Chromium that it should error on an explicit type: 'javascript'. I assumed that back when those tests were written perhaps the spec was different, and it’s since expanded the scope of type: 'javascript'; but am I wrong about this?

At the very least, the spec could be clearer one way or the other about whether an explicit assert { type: 'javascript' } for a JavaScript module is expected to succeed or fail. Everything I read in the spec text led me to assume that it should succeed (because why should it fail, if it’s implied by a missing type) so if it’s supposed to error, I would be very curious as to why that is and what that means for the “default” or “implied” module type.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions