content icon indicating copy to clipboard operation
content copied to clipboard

Document import assertions

Open Josh-Cena opened this issue 3 years ago • 2 comments

What is the new suggestion?

Document import assertions.

Why is it important or useful?

Already shipped in V8 for over a year. JSON modules have been unflagged in Node so a lot of people are starting to rely on it. Need more information on its syntax and semantics.

Very importantly, import assertions do not change the semantics of the module itself—it cannot affect how the module is interpreted, nor can it be used as part of the cache key. Virtually all existing toolings, except JSON modules themselves (assert { type: "json" }), that try to extend import assertions are using them in a spec-incompliant way. (TBH, that to me means the proposal is designed poorly.) We need to call that out.

We just need a new section under https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/import, but with so many additions to JavaScript modules in the future, it may make sense to start a new reference chapter like "modules", parallel to the existing "classes" and "functions" chapters...

Other supporting information

  • Spec draft: https://tc39.es/proposal-import-assertions/
  • Proposal: https://github.com/tc39/proposal-import-assertions
  • This is a sibling proposal of JSON modules, so we should probably mention that as well.
  • Only after this proposal does import() have a second parameter and therefore optional trailing comma. import("foo",) works in Chrome but is a syntax error in Safari/Firefox. We need to add this to BCD and the trailing commas page: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Trailing_commas

Josh-Cena avatar Aug 06 '22 05:08 Josh-Cena

Will this needs some mdn/browser-compat-data updates, or is it already listed there? (If so, under what key?)

teoli2003 avatar Aug 06 '22 05:08 teoli2003

I don't think there's BCD for this either, no. See https://github.com/mdn/browser-compat-data/issues/14052

Josh-Cena avatar Aug 06 '22 05:08 Josh-Cena

This feature would ideally use the with keyword to denote attributes, but there are existing implementations based on a previous version of the proposal using the assert keyword. Due to potential web compatibility risks, the proposal still includes assert marked as deprecated. Usage of the old syntax is discouraged, and its removal is being investigated.

(c) tc39

Import assertions now called import attributes and they has other syntax:

import json from "./foo.json" with { type: "json" };
import("foo.json", { with: { type: "json" } });

coderaiser avatar Jun 03 '23 13:06 coderaiser

I am indeed aware of that. I will update the linked PR once the new syntax gets shipped in browsers and Chrome reaches a decision on what to do with the assert syntax. I have also proposed a little syntax relaxation that will be asked for consensus at the next meeting.

Josh-Cena avatar Jun 03 '23 18:06 Josh-Cena

@Josh-Cena update first commit and topic name in this case, is it outdated and no longer relevant..

the new syntax gets shipped in browsers and Chrome reaches a decision on what to do with the assert syntax

I think they just remove it, since it was experimental and stage-2 only, so it was a bad idea to implement it from the beginning. Anyways I think that both variants should exist in MDN, right now we have a strange situation I don't remember before (only with TypeScript and generators, but that was simple case I think):

  • There is implemented experimental stage-2 feature;
  • Standard is changed (that is normal situation for `stage-2);

So what to do people who already used it? I think they should process their code with a codemod and forget about deprecated syntax.

But we will see what v8 team will reach to.

coderaiser avatar Jun 04 '23 17:06 coderaiser

Instead of letting this PR sit around forever, I'm going to close this and open a new issue when the new syntax and semantics get shipped in browsers. There will be significant changes from the currently documented behavior.

Josh-Cena avatar Jun 17 '23 11:06 Josh-Cena