Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reland JSON module scripts #5658

Merged
merged 71 commits into from
Jul 29, 2021
Merged
Changes from 67 commits
Commits
Show all changes
71 commits
Select commit Hold shift + click to select a range
914e7d5
Reland JSON modules
dandclark Jun 17, 2020
b058240
Add import attribute type checks and initial plumbing.
dandclark Jun 18, 2020
d0137f5
Plumb through ModuleRequest instead of string for 'fetch an import() …
dandclark Jun 18, 2020
b2e7065
Don't cache a null entry in the module map in the case where the MIME…
dandclark Jun 18, 2020
0cbf597
Allow modulepreload to work for all module types without needing to s…
dandclark Jun 19, 2020
6932074
After rebase, fix build break from name change to JSON.Parse in https…
dandclark Jun 19, 2020
1f83c7e
Fix typo in JSON modules example and add note on requiring JSON MIME …
dandclark Jun 22, 2020
a3621dd
Update ModuleRequest definition to reference the import-attributes pr…
dandclark Jun 22, 2020
55ebd12
Revert added null check in 'fetch a modulepreload module script graph…
dandclark Jun 22, 2020
af41b28
Reference ParseJSONModule from import-attributes proposal instead of …
dandclark Jun 22, 2020
a2d4260
Cache an entry in the module map if the fetched module was valid but …
dandclark Jun 23, 2020
f79c01f
Extend note to be clear that the module must have no effect until a r…
dandclark Jun 23, 2020
7c900a1
When a valid MIME type is received that doesn't match the requested t…
dandclark Jun 25, 2020
55c9ed9
Wrap <p class=note> in <li> so it's not directly under an <ol>, which…
dandclark Jun 26, 2020
322808d
Whitespace change: remove double spaces after sentences in notes. Ho…
dandclark Jun 26, 2020
43e6fe3
Merge branch 'upstream-master' into dandclark/import-attributes
dandclark Jun 30, 2020
9f2ae27
Reword invocations of 'create a module script with matching type' to …
dandclark Jun 30, 2020
e86dd65
Change 'fetch a single module script' to fail if there are any unrec…
dandclark Jun 30, 2020
3318ed3
Instead of passing fetch response parameter to 'create a module scrip…
dandclark Jul 7, 2020
3c0dce2
Add assert that 'create a module script with matching type' is not ru…
dandclark Jul 7, 2020
1c9df5a
Reword caching comment
dandclark Jul 8, 2020
3247e99
Handle rename in TC39 proposal from import attributes to import condi…
dandclark Jul 9, 2020
dca376b
Update for the latest rename of the proposal from 'Import Conditions'…
dandclark Jul 24, 2020
e3359b5
Use module type as part of module map cache key instead of caching re…
dandclark Jul 27, 2020
0857ec8
Merge remote-tracking branch 'upstream/master' into dandclark/import-…
dandclark Aug 27, 2020
aafb256
Change 'abort these steps' to 'return' to match recent changes in 'fe…
dandclark Aug 27, 2020
9164acf
Remove JSON modules references, switch JavaScript module script refer…
dandclark Aug 28, 2020
9eda0e2
Properly return non-null module map entry if one already exists.
dandclark Aug 31, 2020
9fbbc6d
Update spec links to reflect split of import assertions and json modules
dandclark Aug 31, 2020
a8cb4a9
Merge remote-tracking branch 'upstream/master' into dandclark/import-…
dandclark Sep 30, 2020
f53c203
Merge remote-tracking branch 'upstream/master' into dandclark/import-…
dandclark Sep 30, 2020
1117dc3
Update module map dfn note to explain purpose of having module type i…
dandclark Sep 30, 2020
73f2be2
Update module map dfn note to explain purpose of having module type i…
dandclark Sep 30, 2020
eec60c5
Ignore unknown import assertions instead of failing. Continue failing…
dandclark Sep 30, 2020
6ed4474
Ignore unknown import assertions instead of failing. Continue failin…
dandclark Sep 30, 2020
c225d56
Move check for unknown type assertion to 'create a module script'.
dandclark Oct 5, 2020
2448342
Move check for unknown type assertion to 'create a module script'.
dandclark Oct 5, 2020
6a160fe
Merge remote-tracking branch 'upstream/master' into dandclark/import-…
dandclark Nov 11, 2020
1566bc0
Merge remote-tracking branch 'upstream/master' into dandclark/import-…
dandclark Nov 12, 2020
3c94b31
Review feedback: use tuple instead of pair, improve module map defini…
dandclark Nov 12, 2020
9da92b5
Review feedback: Link to memory cache issue, null --> 'javascript' ty…
dandclark Nov 13, 2020
26c2b81
Remove duplicated words
dandclark Nov 13, 2020
4ad7d2d
Move quotes outside of <code> elements.
dandclark Nov 16, 2020
614a9f4
Add HostGetSupportedAssertions integration
dandclark Nov 30, 2020
2fc3616
Fix indentation/spacing. Use short list notation.
dandclark Dec 1, 2020
697a6f1
Merge remote-tracking branch 'upstream/master' into dandclark/import-…
dandclark Dec 11, 2020
a9eed9b
Use two separate scripts in example so that at least one will actuall…
dandclark Dec 29, 2020
973ec71
Add module type to visited set.
dandclark Dec 29, 2020
337124e
Merge remote-tracking branch 'upstream/master' into dandclark/import-…
dandclark Dec 30, 2020
cf01c6d
Use two separate scripts in example so that at least one will actuall…
dandclark Dec 29, 2020
d954ad7
Add module type to visited set.
dandclark Dec 29, 2020
5bc28f6
Add HostGetSupportedAssertions integration
dandclark Nov 30, 2020
1b4da46
Fix indentation/spacing. Use short list notation.
dandclark Dec 1, 2020
eb8d82d
Merge branch 'dandclark/import-attributes-no-json' into dandclark/imp…
dandclark Dec 30, 2020
45f6c6e
Fix up some issues from merge
dandclark Dec 31, 2020
87eec91
Update visited set changes for json module type
dandclark Dec 31, 2020
fdc08f8
Clarify that memory cache mitigating violation of import assertions s…
dandclark Dec 31, 2020
a9f4d00
Merge branch 'dandclark/import-attributes-no-json' into dandclark/imp…
dandclark Dec 31, 2020
30e27fb
Merge remote-tracking branch 'upstream/main' into dandclark/import-at…
dandclark Mar 10, 2021
9ec7c7e
Add class='XXX' to note about the memory cache
dandclark Mar 11, 2021
9690560
Merge branch 'dandclark/import-attributes-no-json' into dandclark/imp…
dandclark Jun 24, 2021
f5f9ec1
Merge branch 'main' into dandclark/import-attributes-no-json
dandclark Jun 24, 2021
be37963
Merge branch 'dandclark/import-attributes-no-json' into dandclark/imp…
dandclark Jun 24, 2021
ab21bd5
Merge branch 'main' into dandclark/import-attributes
dandclark Jul 26, 2021
aa104e0
Fix spacing
dandclark Jul 26, 2021
64c523d
Delete duplicated example
dandclark Jul 26, 2021
54498ba
Restore updated comments about CSS/JSON module script definition
dandclark Jul 27, 2021
9de359a
Combine some paragraphs
dandclark Jul 27, 2021
d4f5bd0
Add missing closing tags
dandclark Jul 27, 2021
43526c4
Use CreateDefaultExportSyntheticModule for CSS module scripts
dandclark Jul 27, 2021
10af46b
Use not/or instead of neither/nor
dandclark Jul 27, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
122 changes: 101 additions & 21 deletions source
Original file line number Diff line number Diff line change
Expand Up @@ -2891,13 +2891,15 @@ a.setAttribute('href', 'https://example.com/'); // change the content attribute
<li>The <dfn data-x="js-HostGetSupportedImportAssertions" data-x-href="https://tc39.es/proposal-import-assertions/#sec-hostgetsupportedimportassertions">HostGetSupportedImportAssertions</dfn> abstract operation</li>
</ul>

