* 仕様書

[REFS[
- [1] [CITE@en-GB-x-hixie[HTML Standard]] ([TIME[2016-02-16 01:30:20 +09:00]] 版) <https://html.spec.whatwg.org/#hostresolveimportedmodule(referencingmodule,-specifier)>
]REFS]

* Web ブラウザーの処理

[2] [[Webブラウザー]]は、
[VAR[referencing]] と[VAR[指定子]]に関する
[DFN[[CODE[HostResolveImportedModule]]
を、次のようにしなければ[MUST[なりません]] [SRC[>>1]]。

[FIG(steps)[
= [3] [VAR[参照元スクリプト]]を、 [VAR[referencing]] の [F(ss)[HostDefined]] に設定します。
= [5] [VAR[URL]] を、[VAR[参照元スクリプト]]と[VAR[指定子]]について[[モジュール指定子を解決]]した結果に設定します。
= [4] [VAR[参照元スクリプト]]の[F[設定群オブジェクト]]の[F[モジュール写像]] [ [VAR[URL]] ] の[F[モジュール記録]]を返します。
]FIG]

[13] つまり、与えられた[[モジュール指定子]]を解決して得られた [[URL]]
が、既に読み込み済みの[[モジュールスクリプト]]の [[URL]] であれば、
その[[モジュールスクリプト]]が使われます。

;; [10] [[静的]]な [CODE[import][import()]] で使われます。

-*-*-

[8] [[Webブラウザー]]は、
[VAR[referencing]] と[VAR[指定子]]、 [VAR[promiseCapability]] に関する
[DFN[[CODE[HostImportModuleDynamically]]]]
を、次のようにしなければ[MUST[なりません]]。 [SRC[>>1]]

[FIG(steps)[
= [9] [VAR[スクリプト]]を、 [VAR[referencing]] の [F(ss)[HostDefined]] に設定します。
= [12] [VAR[URL]] を、[VAR[スクリプト]]と[VAR[指定子]]について[[モジュール指定子を解決]]した結果に設定します。
= [15] [VAR[URL]] が[[失敗]]の場合、
== [16] [VAR[完了]]を、新しい [[Completion]] に設定します。
[FIG(list members)[

: [F(ss)[Type]] : [[throw]]
: [F(ss)[Value]] : 新しい [CODE[TypeError]]
: [F(ss)[Target]] : [[空]]

]FIG]
== [20] [VAR[referencing]]、[VAR[指定子]]、[VAR[promiseCapability]], [VAR[完了]]について
[CODE[FinishDynamicImport]] を実行します。
= [22] それ以外の場合、
== [23] [VAR[オプション群]]を、 
[VAR[スクリプト]]の[F[fetchオプション群]]の[F[子孫スクリプトfetchオプション群]]に設定します。
== [21] [[モジュールスクリプトグラフのfetch]] を実行します。
[FIG(list members)[

: [VAR[URL]] : [VAR[URL]]
: [VAR[設定群オブジェクト]] : [VAR[referencing]]の[F[設定群オブジェクト]]
: [VAR[終点]] : [CODE[script]]
: [VAR[オプション群]] : [VAR[オプション群]]
: [VAR[続きの処理]] : >>11

]FIG]
= [33] [[未定義]]を返します。
]FIG]

[11] 続きは、[VAR[結果]]について、次のようにします。 [SRC[>>1]]

[FIG(steps)[
= [24] [VAR[結果]]が [CODE[null]] の場合、
== [25] [VAR[完了]]を、新しい [[Completion]] に設定します。
[FIG(list members)[
: [F(ss)[Type]] : [[throw]]
: [F(ss)[Value]] : [CODE[TypeError]]
: [F(ss)[Target]] : [[空]]
]FIG]
== [26] [VAR[referencing]]、[VAR[指定子]]、[VAR[promiseCapability]]、[VAR[完了]]について、
[[FinishDynamicImport]] を実行します。
= [27] それ以外の場合、
== [28] [[モジュールスクリプトの実行]]をします。
[FIG(list members)[
: [VAR[スクリプト]] : [VAR[結果]]
: [VAR[エラーを再び投げる]] : [[真]]
]FIG]
[VAR[例外]]を、[[投げ]]られた[[例外]]に設定します。
== [29] [VAR[例外]]がある場合、
=== [30] [VAR[referencing]]、[VAR[指定子]]、[VAR[promiseCapability]]、
[VAR[例外]]について [CODE[FinishDynamicImport]] を実行します。
== [31] それ以外の場合、
=== [32] [VAR[referencing]]、[VAR[指定子]]、[VAR[promiseCapability]]、
[[NormalCompletion]] ([[未定義]]) について [CODE[FinishDynamicImport]]
を実行します。
]FIG]

* 歴史

[14] [CITE@en[Handle error cases during module script tree fetching/running better]]
([[domenic]]著, [TIME[2016-08-09 12:06:32 +09:00]])
<https://github.com/whatwg/html/commit/e1af86747f591e33fd0f3f21f301dc866868c9d4>

[17] [CITE@en[Fix bad assert in HostResolveImportedModule]]
([[domenic]]著, [TIME[2017-04-29 05:16:21 +09:00]])
<https://github.com/whatwg/html/commit/e7041402890076aaf7095a3f8931ba0a00393b72>

[18] [CITE@en[Fix error cases of <script type=module>]]
([[domenic]]著, [TIME[2017-05-13 01:50:49 +09:00]])
<https://github.com/whatwg/html/commit/115763124a641e8814665c5014d28155f95ff441>

[19] [CITE@en[Don't handle impossible errors in HostResolveImportedModule]]
([[domenic]]著, [TIME[2017-05-13 01:59:57 +09:00]])
<https://github.com/whatwg/html/commit/616df18cd9e6c17878db5167909c69ca9f9d2906>

[7] [CITE@en[Improve module instantiation/evaluation, especially around errors]]
([[domenic]]著, [TIME[2017-08-04 05:13:45 +09:00]])
<https://github.com/whatwg/html/commit/2b408b65a11fe76b6588b744e0ae74d30ff29b43>

[6] [CITE@en[Integrate dynamic import()]]
([[domenic]]著, [TIME[2017-09-14 21:11:01 +09:00]])
<https://github.com/whatwg/html/commit/d4d1704f9c0efbeed78b885d566af17545b8b5fe>

[35] [CITE@en[Spec import() HTML integration · Issue #2315 · whatwg/html]]
([TIME[2017-10-05 15:15:59 +09:00]])
<https://github.com/whatwg/html/issues/2315>

[36] [CITE@en[Add integrity="" for module scripts, and integrate dynamic import() by domenic · Pull Request #3044 · whatwg/html]]
([TIME[2017-10-05 15:16:34 +09:00]])
<https://github.com/whatwg/html/pull/3044>

[37] [CITE@en[Specify which settings object is used for dynamic import]]
([[nhiroki]]著, [TIME[2018-05-23 05:51:21 +09:00]])
<https://github.com/whatwg/html/commit/07ab5082c326a0548b5d7665ac1524f1fa9e2818>

[38] [CITE@en[Specify which settings object is used for dynamic import by nhiroki · Pull Request #3700 · whatwg/html]]
([TIME[2018-05-23 11:46:58 +09:00]])
<https://github.com/whatwg/html/pull/3700>



[39] [CITE@en[Peeling out import() · Issue #149 · whatwg/loader]]
([TIME[2017-01-31 13:10:57 +09:00]])
<https://github.com/whatwg/loader/issues/149>

[FIG(quote)[
[FIGCAPTION[
[40] [CITE@en[tc39/proposal-dynamic-import: import() proposal for JavaScript]]
([TIME[2017-01-31 13:11:26 +09:00]])
<https://github.com/tc39/proposal-dynamic-import>
]FIGCAPTION]

> This repository contains a proposal for adding a "function-like" import() module loading syntactic form to JavaScript. It is currently in stage 3 of the TC39 process. Previously it was discussed with the module-loading community in whatwg/loader#149.

]FIG]

[41] [CITE[import()]]
([TIME[2016-12-08 00:57:43 +09:00]])
<https://tc39.github.io/proposal-dynamic-import/>

[42] [CITE@en[proposal-dynamic-import/HTML Integration.md at master · tc39/proposal-dynamic-import]]
([TIME[2017-01-31 13:15:29 +09:00]])
<https://github.com/tc39/proposal-dynamic-import/blob/master/HTML%20Integration.md>

[43] [CITE@en[Discussion: Towards a module map proposal · Issue #2547 · whatwg/html]]
([TIME[2017-04-22 15:22:42 +09:00]])
<https://github.com/whatwg/html/issues/2547>

[44] [CITE@en[Editorial: refactor classic and module scripts to be more alike]]
([[domenic]]著, [TIME[2017-09-07 05:40:50 +09:00]])
<https://github.com/whatwg/html/commit/dce999aac4d9deeefa3b338dd2aeb30c17669bc8>

[45] [CITE@en[Editorial: move base URL from "module script" to "script"]]
([[domenic]]著, [TIME[2017-09-14 20:28:50 +09:00]])
<https://github.com/whatwg/html/commit/5458513792ab00d58e6c91ba48faaa611d034a2e>

[46] [CITE@en[Add integrity="" for module scripts, and integrate dynamic import() by domenic · Pull Request #3044 · whatwg/html]]
([TIME[2017-10-04 22:45:17 +09:00]])
<https://github.com/whatwg/html/pull/3044>

[47] [CITE@en[Make integrity="" work on module scripts]]
([[domenic]]著, [TIME[2017-09-14 18:00:24 +09:00]])
<https://github.com/whatwg/html/commit/9275d955dcd604e959cfcc672e0c234b1b8c00db>

[48] [CITE@en[Integrate dynamic import()]]
([[domenic]]著, [TIME[2017-09-14 21:11:01 +09:00]])
<https://github.com/whatwg/html/commit/d4d1704f9c0efbeed78b885d566af17545b8b5fe>

[49] [CITE@en[Adds "audioworklet" and "paintworklet" as destinations. by bfgeek · Pull Request #527 · whatwg/fetch]]
([TIME[2017-11-02 17:58:21 +09:00]])
<https://github.com/whatwg/fetch/pull/527>

[50] [CITE@en[Dynamic import()  |  Web  |  Google Developers]]
([TIME[2017-11-23 00:46:57 +09:00]])
<https://developers.google.com/web/updates/2017/11/dynamic-import>

[51] [CITE@en[Peeling out import() · Issue #149 · whatwg/loader]]
([TIME[2017-12-07 02:35:27 +09:00]])
<https://github.com/whatwg/loader/issues/149>

[52] [CITE@en[New in Chrome 63  |  Web  |  Google Developers]]
([TIME[2017-12-08 00:12:30 +09:00]])
<https://developers.google.com/web/updates/2017/12/nic63?utm_source=feed&utm_medium=feed&utm_campaign>

[53] [CITE@en[Fix import() inside setTimeout()/setInterval() strings]]
([[domenic]]著, [TIME[2017-12-18 15:08:32 +09:00]])
<https://github.com/whatwg/html/commit/12cdfefad607fc824e6ea64a4b967fc24043f684>

[54] [CITE@en[setTimeout() base URL/script fetch options behavior seems wrong · Issue #3116 · whatwg/html]]
([TIME[2017-12-21 17:53:12 +09:00]])
<https://github.com/whatwg/html/issues/3116>

[55] [CITE@en[Fix import() inside setTimeout()/setInterval() strings by domenic · Pull Request #3117 · whatwg/html]]
([TIME[2017-12-21 17:56:17 +09:00]])
<https://github.com/whatwg/html/pull/3117>

[56] [CITE@en[DOM handler and import · Issue #32 · tc39/proposal-dynamic-import]]
([TIME[2017-12-21 17:57:52 +09:00]])
<https://github.com/tc39/proposal-dynamic-import/issues/32>

[57] 今まで [[JavaScript]] [[API]] の [[URL]] は原則的に[[文書]]の[[基底URL]]
を使っていたので、その原則が崩れてしまうのは微妙。
でも外部スクリプトファイルの [CODE[import]] が自身の [[URL]]
じゃなくて使っている[[文書]]の[[基底URL]]を使うようになると、
この上なく使いにくくなりそうだ。じゃあ [CODE[import()]]
もそれと一貫してほしいってことになるのは自然。

[58] [CODE[eval "import()"]] と [CODE[ setTimeout ("import()", ...) ]]
は含まれているスクリプトファイルの[[基底URL]]を引き継ぐ。
[CODE[element.onclick = function () { import () }]]
もそう。一方
[CODE[element.setAttribute ("onclick", "import()")]]
は文書の[[基底URL]]になる、のかな。
でも[[Webブラウザー]]が[[発火]]させるより前に
[CODE[element.onclick]] にアクセスしたら、その時点でコンパイルされるけど、
どうなるのか。


[59] [CITE@en[Editorial: change "resolve a module specifier" to take a URL]]
([[domfarolino]]著, [TIME[2018-07-10 05:10:41 +09:00]])
<https://github.com/whatwg/html/commit/8176d055eaef62cad03327a7cc47c5c7926ffb0c>

[60] [CITE@en[Resolve a module specifier now takes a base URL by domfarolino · Pull Request #3807 · whatwg/html]]
([TIME[2018-07-26 21:39:18 +09:00]])
<https://github.com/whatwg/html/pull/3807>



[34] [CITE@en[Do not run FinishDynamicImport steps in parallel]]
([[domenic]]著, [TIME[2018-11-21 04:46:07 +09:00]])
<https://github.com/whatwg/html/commit/4ee44da9eb0f94bfa77001e8bcf16ede6050630e>

[61] [CITE@en[Do not run FinishDynamicImport steps in parallel by domenic · Pull Request #4182 · whatwg/html]]
([TIME[2019-05-31 14:04:03 +09:00]])
<https://github.com/whatwg/html/pull/4182>

[62] [CITE@en[Dev edition: improve the "Scripting" subsection]]
([[domenic]]著, [TIME[2018-12-10 20:23:20 +09:00]])
<https://github.com/whatwg/html/commit/ebdd2a03de44c6a51e837dbb2dbe6400d8bc6b40>

[63] [CITE@en[Dev edition: improve the "Scripting" subsection by domenic · Pull Request #4183 · whatwg/html]]
([TIME[2019-05-31 14:44:26 +09:00]])
<https://github.com/whatwg/html/pull/4183>

[64] [CITE@en[Always set '''[''''''['''ScriptOrModule''']'''''']''' to null for event handler functions]]
([[domenic]]著, [TIME[2019-02-09 06:47:54 +09:00]])
<https://github.com/whatwg/html/commit/1d6eb9933701ea65f7a9dd670d37a2faa17b8f12>

[65] [CITE@en[import() behaviour in event handlers is path dependent · Issue #4267 · whatwg/html]]
([TIME[2019-06-20 17:51:49 +09:00]])
<https://github.com/whatwg/html/issues/4267>

[66] [CITE@en[Handle module resolution when there is no active script by domenic · Pull Request #4181 · whatwg/html]]
([TIME[2019-06-20 17:52:39 +09:00]])
<https://github.com/whatwg/html/pull/4181>

[67] [CITE@en[Always set '''[''''''['''ScriptOrModule''']'''''']''' to null for event handler functions by domenic · Pull Request #4302 · whatwg/html]]
([TIME[2019-06-20 17:53:40 +09:00]])
<https://github.com/whatwg/html/pull/4302>

[68] [CITE@en[Editorial: update some JavaScript spec references]]
([[domenic]]著, [TIME[2019-08-23 04:36:23 +09:00]])
<https://github.com/whatwg/html/commit/6227b7e91913d21157a1265f6e294a7a89f916c4>