whatwg / loader

Loader Standard
https://whatwg.github.io/loader/
Creative Commons Zero v1.0 Universal
607 stars 45 forks source link

Execute hook #47

Closed matthewp closed 9 years ago

matthewp commented 9 years ago

A hook just prior to execution would be extremely helpful in scenarios where you need to alter the universe for a particular module. Consider something like this:

var execute = loader.execute;
loader.execute = function(key){
  var logger = loader.lookup("logger");

  var log = logger.log;
  logger.log = function(msg){
    console.log(key + ":" + msg);
  };

  var mod = execute.apply(this, arguments);
  logger.log = log;
  return mod;
};

In this example a logger module is redefined so that it prints out the key of the module using it.

This won't work if used async but it's still quite useful.

MajorBreakfast commented 9 years ago

System builder's behavior to not execute modules can be implemented by setting this hook to a NOP. What you're proposing is a generalized version instead of an on/off switch. Seems useful. https://github.com/systemjs/builder/blob/a5d76f63e4362936363de4439274a61d116f486f/lib/builder.js#L69

The instantiate hook is very similar, though. Are both needed?

matthewp commented 9 years ago

Preventing executing for a trace is another use-case, yes.

Instantiate might be able to serve this use case. If you could return an execute function and this applies to dynamic and declarative modules, that would work. The spec is not filled out on instantiate though: http://whatwg.github.io/loader/#browser-instantiate

caridy commented 9 years ago

@matthewp I haven't look at this problem yet (I will), but at the first glance, there is a problem with your example, modifying the result of lookup is not allowed. Maybe you should simplify it to modify some runtime values instead of dealing with the module's dependencies.

matthewp commented 9 years ago

Ah, yes you're right. I'll try to come up with a better example.

caridy commented 9 years ago

@matthewp is this still valid? do you have a better example? Otherwise let's close this.

matthewp commented 9 years ago

I do not for the time being, let's close this and I'll reopen in the future if a good scenario comes up.