Understanding the 'normalize' Loader hook

Ian Hickson ian at hixie.ch
Tue Aug 12 14:45:19 PDT 2014

On Tue, 12 Aug 2014, John Barton wrote:
> See the implemention in es6-module-loader:
> https://github.com/ModuleLoader/es6-module-loader/blob/master/lib/system.js#L117

Ah, fascinating. So basically:

 - "normalize" does relative URL logic based on the current base URL and 
   the referrer (i.e. the URL of the script doing the importing).

 - "locate" resolves that URL and adds ".js" (only if the last segment 
   is part of the "path" part of the URL and doesn't contain a "."?).

Is that more or less right? (There's also the loader.paths stuff. Why is 
that in "locate" rather than "normalize"?)

(BTW, I noticed that both in that code above and in the spec, "referrer" 
is consistently misspelt as "referer", the HTTP way.)

I'm exploring some ways that the module system could be extended on the 
Web platform. One thing that some people have asked for is ways to 
declaratively import stylesheets and the like from inside script, as in:

   import sprites from "images.png";

...where sprites gives you access to an Image element (<img>).

For some cases, e.g. style sheets, you really want to provide more 
information than just a name, so we'd presumably have the import statement 
use an element's ID to do the lookup, as in:

   <link rel=stylesheet href="foo.css" id=foo-styles ...>

   import styles from "foo-styles"; // gets us a reference to the <link> 
                                    // element somehow

Presumably, the way this works is the "locate" hook returns the actual 
HTMLLinkElement. But what does the "normalize" hook return? It can't 
return a URL, since then "locate" and "fetch" won't know to not fetch 
anything. Is there any way to return out-of-band data with the normalized 
name? (Really I think "normalize" and "locate" should probably be one 
hook, which returns either a URL or an object. Having them split, 
especially with the caching happening off a key in the middle, is quite 
difficult to map to the Web platform.)

One option that jorendorff suggested in #whatwg is that if you want to 
refer to a local ID, you use "#foo", as in:

   import styles from "#foo-styles";

This kind of approach has historically been problematic (see the history 
of usemap="" for example), but I don't see a better solution.

Ian Hickson               U+1047E                )\._.,--....,'``.    fL
http://ln.hixie.ch/       U+263A                /,   _.. \   _\  ;`._ ,.
Things that are impossible just take longer.   `._.-(,_..'--(,_..'`-.;.'

More information about the es-discuss mailing list