0

現在のすべてのモジュール ローダーはAMD,CommonJS,SystemJS、外部オブジェクトを現在のモジュール スコープにロードするために変数定義を使用するのと同様です

お気に入り:

var something = require('something');

また:

define('module',['something'],function(something){});

外部モジュールから何をインポートする必要があるのか​​ わからない場合、またはすべてをインポートする必要がある場合、実行時に変数を定義できないため、これが問題になります。

これが ES6 トランスレータが使用しない主な理由だと思います

import * from 'something';

構文であり、これらは ES6 仕様には含まれていません。

したがって、動的モジュールスコープとは、モジュール変数を実行時に定義/ロードできることを意味します。これにより、ES6 構文を次のように拡張できます。

import * from 'something';
import Something.* from 'something';
import /regexp/ from 'something';

私の見解では、これは、次のようなすべての名前をリストするよりも、インポートを定義するためのより最適な方法です。

import {
    ONE,
    TWO,
    ...
} from 'something';

今私の本当の質問:

withこれを達成するために使用しないのはなぜですか?

問題を解決できる ES6 から ES5 への簡単な変換例を次に示します。

ファイル: modules/module-1.js

var localVar = 'VALUE';
function localFun(a,b){
    return a +' and '+ b;
}
export {
    localVar as module1Var
    localFun as module1Fun
}

に:

(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
                // ...
            };
        });
    }
})();

ファイル: modules/module-1.js

import * from 'modules/module-1.js';

console.info(module1Fun(module1Var)); //prints: VALUE and VALUE

に:

(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                
        });
    }
})();

withトランスパイラー/ローダーでも避ける必要は本当にありますか?

私は別の ES6to5 トランスレータとモジュール ローダーを作成することを考えているので、この人たちについてのあなたの考えに感謝します。:)

4

2 に答える 2