我使用Minify来合并和缩小我的 JS,在我的主干应用程序的 index.html 中,我只有一个这样的代码块:
if (APP._dev === true) {
// So on development, we load all the files individually
$.when(
// Utilities
$.getScript(APP._rootDir + 'app/js/util.js'),
// Models
$.getScript(APP._rootDir + 'app/js/models/Timer.js'),
$.getScript(APP._rootDir + 'app/js/models/Section.js'),
// Collections
$.getScript(APP._rootDir + 'app/js/collections/Timers.js'),
$.getScript(APP._rootDir + 'app/js/collections/Sections.js'),
// Views
$.getScript(APP._rootDir + 'app/js/views/TitleView.js'),
$.getScript(APP._rootDir + 'app/js/views/BackgroundAudioView.js'),
// Router
$.getScript(APP._rootDir + 'app/js/routers/APPRouter.js'),
// Load in our templates file and add each template to a
// proerty visible in our application scope
$.get(APP._rootDir + 'app/js/templates/all.html',function(result){
var tmp = $('<div></div>').html(result);
$('script.template', tmp).each(function(){
APP._templates[this.id] = $(this).html();
});
},'html'),
// Here we create a new deferred object which we will bind
// to JQuery's ready() event.
$.Deferred(function (deferred) {
// DOM Ready. Invokes our APP.init() function.
$(deferred.resolve); // init
})
).done(
APP.init
).fail(
function () { /* fail how you would like */ }
);
} else {
$.when(
// And on prod, we load a combined & minified JS file instead
$.getScript(APP._rootDir + 'min/?g=js'),
// Load in our templates file and add each template to a
// proerty visible in our application scope
$.get(APP._rootDir + 'app/js/templates/all.html',function(result){
var tmp = $('<div></div>').html(result);
$('script.template', tmp).each(function(){
APP._templates[this.id] = $(this).html();
});
},'html'),
// Here we create a new deferred object which we will bind
// to JQuery's ready() event.
$.Deferred(function (deferred) {
// DOM Ready. Invokes our APP.init() function.
$(deferred.resolve); // init
})
).done(
APP.init
).fail(
function () { /* fail how you would like */ }
);
}
然后,在我的 Minify/min/
目录中的 groupsConfig 中,我定义我的构建数组以匹配 jQuery 的延迟对象加载顺序:
return array(
'js' => array(
'//' . $rootDir . '/util.js',
'//' . $rootDir . '/models/Timer.js',
'//' . $rootDir . '/models/Section.js',
'//' . $rootDir . '/collections/Timers.js',
'//' . $rootDir . '/collections/Sections.js',
'//' . $rootDir . '/views/TitleView.js',
'//' . $rootDir . '/views/BackgroundAudioView.js',
'//' . $rootDir . '/routers/APPRouter.js'
),
'css' => array(),
);
它可能不像它可能的那样 DRY,但它可以工作,您可以根据需要对其进行调整。