News for hackers. Stuff that matters.
Re:Microsoft Edge supports ES6 modules by mevile on Tuesday May 17, 2016 @02:31PM (#11716072)
So what I still don't understand is how es6 module imports work with requests. Even reading through that blog post it's not obvious to me. Does the browser make an HTTP request for the file on the server based on the relative location on the server? So if I have type="module" and app.js is in example.com/js/app.js and then I have an import statement for ./foo/bar.js it makes that request to example.com/js/foo/bar.js? I imagine all these requests would add up and will block if so. With HTTP2 maybe it would be OK to do it this way but I see myself creating single JavaScript builds for the foreseeable future making this sort of module support unlikely to help in anything other than development (which still is nice).
Re:Microsoft Edge supports ES6 modules by calebegg on Tuesday May 17, 2016 @03:27PM (#11716558)
Note that since ES6 imports are statically defined, the browser can find and begin to download dependent files before even parsing the actual JS. They could even start downloading dependent files in parallel before the first file is fully downloaded.

But bundling will still probably be faster and will probably continue to be a mainstay of production web apps, just like minification already is.

Re:Microsoft Edge supports ES6 modules by Arnavion on Tuesday May 17, 2016 @03:33PM (#11716610)
>Note that since ES6 imports are statically defined, the browser can find and begin to download dependent files before even parsing the actual JS.

I think you meant executing the actual JS. Finding the imports requires parsing the file, and imports can be anywhere in the file as long as they're at the top level.

Re:Microsoft Edge supports ES6 modules by domenicd on Tuesday May 17, 2016 @03:41PM (#11716683)
It depends. For HTML all browsers do a form of "preparsing" where they do simplistic string searches for things that look like URLs and start speculatively fetching based on that. You could imagine a similar pass for JS modules.
Re:Microsoft Edge supports ES6 modules by bzbarsky on Wednesday May 18, 2016 @02:37AM (#11720188)
Firefox doesn't do simplistic string searches, for what it's worth. It actually tokenizes the HTML and kicks off loads from tokenization.

If someone then misuses document.write the token stream has to be thrown out and things have to be retokenized, but that's rare in practice. The tradeoff is better accuracy of the preloader and not having to go through the text twice unless someone is misusing document.write.

Re:Microsoft Edge supports ES6 modules by Arnavion on Tuesday May 17, 2016 @04:10PM (#11716967)
Ah, that's cool.
Re:Microsoft Edge supports ES6 modules by MatthewPhillips on Tuesday May 17, 2016 @03:38PM (#11716653)
You still have to fetch a module before you know it's dependencies. So if A depends on B which depends on C, you must fetch those sequentially. With a deep dependency graph this will be a problem.
Re:Microsoft Edge supports ES6 modules by WorldMaker on Tuesday May 17, 2016 @03:58PM (#11716853)
Bundling may not necessarily be faster with HTTP/2 and proper cache controls.
Re:Microsoft Edge supports ES6 modules by MatthewPhillips on Tuesday May 17, 2016 @04:18PM (#11717039)
I don't see a practical difference between bundling and server push to the developer. You are still using tooling that traces your dependency graph and produces... something. In the case of a bundler it produces a concatenated script and in the case of http push it produces a list of files that should be sent with a particular route. To the developer, why should the latter be preferred over the former?
Re:Microsoft Edge supports ES6 modules by rajivm on Tuesday May 17, 2016 @09:48PM (#11719158)
Three reasons off the top of my head: - Smaller cacheable objects -- every change you make doesn't invalidate the whole "bundle". - If there are many permutations of the optimal bundles: different browsers, different pages use different scripts, etc. - The browser does not need to wait for the "full bundle" to download to execute your app/site -- it can start once the first necessary assets are loaded.

In one project I worked on, we had 3 bundles: - early load (inside the head tag) - deferred load secondary (after body tag, for the other site pages) - deferred load primary (after the body tag, for the 'hot' pages of the site) With a optimal http/2.0 setup, we wouldn't need to make these never fully optimal bundles.

Re:Microsoft Edge supports ES6 modules by WorldMaker on Tuesday May 17, 2016 @05:28PM (#11717599)
From a developer side, at least, HTTP/2+caching (with or without server push) is preferential in that the browser handles everything (and knows what it needs) versus bundling which is a build process of some sort.

From a developer perspective, producing the only the dependency graph is still going to be faster and need less overall IO than building a bundle. You can get a sense of it today with jspm dep-cache versus jspm bundle times.

Re:Microsoft Edge supports ES6 modules by mevile on Tuesday May 17, 2016 @04:06PM (#11716923)
Right, with HTTP2 and caching, you'd actually save on bandwidth if you didn't have to deploy a new build whenever an individual file changed.
Re:Microsoft Edge supports ES6 modules by MatthewPhillips on Tuesday May 17, 2016 @02:34PM (#11716097)
Yes, it makes separate requests, like if you made separate script tags (just using module semantics instead). With HTTP2 it will be ok, but you will still need something that understands your dependency graph so your server knows which modules to include with the request.
Re:Microsoft Edge supports ES6 modules by eknkc on Tuesday May 17, 2016 @02:47PM (#11716217)
I guess service workers might solve a lot of issues here. You could do weird stuff like basically serving a tar file for all assets and use a service worker to handle individual requests.
Re:Microsoft Edge supports ES6 modules by MatthewPhillips on Tuesday May 17, 2016 @02:52PM (#11716255)
That's a really smart idea, I hadn't considered that. Once you add in caching you might get pretty reasonable performance.
Re:Microsoft Edge supports ES6 modules by WorldMaker on Tuesday May 17, 2016 @03:54PM (#11716812)
Also, the not yet implemented module loader spec would include a format similar to the AMD bundle format where modules are wrapped in calls to `System.register`.
Re:Microsoft Edge supports ES6 modules by MatthewPhillips on Tuesday May 17, 2016 @04:13PM (#11716997)
There's no such bundling format under consideration, as far as I'm aware. System.register is a SystemJS API, not a spec one.
Re:Microsoft Edge supports ES6 modules by WorldMaker on Wednesday May 18, 2016 @09:45AM (#11721848)
The Loader spec does include the concepts of a module registry and a low level API for them. From what I can tell skimming the specs, the high level construct of System.register has been in and out of the spec. Also from what I can tell, System.register may be possible to be implemented on top of the low level API regardless of whether it makes it into the final spec or not.
Re:Microsoft Edge supports ES6 modules by egeozcan on Tuesday May 17, 2016 @03:10PM (#11716428)
This is an interesting idea. On the other hand, I think if you started packing everything into a tar file, that could as well be a js file.
Re:Microsoft Edge supports ES6 modules by gsnedders on Tuesday May 17, 2016 @03:14PM (#11716459)
> On the other hand, I think if you started packing everything into a tar file, that could as well be a js file.

Modules give you a certain degree of namespacing, which is nice. Compiling down to a tar to be served to a Service Worker is undoubtedly easier than the mangling needed to remove the modules.

Re:Microsoft Edge supports ES6 modules by GundersenM on Tuesday May 17, 2016 @05:37PM (#11717688)
I recently wrote about this, and how it can be solved using HTTP/2 and Service Workers: https://mariusgundersen.net/module-pusher/