├── .editorconfig
├── .gitattributes
├── .github
└── workflows
│ └── build.yml
├── .gitignore
├── .npmrc
├── .nvmrc
├── LICENSE
├── README.md
├── content-type-vs-file-extension.md
├── index.html
├── package.json
├── spec.emu
└── tag-security-and-privacy.md
/.editorconfig:
--------------------------------------------------------------------------------
1 | root = true
2 |
3 | [*]
4 | charset = utf-8
5 | indent_style = tab
6 | end_of_line = lf
7 | insert_final_newline = true
8 | trim_trailing_whitespace = true
9 |
10 | [{README.md,package.json,spec.html,.travis.yml}]
11 | indent_style = space
12 | indent_size = 2
13 |
--------------------------------------------------------------------------------
/.gitattributes:
--------------------------------------------------------------------------------
1 | # Automatically normalize line endings for all text-based files
2 | * text=auto
3 |
4 | index.html -diff merge=ours
5 | spec.js -diff merge=ours
6 | spec.css -diff merge=ours
7 |
--------------------------------------------------------------------------------
/.github/workflows/build.yml:
--------------------------------------------------------------------------------
1 | name: Deploy spec
2 |
3 | on: [push]
4 |
5 | jobs:
6 | build:
7 | runs-on: ubuntu-latest
8 |
9 | steps:
10 | - uses: actions/checkout@v2
11 | - uses: actions/setup-node@v1
12 | with:
13 | node-version: '12.x'
14 | - run: npm install
15 | - run: npm run build
16 | - name: commit changes
17 | uses: elstudio/actions-js-build/commit@v3
18 | with:
19 | commitMessage: "fixup: [spec] `npm run build`"
20 |
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | dist
2 |
3 | # Installed npm modules
4 | package-lock.json
5 | node_modules
6 |
7 | # Folder view configuration files
8 | .DS_Store
9 | Desktop.ini
10 |
11 | # Thumbnail cache files
12 | ._*
13 | Thumbs.db
14 |
15 | # Files that might appear on external disks
16 | .Spotlight-V100
17 | .Trashes
18 |
--------------------------------------------------------------------------------
/.npmrc:
--------------------------------------------------------------------------------
1 | package-lock=false
2 |
--------------------------------------------------------------------------------
/.nvmrc:
--------------------------------------------------------------------------------
1 | 12
2 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | Apache License
2 | Version 2.0, January 2004
3 | http://www.apache.org/licenses/
4 |
5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6 |
7 | 1. Definitions.
8 |
9 | "License" shall mean the terms and conditions for use, reproduction,
10 | and distribution as defined by Sections 1 through 9 of this document.
11 |
12 | "Licensor" shall mean the copyright owner or entity authorized by
13 | the copyright owner that is granting the License.
14 |
15 | "Legal Entity" shall mean the union of the acting entity and all
16 | other entities that control, are controlled by, or are under common
17 | control with that entity. For the purposes of this definition,
18 | "control" means (i) the power, direct or indirect, to cause the
19 | direction or management of such entity, whether by contract or
20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the
21 | outstanding shares, or (iii) beneficial ownership of such entity.
22 |
23 | "You" (or "Your") shall mean an individual or Legal Entity
24 | exercising permissions granted by this License.
25 |
26 | "Source" form shall mean the preferred form for making modifications,
27 | including but not limited to software source code, documentation
28 | source, and configuration files.
29 |
30 | "Object" form shall mean any form resulting from mechanical
31 | transformation or translation of a Source form, including but
32 | not limited to compiled object code, generated documentation,
33 | and conversions to other media types.
34 |
35 | "Work" shall mean the work of authorship, whether in Source or
36 | Object form, made available under the License, as indicated by a
37 | copyright notice that is included in or attached to the work
38 | (an example is provided in the Appendix below).
39 |
40 | "Derivative Works" shall mean any work, whether in Source or Object
41 | form, that is based on (or derived from) the Work and for which the
42 | editorial revisions, annotations, elaborations, or other modifications
43 | represent, as a whole, an original work of authorship. For the purposes
44 | of this License, Derivative Works shall not include works that remain
45 | separable from, or merely link (or bind by name) to the interfaces of,
46 | the Work and Derivative Works thereof.
47 |
48 | "Contribution" shall mean any work of authorship, including
49 | the original version of the Work and any modifications or additions
50 | to that Work or Derivative Works thereof, that is intentionally
51 | submitted to Licensor for inclusion in the Work by the copyright owner
52 | or by an individual or Legal Entity authorized to submit on behalf of
53 | the copyright owner. For the purposes of this definition, "submitted"
54 | means any form of electronic, verbal, or written communication sent
55 | to the Licensor or its representatives, including but not limited to
56 | communication on electronic mailing lists, source code control systems,
57 | and issue tracking systems that are managed by, or on behalf of, the
58 | Licensor for the purpose of discussing and improving the Work, but
59 | excluding communication that is conspicuously marked or otherwise
60 | designated in writing by the copyright owner as "Not a Contribution."
61 |
62 | "Contributor" shall mean Licensor and any individual or Legal Entity
63 | on behalf of whom a Contribution has been received by Licensor and
64 | subsequently incorporated within the Work.
65 |
66 | 2. Grant of Copyright License. Subject to the terms and conditions of
67 | this License, each Contributor hereby grants to You a perpetual,
68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable
69 | copyright license to reproduce, prepare Derivative Works of,
70 | publicly display, publicly perform, sublicense, and distribute the
71 | Work and such Derivative Works in Source or Object form.
72 |
73 | 3. Grant of Patent License. Subject to the terms and conditions of
74 | this License, each Contributor hereby grants to You a perpetual,
75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable
76 | (except as stated in this section) patent license to make, have made,
77 | use, offer to sell, sell, import, and otherwise transfer the Work,
78 | where such license applies only to those patent claims licensable
79 | by such Contributor that are necessarily infringed by their
80 | Contribution(s) alone or by combination of their Contribution(s)
81 | with the Work to which such Contribution(s) was submitted. If You
82 | institute patent litigation against any entity (including a
83 | cross-claim or counterclaim in a lawsuit) alleging that the Work
84 | or a Contribution incorporated within the Work constitutes direct
85 | or contributory patent infringement, then any patent licenses
86 | granted to You under this License for that Work shall terminate
87 | as of the date such litigation is filed.
88 |
89 | 4. Redistribution. You may reproduce and distribute copies of the
90 | Work or Derivative Works thereof in any medium, with or without
91 | modifications, and in Source or Object form, provided that You
92 | meet the following conditions:
93 |
94 | (a) You must give any other recipients of the Work or
95 | Derivative Works a copy of this License; and
96 |
97 | (b) You must cause any modified files to carry prominent notices
98 | stating that You changed the files; and
99 |
100 | (c) You must retain, in the Source form of any Derivative Works
101 | that You distribute, all copyright, patent, trademark, and
102 | attribution notices from the Source form of the Work,
103 | excluding those notices that do not pertain to any part of
104 | the Derivative Works; and
105 |
106 | (d) If the Work includes a "NOTICE" text file as part of its
107 | distribution, then any Derivative Works that You distribute must
108 | include a readable copy of the attribution notices contained
109 | within such NOTICE file, excluding those notices that do not
110 | pertain to any part of the Derivative Works, in at least one
111 | of the following places: within a NOTICE text file distributed
112 | as part of the Derivative Works; within the Source form or
113 | documentation, if provided along with the Derivative Works; or,
114 | within a display generated by the Derivative Works, if and
115 | wherever such third-party notices normally appear. The contents
116 | of the NOTICE file are for informational purposes only and
117 | do not modify the License. You may add Your own attribution
118 | notices within Derivative Works that You distribute, alongside
119 | or as an addendum to the NOTICE text from the Work, provided
120 | that such additional attribution notices cannot be construed
121 | as modifying the License.
122 |
123 | You may add Your own copyright statement to Your modifications and
124 | may provide additional or different license terms and conditions
125 | for use, reproduction, or distribution of Your modifications, or
126 | for any such Derivative Works as a whole, provided Your use,
127 | reproduction, and distribution of the Work otherwise complies with
128 | the conditions stated in this License.
129 |
130 | 5. Submission of Contributions. Unless You explicitly state otherwise,
131 | any Contribution intentionally submitted for inclusion in the Work
132 | by You to the Licensor shall be under the terms and conditions of
133 | this License, without any additional terms or conditions.
134 | Notwithstanding the above, nothing herein shall supersede or modify
135 | the terms of any separate license agreement you may have executed
136 | with Licensor regarding such Contributions.
137 |
138 | 6. Trademarks. This License does not grant permission to use the trade
139 | names, trademarks, service marks, or product names of the Licensor,
140 | except as required for reasonable and customary use in describing the
141 | origin of the Work and reproducing the content of the NOTICE file.
142 |
143 | 7. Disclaimer of Warranty. Unless required by applicable law or
144 | agreed to in writing, Licensor provides the Work (and each
145 | Contributor provides its Contributions) on an "AS IS" BASIS,
146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
147 | implied, including, without limitation, any warranties or conditions
148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
149 | PARTICULAR PURPOSE. You are solely responsible for determining the
150 | appropriateness of using or redistributing the Work and assume any
151 | risks associated with Your exercise of permissions under this License.
152 |
153 | 8. Limitation of Liability. In no event and under no legal theory,
154 | whether in tort (including negligence), contract, or otherwise,
155 | unless required by applicable law (such as deliberate and grossly
156 | negligent acts) or agreed to in writing, shall any Contributor be
157 | liable to You for damages, including any direct, indirect, special,
158 | incidental, or consequential damages of any character arising as a
159 | result of this License or out of the use or inability to use the
160 | Work (including but not limited to damages for loss of goodwill,
161 | work stoppage, computer failure or malfunction, or any and all
162 | other commercial damages or losses), even if such Contributor
163 | has been advised of the possibility of such damages.
164 |
165 | 9. Accepting Warranty or Additional Liability. While redistributing
166 | the Work or Derivative Works thereof, You may choose to offer,
167 | and charge a fee for, acceptance of support, warranty, indemnity,
168 | or other liability obligations and/or rights consistent with this
169 | License. However, in accepting such obligations, You may act only
170 | on Your own behalf and on Your sole responsibility, not on behalf
171 | of any other Contributor, and only if You agree to indemnify,
172 | defend, and hold each Contributor harmless for any liability
173 | incurred by, or claims asserted against, such Contributor by reason
174 | of your accepting any such warranty or additional liability.
175 |
176 | END OF TERMS AND CONDITIONS
177 |
178 | APPENDIX: How to apply the Apache License to your work.
179 |
180 | To apply the Apache License to your work, attach the following
181 | boilerplate notice, with the fields enclosed by brackets "{}"
182 | replaced with your own identifying information. (Don't include
183 | the brackets!) The text should be enclosed in the appropriate
184 | comment syntax for the file format. We also recommend that a
185 | file or class name and description of purpose be included on the
186 | same "printed page" as the copyright notice for easier
187 | identification within third-party archives.
188 |
189 | Copyright {yyyy} {name of copyright owner}
190 |
191 | Licensed under the Apache License, Version 2.0 (the "License");
192 | you may not use this file except in compliance with the License.
193 | You may obtain a copy of the License at
194 |
195 | http://www.apache.org/licenses/LICENSE-2.0
196 |
197 | Unless required by applicable law or agreed to in writing, software
198 | distributed under the License is distributed on an "AS IS" BASIS,
199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
200 | See the License for the specific language governing permissions and
201 | limitations under the License.
202 |
203 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # Import Attributes
2 |
3 | Champions: Sven Sauleau ([@xtuc](https://github.com/xtuc)), Daniel Ehrenberg ([@littledan](https://github.com/littledan)), Myles Borins ([@MylesBorins](https://github.com/MylesBorins)), Dan Clark ([@dandclark](https://github.com/dandclark)), and Nicolò Ribaudo ([@nicolo-ribaudo](https://github.com/nicolo-ribaudo)).
4 |
5 | Status: Stage 4
6 |
7 | > ⚠️ The specification in this proposal might be out of date. [tc39/ecma262#3057](https://github.com/tc39/ecma262/pull/3057) is the latest version.
8 | >
9 | > Some of the changes present in the current specification have not been presented to committee yet: [#142](https://github.com/tc39/proposal-import-attributes/issues/142)
10 |
11 | Please leave any feedback you have in the [issues](http://github.com/tc39/proposal-import-attributes/issues)!
12 |
13 | ## Synopsis
14 |
15 | The Import Attributes proposal, formerly known as Import Assertions, adds an inline syntax for module import statements to pass on more information alongside the module specifier. The initial application for such attributes will be to support additional types of modules in a common way across JavaScript environments, starting with [JSON modules](http://github.com/tc39/proposal-json-modules).
16 |
17 | The syntax will be as follows (shown here is the proposed method for importing a JSON module):
18 | ```js
19 | import json from "./foo.json" with { type: "json" };
20 | import("foo.json", { with: { type: "json" } });
21 | ```
22 |
23 | The specification of JSON modules was originally part of this proposal, but it was [resolved](https://github.com/tc39/notes/blob/master/meetings/2020-07/july-21.md#import-conditions-for-stage-3) during the July 2020 meeting to split JSON modules out into a [separate Stage 3 proposal](http://github.com/tc39/proposal-json-modules).
24 |
25 | ## Motivation
26 |
27 | Standards-track JSON ES modules were [proposed](https://github.com/w3c/webcomponents/issues/770) to allow JavaScript modules to easily import JSON data files, similarly to how they are supported in many nonstandard JavaScript module systems. This idea quickly got broad support from web developers and browsers, and was merged into HTML, with an implementation for V8/Chromium created by Microsoft.
28 |
29 | However, in [an issue](https://github.com/w3c/webcomponents/issues/839), Ryosuke Niwa (Apple) and Anne van Kesteren (Mozilla) proposed that security would be improved if some syntactic marker were required when importing JSON modules and similar module types which cannot execute code, to prevent a scenario where the responding server unexpectedly provides a different MIME type, causing code to be unexpectedly executed. The solution was to somehow indicate that a module was JSON, or in general, not to be executed, somewhere in addition to the MIME type.
30 |
31 | Some developers have the intuition that the file extension could be used to determine the module type, as it is in many existing non-standard module systems. However, it's a deep web architectural principle that the suffix of the URL (which you might think of as the "file extension" outside of the web) does not lead to semantics of how the page is interpreted. In practice, on the web, there is a widespread [mismatch between file extension and the HTTP Content Type header](content-type-vs-file-extension.md). All of this sums up to it being infeasible to depend on file extensions/suffixes included in the module specifier to be the basis for this checking.
32 |
33 | There are other possible pieces of metadata which could be associated with modules, see [#8](https://github.com/tc39/proposal-import-attributes/issues/8) and [tc39/proposal-import-reflection#18](https://github.com/tc39/proposal-import-reflection/issues/18) for further discussion.
34 |
35 | Proposed ES module types that are blocked by this security concern, in addition to JSON modules, include [CSS modules](https://github.com/whatwg/html/pull/4898) and potentially [HTML modules](https://github.com/whatwg/html/pull/4505) if the HTML module proposal is restricted to [not allow script](https://github.com/w3c/webcomponents/issues/805).
36 |
37 | ## Rationale
38 |
39 | There are three places where this data could be provided:
40 | - As part of the module specifier (e.g., as a pseudo-scheme)
41 | - Challenges: Adds complexity to URLs or other module specifier syntaxes, and risks being confusing to developers (further discussion: [#11](https://github.com/tc39/proposal-import-attributes/issues/11))
42 | - webpack supports this sort of construct ([docs](https://webpack.js.org/concepts/loaders/#inline)).
43 | - Demand from users for similar behavior in Parcel, with pushback from some maintainers ([#3477](https://github.com/parcel-bundler/parcel/issues/3477))
44 | - Separately, out of band (e.g., a separate resource file)
45 | - Challenges: How to load that resource file; what should the format be; unergonomic to have to jump between files during development (further discussion: [#13](https://github.com/tc39/proposal-import-attributes/issues/13))
46 | - In the JavaScript source text
47 | - Challenges: Requires a change at the JavaScript language level (this proposal)
48 |
49 | This proposal pursues the third option, as we expect it to lead to the best developer experience, and are hopeful that language design/standardization issues can be resolved.
50 |
51 | ## Proposed syntax
52 |
53 | Import attributes have to be made available in several different contexts. They use a key-value syntax is used preceded by the `with` keyword, with the key `type` used as an example indicating the module type. Such key-value syntax can be used in various different contexts.
54 |
55 | ### import statements
56 |
57 | The ImportDeclaration would allow any arbitrary attributes after the `with` keyword.
58 |
59 | For example, the `type` attribute could be used to indicate a module type, for example importing a JSON module with the following syntax.
60 |
61 | ```mjs
62 | import json from "./foo.json" with { type: "json" };
63 | ```
64 |
65 | The `with` syntax in the `ImportDeclaration` statement uses curly braces, for the following reasons (as discussed in [#5](https://github.com/tc39/proposal-import-attributes/issues/5)):
66 | - JavaScript developers are already used to the Object literal syntax and since it allows a trailing comma copy/pasting attributes will be easy.
67 | - it clearly indicates the end of the attributes list when splitting them across multiple lines.
68 |
69 | ### re-export statements
70 |
71 | Similar to import statements, the ExportDeclaration, when re-exporting from another module, would allow any arbitrary attributes after the `with` keyword.
72 |
73 | ```mjs
74 | export { val } from './foo.js' with { type: "javascript" };
75 | ```
76 |
77 | ### dynamic import()
78 |
79 | The `import()` pseudo-function would allow import attributes to be indicated in an options bag in the second argument.
80 |
81 | ```js
82 | import("foo.json", { with: { type: "json" } })
83 | ```
84 |
85 | The second parameter to `import()` is an options bag, with the only option currently defined to be `with`: the value here is an object containing the import attributes. There are other proposals for entries to put in the options bag: for example, the [Module Source Imports](https://github.com/tc39/proposal-import-reflection) proposal introduces a `phase` property.
86 |
87 | ### Integration of modules into environments
88 |
89 | Host environments (e.g., the Web platform, Node.js) often provide various different ways of loading modules. The analogous string could be passed through these ways of loading other kinds of modules.
90 |
91 | #### Worker instantiation
92 |
93 | ```js
94 | new Worker("foo.wasm", { type: "module", with: { type: "webassembly" } });
95 | ```
96 |
97 | Sidebar about WebAssembly module types and the web: it's still uncertain whether importing WebAssembly modules would need to be marked specially, or would be imported just like JavaScript. Further discussion in [#19](https://github.com/tc39/proposal-import-attributes/issues/19).
98 |
99 | #### HTML
100 |
101 | Although changes to HTML won't be specified by TC39, an idea here would be that each import attribute, preceded by `with`, becomes an HTML attribute which could be used in script tags.
102 |
103 | ```html
104 |
105 | ```
106 |
107 | (See the caveat about WebAssembly above.)
108 |
109 | #### WebAssembly
110 |
111 | In the context of the [WebAssembly/ESM integration proposal](https://github.com/webassembly/esm-integration): for imports of other module types from within a WebAssembly module, this proposal would introduce a new custom section (named `importattributes`) that will annotate with attributes each imported module (which is listed in the import section).
112 |
113 | ## Proposed semantics and interoperability
114 |
115 | This proposal does not specify behavior for any particular attribute key or value. The [JSON modules proposal](https://github.com/tc39/proposal-json-modules) will specify that `type: "json"` must be interpreted as a JSON module, and will specify common semantics for doing so. It is expected the `type` attribute will be leveraged to support additional module types in future TC39 proposals as well as by hosts. HTML and CSS modules are under consideration, and these may use similar explicit `type` syntax when imported.
116 |
117 | Attributes in addition than `type` may also be introduced for purposes not yet foreseen.
118 |
119 | JavaScript implementations are encouraged to reject attributes and type values which are not implemented in their environment (rather than ignoring them). This is to allow for maximal flexibility in the design space in the future--in particular, it enables new import attributes to be defined which change the interpretation of a module, without breaking backwards-compatibility.
120 |
121 | ## FAQ
122 |
123 | ### Why not out of band?
124 |
125 | Why not both? The champions of this proposal think that exploring both an in- and out of band solutions to various kinds of metadata. While we prefer in-band metadata for module types, we are happy to see the development of various out-of-band manifests of modules being proposed and implemented in certain JS environments:
126 | - [import maps](https://github.com/WICG/import-maps) to map module specifiers to URLs/paths
127 | - [Node.js policy files](https://nodejs.org/api/policy.html), e.g., to perform integrity checks on modules
128 |
129 | This proposal does not exclude out-of-band metadata being used for module types. And it definitely doesn't argue that all metadata should be in-band. For example, integrity hashes simply don't work in-band, both because module circularities make them impossible to calculate, and because of the need for a "cascading" update when a deep dependency changes.
130 |
131 | Out-of-band solutions face certain downsides; these are not necessarily fatal, but are interesting to take into account when considering the solution space and making tradeoffs:
132 | - **By-hand authoring experience**: While an in-band solution is somewhat verbose, it is also more straightforward for developers to adopt when writing code without much tooling. For smaller projects developers do not need to create an extra file by hand.
133 | - **Tooling complexity for large projects**: For large project with many dependencies, developers will not have to worry about creating a large manifest by compiling the metadata of all of their dependencies. Module authors will also not have to worry about shipping a manifest in order for consumers to be able to run their modules.
134 | - **Performance tradeoffs**: The experience in Node.js's experimental, out-of-band policy files is that they can carry significant startup cost, due to certain aspects of loading and parsing.
135 |
136 | ### How is common behavior ensured across JavaScript environments?
137 |
138 | A central goal of this proposal is to share as much syntax and behavior across JavaScript environments as possible. To the same end, we also [propose](https://github.com/tc39/proposal-json-modules) a standardization of JSON modules to the extent that this is possible (omitting just the contents of the redundant type check, which necessarily differs between environments, in addition to the pre-existing host-defined parts such as interpreting the module specifier and fetching the module).
139 |
140 | However, at the same time, behavior of modules in general, and the set of module types specifically, is expected to differ across JavaScript environments. For example, WebAssembly, HTML and CSS modules may not make sense in certain minimal embedded JavaScript environments. We hope that environments can experiment and collaborate where it makes sense for them.
141 |
142 | We see the management of compatibility issues across environments as similar, independent of whether metadata is held in-band or out-of-band. An out of band solution would also suffer from the risk of inconsistent implementation or support across host environments if some kind of coordination does not occur.
143 |
144 | The topic of attribute divergence is further discussed in [#34](https://github.com/tc39/proposal-import-attributes/issues/34).
145 |
146 | ### How would this proposal work with caching?
147 |
148 | Attributes are part of the module cache key and can affect how a module is loaded: the cache key is extended from _(referrer, specifier)_ to _(referrer, specifier, attributes)_.
149 |
150 | ### Why not use more terse syntax to indicate module types, like `import json from "./foo.json" as "json"`?
151 |
152 | Another option considered and not selected has been to use a single string as the attribute, indicating the type. This option is not selected due to its implication that any particular attribute is special; even though this proposal only specifies the `type` attribute, the intention is to be open to more attributes in the future. (discussion in [#12](https://github.com/tc39/proposal-import-attributes/issues/12)).
153 |
154 | ### Should more than just strings be supported as attribute values?
155 |
156 | We could permit import attributes to have more complex values than simply strings, for example:
157 |
158 | ```js
159 | import value from "module" with { attr: { key1: "value1", key2: [1, 2, 3] } };
160 | ```
161 |
162 | This would allow import attributes to scale to support a larger variety of metadata.
163 |
164 | We propose to omit this generalization in the initial proposal, as a key/value list of strings already affords significant flexibility to start, but we're open to a follow-on proposal providing this kind of generalization.
165 |
166 | ### What are you open to changing? When do we have to settle down on the details?
167 |
168 | We are planning to make descisions and reach consensus during specific stages of this proposal. Here's our plan.
169 |
170 | Original plan before Stage 2 and Stage 3
172 |
173 | #### Before stage 2
174 |
175 | We have achieved consensus on the following core decisions as part of Stage 2, including:
176 |
177 | - The attribute form; key-value or single string ([#12](https://github.com/tc39/proposal-import-attributes/issues/12))
178 |
179 | ```mjs
180 | // Not selected
181 | import value from "module" as "json";
182 |
183 | // Not selected
184 | import value from "module" with type: "json";
185 |
186 | // Proposal that was approved from Stage 2 to Stage 3 the first time
187 | import value from "module" assert { type: "json" };
188 | ```
189 |
190 | #### Before stage 3
191 |
192 | After Stage 2 and before Stage 3, we're open to settling on some less core details, such as:
193 |
194 | - Considering alternatives for the `with`/`if`/`assert` keywords ([#3](https://github.com/tc39/proposal-import-attributes/issues/3))
195 |
196 | ```mjs
197 | import value from "module" when { type: 'json' };
198 | import value from "module" given { type: 'json' };
199 | ```
200 |
201 | - How dynamic import would accept import attributes:
202 | ```mjs
203 | import("foo.wasm", { with: { type: "webassembly" } });
204 | ```
205 |
206 | For consistency the `assert` key is used for both dynamic and static imports.
207 |
208 | An alternative would be to remove the `assert` nesting in the object:
209 | ```mjs
210 | import("foo.wasm", { type: "webassembly" });
211 | ```
212 |
213 | However, that's not possible with the `Worker` API since it already uses an object with a `type` key as the second parameter. Which would make the APIs inconsistent.
214 |
215 |
7 | title: Import Attributes 8 | status: proposal 9 | stage: 3 10 | location: https://github.com/tc39/proposal-import-attributes 11 | copyright: false 12 | contributors: Sven Sauleau, Myles Borins, Daniel Ehrenberg, Daniel Clark, Nicolò Ribaudo 13 |14 | 15 |
See the explainer for information.
18 |The relevant syntax changes are in the Import Calls and Imports sections.
19 | 20 |This document might be out of date. Please refer to the tc39/ecma262#3057 preview for the latest version.
22 |A ModuleRequest Record represents the request to import a module with given import attributes. It consists of the following fields:
136 |141 | Field Name 142 | | 143 |144 | Value Type 145 | | 146 |147 | Meaning 148 | | 149 |
---|---|---|
152 | [[Specifier]] 153 | | 154 |155 | String 156 | | 157 |158 | The module specifier 159 | | 160 |
163 | [[Attributes]] 164 | | 165 |166 | a List of ImportAttribute Records 167 | | 168 |169 | The import attributes 170 | | 171 |
A LoadedModuleRequest Record represents the request to import a module together with the resulting Module Record. It consists of the same fields defined in table
183 | Field Name 184 | | 185 |186 | Value Type 187 | | 188 |189 | Meaning 190 | | 191 |
---|---|---|
194 | [[Specifier]] 195 | | 196 |197 | a String 198 | | 199 |200 | The module specifier 201 | | 202 |
205 | [[Attributes]] 206 | | 207 |208 | a List of ImportAttribute Records 209 | | 210 |211 | The import attributes 212 | | 213 |
216 | [[Module]] 217 | | 218 |219 | a Module Record 220 | | 221 |222 | The loaded module corresponding to this module request 223 | | 224 |
An ImportAttribute Record consists of the following fields:
229 |234 | Field Name 235 | | 236 |237 | Value Type 238 | | 239 |240 | Meaning 241 | | 242 |
---|---|---|
245 | [[Key]] 246 | | 247 |248 | String 249 | | 250 |251 | The attribute key 252 | | 253 |
256 | [[Value]] 257 | | 258 |259 | String 260 | | 261 |262 | The attribute value 263 | | 264 |
A Cyclic Module Record is used to represent information about a module that can participate in dependency cycles with other modules that are subclasses of the Cyclic Module Record type. Module Records that are not subclasses of the Cyclic Module Record type must not participate in dependency cycles with Source Text Module Records.
397 |In addition to the fields defined in
402 | Field Name 403 | | 404 |405 | Value Type 406 | | 407 |408 | Meaning 409 | | 410 |
---|---|---|
413 | [[Status]] 414 | | 415 |416 | | 417 | |
420 | [[EvaluationError]] 421 | | 422 |423 | | 424 | |
427 | [[DFSIndex]] 428 | | 429 |430 | | 431 | |
434 | [[DFSAncestorIndex]] 435 | | 436 |437 | | 438 | |
441 | [[RequestedModules]] 442 | | 443 |
444 | a List of |
446 | 447 | A List of all the |ModuleSpecifier| strings and import attributes used by the module represented by this record to request the importation of a module. The List is in source text occurrence order. 448 | | 449 |
452 | [[LoadedModules]] 453 | | 454 |
455 | a List of |
457 |
458 | A map from the specifier strings used by the module represented by this record to request the importation of a module with the relative attributes to the resolved Module Record. The list does not contain two different Records with the same |
460 |
463 | [[CycleRoot]] 464 | | 465 |466 | | 467 | |
470 | [[HasTLA]] 471 | | 472 |473 | | 474 | |
477 | [[AsyncEvaluation]] 478 | | 479 |480 | | 481 | |
484 | [[TopLevelCapability]] 485 | | 486 |487 | | 488 | |
491 | [[AsyncParentModules]] 492 | | 493 |494 | | 495 | |
498 | [[PendingAsyncDependencies]] 499 | | 500 |501 | | 502 | |
An ImportEntry Record is a Record that digests information about a single declarative import. Each ImportEntry Record has the fields defined in
569 | Field Name 570 | | 571 |572 | Value Type 573 | | 574 |575 | Meaning 576 | | 577 |
---|---|---|
580 | [[ModuleRequest]] 581 | | 582 |
583 | |
586 |
587 | |
590 |
593 | [[ImportName]] 594 | | 595 |596 | | 597 | |
600 | [[LocalName]] 601 | | 602 |603 | | 604 | |
An example of when _referrer_ can be a Realm Record is in a web browser host. There, if a user clicks on a control given by
626 | 627 |<button type="button" onclick="import('./foo.mjs')">Click me</button>
628 |
629 | there will be no active script or module at the time the
An implementation of HostLoadImportedModule must conform to the following requirements:
633 |If this operation is called multiple times with the sametwo (_referrer_, _specifier__moduleRequest_) pairs such that:
then it must perform FinishLoadingImportedModule(_referrer_, _specifier__moduleRequest_, _payload_, _result_) with the same _result_ each time.
The actual process performed is host-defined, but typically consists of performing whatever I/O operations are necessary to load the appropriate Module Record. Multiple different (_referrer_, _specifier_) pairs(_referrer_, _moduleRequest_.[[Specifer]], _moduleRequest_.[[Attributes]]) triples may map to the same Module Record instance. The actual mapping semantics is host-defined but typically a normalization process is applied to _specifier_ as part of the mapping process. A typical normalization process would include actions such as expansion of relative and abbreviated path specifiers.
The description of the [[LoadedModules]] field of Realm Record, Script Record, and Cyclic Module Record should be updated to use LoadedModuleRequest Records.
682 |An implementation of HostGetSupportedImportAttributes must conform to the following requrements:
716 | 717 |The default implementation of HostGetSupportedImportAttributes is to return a new empty List.
726 | 727 |The import attributes proposal is intended to give key information about how modules are interpreted to hosts. For the Web embedding and environments which aim to be similar to it, the string is interpreted as the "module type". This is not the primary way the module type is determined (which, on the Web, would be the MIME type, and in other environments may be the file extension), but rather a secondary check which is required to pass for the module graph to load.
835 | 836 |In the Web embedding, the following changes would be made to the HTML specification for import attributes:
837 | 838 |The module map is keyed by the absolute URL and the _type_. Initially no other import attributes are supported, so they are not present.
851 |