Understanding the 'normalize' Loader hook

Ian Hickson ian at hixie.ch
Tue Aug 12 15:26:45 PDT 2014


On Tue, 12 Aug 2014, John Barton wrote:
> On Tue, Aug 12, 2014 at 2:45 PM, Ian Hickson <ian at hixie.ch> wrote:
> > 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).
> 
> I would expect that normalized names to be platform independent.

Sure. I'm talking specifically about the Web platform here.


> >  - "locate" resolves that URL and adds ".js"
> 
> This step is platform dependent, it produces an address from a name.

Right.


> > (only if the last segment is part of the "path" part of the URL and 
> > doesn't contain a "."?).
> 
> No such restriction is applied in our code.

Sure. What do we want for the default Web loader though?


> > Is that more or less right? (There's also the loader.paths stuff. Why 
> > is that in "locate" rather than "normalize"?)
> 
> Because the paths processing can produce arbitrary addresses (to cache, 
> CDN, I guess)

Sure but presumably if someone explicitly puts in the long URL, they still 
don't want it to cause a second load, right?

I mean, if you do:

   loader.paths = {
     'foo': 'scripts/foo',
   };

   // in a different module

   import "foo";

   // in a different module

   import "scripts/foo";

...surely you want just one module loaded. No?

Come to think of it, maybe you actually just want one loaded even if 
someone does:

   import "scripts/foo.js";

...or even:

   // assuming the base URL the whole time has been
   // http://www.example.com/
   import "http://www.example.com/scripts/foo.js";

Is there any time we'd want all those imports to result in separate module 
loads?


> > I'm exploring some ways that the module system could be extended on 
> > the Web platform.
> 
>  You might look at Guy Bedford's experiment on loader plugins
> https://github.com/systemjs/systemjs
>  that follow the path laid down by Jame Burke in requirejs
> http://requirejs.org/docs/api.html#plugins

Same basic idea, yeah. It's unfortunate, IMHO, that we have to have 
special syntax to get something from the "normalize" hook to the "locate" 
hook. It means it gets exposed in the "name" of the Module, and it means 
that if your syntax has any redundancy (e.g. the way the loader plugins 
SystemJS can infer the plugin type before the "!" or have it explicitly 
given after the "!") you can end up with duplicates you didn't intend.

-- 
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