Re: [w3c/webcomponents] HTML Modules (#645)

I think the `as` part is useful to include still, even if limited to built-in types:

- it might actually help static analysis, since a static analyser might not know what the MIME type would be in advance
- it makes the intent explicit, which helps you understand the code
- it verifies the MIME type is the expected one, i.e. that you get an error if the MIME type accidentally changes, rather than silently switching the type of import.

I can see custom imports being contentious for static imports since they are inherently dynamic. Perhaps if dynamic `import()` handles custom imports, then you can at least do something like this:

```js
// normal static imports
import * from '/foo.js';
import doc from '/foo.html' as Document; // built-in extension

// dynamic imports, library implementations with Symbol.importer
Promise.all([
    import('/module1.html', HTMLModule),
    import('/module1.html', HTMLModule)
])
.then(imports =>
{
    // ...
});
```

Something like a top-level `await` in modules would make this case look nicer, but a quick shows [that is contentious](https://gist.github.com/Rich-Harris/41e8ccc755ea232a5e7b88dee118bcf5):

```js
// normal static imports
import * from '/foo.js';
import doc from '/foo.html' as Document; // built-in extension

// dynamic imports, library implementations with Symbol.importer
const module1 = await import('/module1.html', HTMLModule);
const module2 = await import('/module2.html', HTMLModule);

// ...
```

As the other article points out, this appears to be inherently slower. Since static imports can be statically analysed, the browser can start fetching them all simultaneously, whereas dynamic imports end up doing a sequential set of requests as each one finishes loading. Maybe that means we should go back to using static import for custom imports so the browser can at least statically analyse all the URLs and start prefetching them... would we be able to have this?

```js
// enables importing as HTMLModule, using library implementation in lib.js
import HTMLModule from '/lib.js'
import doc from '/module.html' as HTMLModule; // custom import
```

-- 
You are receiving this because you are subscribed to this thread.
Reply to this email directly or view it on GitHub:
https://github.com/w3c/webcomponents/issues/645#issuecomment-322753078

Received on Wednesday, 16 August 2017 12:21:37 UTC