<p>The following terms are defined in the <cite>JSON modules</cite> proposal and used in this
specification: <ref spec=JSJSONMODULES></p>
<p>User agents that support JavaScript must also implement the <cite>JSON modules</cite>
proposal. The following terms are defined there, and used in this specification: <ref
spec=JSJSONMODULES></p>

<ul class="brief">
<li><dfn data-x-href="https://tc39.es/proposal-json-modules/#sec-createsyntheticmodule">CreateSyntheticModule</dfn></li>
<li><dfn data-x-href="https://tc39.es/proposal-json-modules/#sec-setsyntheticmoduleexport">SetSyntheticModuleExport</dfn></li>
<li><dfn data-x-href="https://tc39.es/proposal-json-modules/#sec-synthetic-module-records">Synthetic Module Record</dfn></li>
<li>The <dfn data-x-href="https://tc39.es/proposal-json-modules/#sec-createsyntheticmodule">CreateSyntheticModule</dfn> abstract operation</li>
<li>The <dfn data-x-href="https://tc39.es/proposal-json-modules/#sec-setsyntheticmoduleexport">SetSyntheticModuleExport</dfn> abstract operation</li>
<li>The <dfn data-x-href="https://tc39.es/proposal-json-modules/#sec-synthetic-module-records">Synthetic Module Record</dfn> specification type</li>
<li>The <dfn data-x-href="https://tc39.es/proposal-json-modules/#sec-parse-json-module">ParseJSONModule</dfn> abstract operation</li>
</ul>
</dd>

