All current module loaders like AMD,CommonJS,SystemJS
using variable definitions for loading external objects into current module scope
like:
var something = require('something');
or:
define('module',['something'],function(something){});
In case when you don't know what you need to import from external module, or just need to import everything, this becoming issue, because impossible to define variable in runtime.
I guess this is the main reason why ES6 translators not using
import * from 'something';
syntax and they are not included this in ES6 Spec.
So by saying dynamic module scope I mean that module variables can be defined/loaded runtime, which will allow to extend ES6 syntax to something like:
import * from 'something';
import Something.* from 'something';
import /regexp/ from 'something';
In my view this is more optimal way to define imports, rather then to list all names like:
import {
ONE,
TWO,
...
} from 'something';
Now my real question:
why to do not use
with
to achieve this ?
Here is simple example translations from ES6 to ES5 that can solve problem:
file: modules/module-1.js
var localVar = 'VALUE';
function localFun(a,b){
return a +' and '+ b;
}
export {
localVar as module1Var
localFun as module1Fun
}
to:
(function(){
// define module named 'modules/module-1'
// and return related scope object
// containing 'execute_module' function predefined
with (define_module('modules/module-1')) {
// will register actual module execution
// which will be called after all imports
// initialized
execute_module(function(){
var localVar = 'VALUE';
function localFun(a,b){
return a +' and '+ b;
}
// returns value as current module exports
return {
module1Var : localVar,
module1Fun : localFun
// ...
};
});
}
})();
file: modules/module-1.js
import * from 'modules/module-1.js';
console.info(module1Fun(module1Var)); //prints: VALUE and VALUE
to:
(function(){
// define module named 'modules/module-2'
// after execute is called loader will do
// all imports and bind exports from modules
// to current module scope and then invoke callback
// after which imported variables will appear in with scope
// and will be visible in callback scope.
with (define_module('modules/module-2',{
'modules/module-1' : '*',
// also can filter exports by regexp
'modules/other' : /.*/
})) {
execute_module(function(){
console.info(module1Fun(module1Var)); //prints: VALUE and VALUE
});
}
})();
is it really necessary to avoid
with
even in transpilers/loaders ?
I will appreciate your thoughts on this guys, because I'm thinking about writing another ES6to5 translator and module loader. :)