From 635862ca3e74b1ed96b6e1be01345c586bbc6405 Mon Sep 17 00:00:00 2001 From: Daniel Ehrenberg Date: Tue, 4 Dec 2018 10:59:56 +0100 Subject: [PATCH 1/6] Describe fallback based on top-level await Addresses #61 --- README.md | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/README.md b/README.md index 99cde59..07db5f4 100644 --- a/README.md +++ b/README.md @@ -302,6 +302,28 @@ would not: in all classes of browsers, it would fetch the polyfill unconditional which will work as desired in all classes of browser. +#### Alternating between different logic based on the existence of a built-in module + +_See further discussion of this case in the issue tracker: [#61](https://github.com/domenic/package-name-maps/issues/61)._ + +Not all fallbacks take the role of running one piece of code. For example, sometimes, one code path is to be taken if a particular platform API exists, and another code path is taken if it doesn't exist. The import maps proposal does not aim to solve all such scenarios in a built-in way; instead, The Stage 2 TC39 proposal [top-level await](https://github.com/tc39/proposal-top-level-await) can be used to meet some of these use cases. Note that top-level await blocks import of the module containing the await. + +Imagine IndexedDB were provided by a built-in module `@std/indexed-db` and localStorage were provided by a built-in module `@std/local-storage`. For a particular application, all supported browsers support localStorage, and only some support IndexedDB. Although it's possible to polyfill IndexedDB on top of localStorage, in this scenario, doing so leads to performance overhead vs more specialized usage. Therefore, it's preferrable to use a specialized implementation based on either IndexedDB or localStorage directly. + +In this scenario, a module may use top-level await to perform feature testing and fallback as follows: + +```js +export let myStorageFunction; + +try { + const indexedDB = await import("@std/indexed-db"); + myStorageFunction = function() { /* in terms of indexedDB */ }; +} catch (e) { + const localStorage = await import(@std/local-storage"); + myStorageFunction = function() { /* in terms of localStorage */ }; +} +``` + ### Scoping examples #### Multiple versions of the same module From edd0fe300ac80fa364c5eefba1b835f7c894e812 Mon Sep 17 00:00:00 2001 From: Daniel Ehrenberg Date: Tue, 4 Dec 2018 11:01:03 +0100 Subject: [PATCH 2/6] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 07db5f4..a961468 100644 --- a/README.md +++ b/README.md @@ -319,7 +319,7 @@ try { const indexedDB = await import("@std/indexed-db"); myStorageFunction = function() { /* in terms of indexedDB */ }; } catch (e) { - const localStorage = await import(@std/local-storage"); + const localStorage = await import("@std/local-storage"); myStorageFunction = function() { /* in terms of localStorage */ }; } ``` From 1c36108dc957f10ab22c08ceb5186de2c45a3d9e Mon Sep 17 00:00:00 2001 From: Daniel Ehrenberg Date: Tue, 4 Dec 2018 11:03:33 +0100 Subject: [PATCH 3/6] Update README.md --- README.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index a961468..406164c 100644 --- a/README.md +++ b/README.md @@ -19,6 +19,7 @@ _Or, how to control the behavior of JavaScript imports_ - [For built-in modules, in module-import-map-supporting browsers](#for-built-in-modules-in-module-import-map-supporting-browsers) - [For built-in modules, in browsers without import maps](#for-built-in-modules-in-browsers-without-import-maps) - [This doesn't work for `