Expand Down Expand Up @@ -58472,6 +58474,9 @@ interface <dfn interface>HTMLScriptElement</dfn> : <span>HTMLElement</span> {
<p>The contents of the external script resource for <span data-x="CSS module script">CSS module
scripts</span> must conform to the requirements of the CSS specification. <ref spec=CSS></p>

<p>The contents of the external script resource for <span data-x="JSON module script">JSON module
scripts</span> must conform to the requirements of the JSON specification <ref spec=JSON>.</p>

<p>When used to include <span data-x="data block">data blocks</span>, the data must be embedded
inline, the format of the data must be given using the <code data-x="attr-script-type">type</code>
attribute, and the contents of the <code>script</code> element must conform to the requirements
Expand Down Expand Up @@ -58791,7 +58796,29 @@ o............A....e
where this <code>script</code> element appears in the document, it will not be evaluated until
both document parsing has complete and its dependency (<code data-x="">dom-utils.mjs</code>) has
been fetched and evaluated.</p>
</div>

<div class="example" id="json-module-script-example">
<p>The following sample shows how a <span>JSON module script</span> can be imported from inside
a <span>JavaScript module script</span>:</p>

<pre><code class="html" data-x="">&lt;script type="module">
import peopleInSpace from "http://api.open-notify.org/astros.json" assert { type: "json" };

const list = document.querySelector("#people-in-space");
for (const { craft, name } of peopleInSpace.people) {
const li = document.createElement("li");
li.textContent = `${name} / ${craft}`;
list.append(li);
}
&lt;/script></code></pre>

<p>MIME type checking for module scripts is strict. In order for the fetch of the <span>JSON
module script</span> to succeed, the HTTP reponse must have a <span>JSON MIME type</span>, for
example <code data-x="">Content-Type: text/json</code>. On the other hand, if the <code
data-x="">assert { type: "json" }</code> part of the statement is omitted, it is assumed that the
intent is to import a <span>JavaScript module script</span>, and the fetch will fail if the HTTP
response has a MIME type that is not a <span>JavaScript MIME type</span>.</p>
</div>

<div w-nodev>
Expand Down Expand Up @@ -90366,6 +90393,9 @@ document.querySelector("button").addEventListener("click", bound);
<li><p>a <span>Synthetic Module Record</span>, for <span data-x="CSS module script">CSS
module scripts</span>; or</p></li>

<li><p>a <span>Synthetic Module Record</span>, for <span data-x="JSON module script">JSON
module scripts</span>; or</p></li>
dandclark marked this conversation as resolved.
Show resolved Hide resolved

<li><p>null, representing a parsing failure.</p></li>
</ul>
</dd>
Expand Down Expand Up @@ -90430,7 +90460,7 @@ document.querySelector("button").addEventListener("click", bound);
data-x="concept-script">script</span>. It has no additional <span data-x="struct
item">items</span>.</p>

<p><span data-x="module script">Module scripts</span> can be classified into two types:</p>
<p><span data-x="module script">Module scripts</span> can be classified into three types:</p>

<ul>
<li><p>A <span>module script</span> is a <dfn data-export="">JavaScript module script</dfn> if
Expand All @@ -90442,21 +90472,35 @@ document.querySelector("button").addEventListener("click", bound);
data-x="concept-script-record">record</span> is a <span>Synthetic Module Record</span>, and it
was created via the <span data-x="creating a CSS module script">create a CSS module
script</span> algorithm. CSS module scripts represent a parsed CSS stylesheet.</p>
<!--
This definition is not super-rigorous, but it doesn't need to be for now. Technically, the
"it was created via..." requirement is redundant, since there are no other Synthetic Module
Record users.

If we ever: (a) get more Synthetic Module Record Users; or (b) start testing if something is a
CSS module script in algorithms, instead of just referring to the concept, then we should
consider adding a type item to the module script struct.
<!--
This definition is not super-rigorous, but it doesn't need to be for now. If we ever start
testing if something is a CSS module script in algorithms, instead of just referring to the
concept, then we should consider adding a type item to the module script struct.
-->

<p class="note">As CSS stylesheets do not import dependent modules, and do not throw exceptions
on evaluation, the <span data-x="concept-script-script-fetch-options">fetch options</span> and
<span data-x="concept-script-base-url">base URL</span> of a <span>CSS module script</span> are
always null.</p>
</li>

<li>
<p>A <span>module script</span> is a <dfn data-export="">JSON module script</dfn> if its <span
data-x="concept-script-record">record</span> is a <span>Synthetic Module Record</span>, and it
was created via the <span data-x="creating a JSON module script">create a JSON module
script</span> algorithm. JSON module scripts represent a parsed JSON document.</p>

<!--
This definition is not super-rigorous, but it doesn't need to be for now. If we ever start
testing if something is a JSON module script in algorithms, instead of just referring to the
concept, then we should consider adding a type item to the module script struct.
-->

<p class="note">As JSON documents do not import dependent modules, and do not throw exceptions
on evaluation, the <span data-x="concept-script-script-fetch-options">fetch options</span> and
<span data-x="concept-script-base-url">base URL</span> of a <span>JSON module script</span> are
always null.</p>
dandclark marked this conversation as resolved.
Show resolved Hide resolved
</ul>

<p>The <dfn>active script</dfn> is determined by the following algorithm:</p>
Expand Down Expand Up @@ -90857,8 +90901,9 @@ document.querySelector("button").addEventListener("click", bound);
type</var> be <var>entry</var>.[[Value]]. Otherwise let <var>module type</var> be "<code
data-x="">javascript</code>".</p></li>

<li><p>If <var>module type</var> is neither "<code data-x="">javascript</code>" nor "<code
data-x="">css</code>", then asynchronously complete this algorithm with null, and return.</p></li>
<li><p>If <var>module type</var> is neither "<code data-x="">javascript</code>", "<code
data-x="">css</code>", nor "<code data-x="">json</code>", then asynchronously complete this
domenic marked this conversation as resolved.
Show resolved Hide resolved
algorithm with null, and return.</p></li>

<li><p><span>Fetch a single module script</span> given <var>url</var>, <var>settings
object</var>, "<code data-x="">script</code>", <var>options</var>, <var>settings object</var>,
Expand Down Expand Up @@ -91335,11 +91380,11 @@ document.querySelector("button").addEventListener("click", bound);
</ol>
</li>

<li><p>Assert: <var>module type</var> is either "<code data-x="">javascript</code>" or "<code
data-x="">css</code>". Otherwise we would not have reached this point because a failure would
have been raised when inspecting <var>moduleRequest</var>.[[Assertions]] in <a
href="#validate-requested-module-specifiers">create a JavaScript module script</a> or <span>fetch
an import() module script graph</span>.</p></li>
<li><p>Assert: <var>module type</var> is either "<code data-x="">javascript</code>", "<code
data-x="">css</code>", or "<code data-x="">json</code>". Otherwise we would not have reached this
point because a failure would have been raised when inspecting
<var>moduleRequest</var>.[[Assertions]] in <a href="#validate-requested-module-specifiers">create
a JavaScript module script</a> or <span>fetch an import() module script graph</span>.</p></li>

<li><p>Let <var>moduleMap</var> be <var>module map settings object</var>'s <span
data-x="concept-settings-object-module-map">module map</span>.</p></li>
Expand Down Expand Up @@ -91424,6 +91469,11 @@ document.querySelector("button").addEventListener("click", bound);
result of <span>creating a CSS module script</span> given <var>source text</var> and <var>module
map settings object</var>.</p></li>

<li><p>If <var>MIME type essence</var> is a <span>JSON MIME type</span> and <var>module
type</var> is "<code data-x="">json</code>", then set <var>module script</var> to the result of
<span>creating a JSON module script</span> given <var>source text</var> and <var>module map
settings object</var>.</p></li>

<li>
<p><span data-x="map set">Set</span> <var>moduleMap</var>[(<var>url</var>, <var>module
type</var>)] to <var>module script</var>, and asynchronously complete this algorithm with
Expand Down Expand Up @@ -91627,7 +91677,8 @@ document.querySelector("button").addEventListener("click", bound);

<li>
<p>If <var>url</var> is failure, or if <var>module type</var> is neither "<code
data-x="">javascript</code>" nor "<code data-x="">css</code>", then:</p>
data-x="">javascript</code>", "<code data-x="">css</code>", nor "<code data-x="">json</code>",
domenic marked this conversation as resolved.
Show resolved Hide resolved
then:</p>
<ol>
<li><p>Let <var>error</var> be a new <code>TypeError</code> exception.</p></li>

Expand Down Expand Up @@ -91712,6 +91763,35 @@ document.querySelector("button").addEventListener("click", bound);
</li>
</ol>
dandclark marked this conversation as resolved.
Show resolved Hide resolved

<p>To <dfn data-x="creating a JSON module script">create a JSON module script</dfn>, given a
string <var>source</var> and an <span>environment settings object</span> <var>settings</var>:</p>

<ol>
<li><p>Let <var>script</var> be a new <span>module script</span> that this algorithm will
subsequently initialize.</p></li>

<li><p>Set <var>script</var>'s <span>settings object</span> to <var>settings</var>.</p></li>

<li><p>Set <var>script</var>'s <span data-x="concept-script-base-url">base URL</span> and
<span data-x="concept-script-script-fetch-options">fetch options</span> to null.</p></li>

<li><p>Set <var>script</var>'s <span data-x="concept-script-parse-error">parse error</span> and
<span data-x="concept-script-error-to-rethrow">error to rethrow</span> to null.</p></li>

<li>
<p>Let <var>result</var> be <span>ParseJSONModule</span>(<var>source</var>).
dandclark marked this conversation as resolved.
Show resolved Hide resolved

<p>If this throws an exception, set <var>script</var>'s <span
data-x="concept-script-parse-error">parse error</span> to that exception, and return
<var>script</var>.</p>
</li>

<li><p>Set <var>script</var>'s <span data-x="concept-script-record">record</span> to
<var>result</var>.
dandclark marked this conversation as resolved.
Show resolved Hide resolved

<li><p>Return <var>script</var>.</p></li>
</ol>

<h5 id="calling-scripts">Calling scripts</h5>

<p>To <dfn export>run a classic script</dfn> given a <span>classic script</span> <var>script</var>
Expand Down