RequireJS の最近のバージョンでは、プレーンな JS ファイルが、実際には何も返さない AMD モジュールであるかのように見せかけることができます。
私が試した最新バージョンの 2.1.4 では、プレーンな JS ファイルをモジュールのように扱うことができます。例:
require(
[
'path/to/module' // <- AMD module
,'path/to/plainjs' // <- actually a plain JS file
]
, function(module, plain){
// module will be per define in that file.
// plain will be 'undefined' type
}
)
モジュールのような参照をプレーンな JS ファイルに自由に混在させることができます。それらが正しい順序でロードされている限り、更新するグローバルが更新され、必要なものが得られます。例:
require(['js/underscore'], function(){
// nesting to insure Underscore, a prereq to BackBone
// completes loading before backbone starts
require(
[
'path/to/module' // <- AMD module
,'js/backbone' // <- actually a plain JS file
]
, function(module){
// module will be per define in that file.
window.BackBone // is available for you
}
)
})
RequireJS では、プレーン JS ファイルの最後に「.js」を追加してプレーン JS であることを示す必要がありましたが、上記の例では「.js」を使用していないことに注意してください。この拡張子のないモジュール参照により、従うモジュール IDpaths
とmaps
エイリアス。ID.js
はリテラルとして扱われ、変換されません。