2

js ファイルのモジュール名を作成/定義するためだけに、requirejs オプティマイザを使用したいと考えています。

例:modules/moduleAの下にtest.jsというファイルがある場合

modules
|
|
-----moduleA
     |
     -----test.js

パスに基づいてrequirejsオプティマイザーにモジュール名だけを作成してもらいたい...たとえば、以下では、requirejsオプティマイザーにモジュール名modules/moduleA/testを作成させ、test.jsに存在する定義内に配置してもらいたい

例: requireJS オプティマイザーを実行する前に

test.js コンテンツ:

define( ["my/cart", "my/inventory"],
            function(cart, inventory) {
               ...
           }
        );

requirejs オプティマイザーの実行後

test.js コンテンツ

define("modules/moduleA/test",
            ["my/cart", "my/inventory"],
            function(cart, inventory) {
               ...
           }
        );

関連する依存ファイルを後で grunt を使用して結合する処理を行います...しかし、requirejs オプティマイザーが各ファイルにアクセスし、それが提示するパスに基づいてモジュール名を提供するようにします。

これは実現可能ですか?

4

1 に答える 1

0

Requirejs の作成者である James Burke は、別のフォーラムでこれに対する回答を提供しました。

これは、RequireJS の変換を使用して簡単に実行できます。

var requirejs = require('requirejs');
requirejs.tools.useLib(function(require) {
  require(['transform'], function (transform) {
     //todo: generate a files array with the list of files to concatenate.

      files.forEach(function(filePath) {
        var fullPath = path.join(targetDir, filePath);
        var contents = fs.readFileSync(fullPath, 'utf8');
        var moduleId = /* todo, figure out what the moduleId is for the module */
        var transformed = transform
                          .toTransport('', moduleId, fullPath, contents);

        //todo: concat the contents together
      });
    });
  });
});
于 2015-09-11T18:01:38.173 に答える