Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ES6 Modules vs. HTML Imports

HTML Imports are a part of the Web Components specification and provide a way to handle dependencies on the Web. ES6 modules also do the same thing, but just for Javascript code.

Is there is any clarity on how these two will work together?

Edit: An example: On a recent project of mine, I had two Javascript components (files) one of which depended on the other, but any HTML code (which is another component) could use either of them. So when I included the dependent script in my HTML, I did not want to include the parent script too (avoiding manual dependency handling). There seems to be no well-defined way to do it, other than mixing ES6 modules with script includes. The only option I see is maintaining a separate file for each component, with the required files and dependencies specified, such as component.io does.

like image 670
Debjit Avatar asked Jan 17 '14 03:01

Debjit


People also ask

What is the difference between HTML import and ES6 import?

HTML Imports are a part of the Web Components specification and provide a way to handle dependencies on the Web. ES6 modules also do the same thing, but just for Javascript code.

What are the basics of ES6 modules?

The basics of ES6 modules 16.3.1. Named exports (several per module) 16.3.2. Default exports (one per module) 16.3.3. Imports and exports must be at the top level 16.3.4. Imports are hoisted 16.3.5. Imports are read-only views on exports

What are the different types of exports in ES6?

16.3 The basics of ES6 modules # There are two kinds of exports: named exports (several per module) and default exports (one per module). As explained later, it is possible use both at the same time, but usually best to keep them separate.

What are read-only views on ES6 module exports?

The imports of an ES6 module are read-only views on the exported entities. That means that the connections to variables declared inside module bodies remain live, as demonstrated in the following code.


2 Answers

How web components interact with ES6 modules has not been finalized yet, but there are at least two options.

ES6 has the notion of realms. If you have JavaScript in two iFrames, then the two iFrames can communicate with each other and pass data back and forth. But they are in different realms. This means that you can modify the Array.prototype object in one without affecting the other. Each web component will most likely have its own realm, and so they will not interfere with each other.

Each realm has a bunch of global objects, and that includes (most likely, the spec isn't finalized yet) the Loader object. You can create a new instance of a Loader and use it to load modules. There already exists one in the realm, which is the default one. Each Loader instance has it's own list of defined modules, and so each web component could be given it's own Loader instance.

I'm not sure if webcomponents will be given different Realms or different Loader objects, but different web components will most likely not be able to interfere with each other.

like image 171
Marius Avatar answered Oct 16 '22 16:10

Marius


Here's the latest on this: Chrome has native support for all 4 Web Component specs, while Mozilla announced they will not ship an implementation of HTML Imports precisely because of this pending reconciliation with ES6 Modules, which isn't going to be resolved anytime soon, specially coz here's what Mozilla has to say about it:

We expect that once JavaScript modules — a feature derived from JavaScript libraries written by the developer community — is shipped, the way we look at this problem will have changed. We have also learned from Gaia and others, that lack of HTML Imports is not a problem as the functionality can easily be provided for with a polyfill if desired.

`

like image 37
Tarun Avatar answered Oct 16 '22 15:10

Tarun