yepnope.js 是一支小型的條件式資源載入器,核心精神是「用到才載、不支援就載另一份」。它體積小(gzip 後約 1.6K)、能載 CSS 也能載 JS,並已整合進 Modernizr。
yepnope({
test : Modernizr.geolocation,
yep : 'normal.js',
nope : ['polyfill.js', 'wrapper.js']
});
意思是:若瀏覽器支援 geolocation 就載 normal.js,不支援則載 polyfill.js 與 wrapper.js。
yepnope([{
test : /* boolean(ish) 條件判斷 */,
yep : /* array | string,條件為真時載入 */,
nope : /* array | string,條件為假時載入 */,
both : /* array | string,不論真假都載入 */,
load : /* array | string,同 both */,
callback: /* function (testResult, key) | object { key: fn } */,
complete: /* function,全部載入完成後執行 */
}, ...]);
體積小、可載 CSS 與 JS、通過作者能找到的瀏覽器測試、把「載入」與「執行」徹底分離(因此你能控制執行順序)、API 友善、模組化可自行擴充、鼓勵按需載入、預設依你提供的順序執行。
其中最實用的是資源回退:主來源失敗時只需補載備用檔案,不會連帶影響依賴它的其他腳本。
yepnope([
{
load: 'https://ajax.googleapis.com/ajax/libs/jquery/1.5.1/jquery.min.js',
complete: function () {
if (!window.jQuery) {
yepnope('local/jquery.min.js'); // CDN 掛了就走本地
}
}
},
{
load: 'jquery.plugin.js',
complete: function () {
jQuery(function () {
jQuery('div').plugin();
});
}
}
]);
相比之下,其他載入器往往得把依賴關係寫成巢狀:
someLoader('https://ajax.googleapis.com/ajax/libs/jquery/1.5.1/jquery.min.js', function(){
if (!window.jQuery) {
someLoader('local/jquery.min.js', 'jquery.plugin.js');
} else {
someLoader('jquery.plugin.js');
}
});
並非永遠最快,經最佳化後 labjs 等方案可能更快;需要為資源設定適當的快取標頭;功能上僅止於載入器,不像 RequireJS 那樣有自建工具與完整 API;預設依序執行,某些情境下會犧牲一點速度。
// 傳字串
yepnope('/url/to/your/script.js');
// 傳物件
yepnope({ load: '/url/to/your/script.js' });
yepnope({
test : window.JSON,
load : '/url/to/your/script.js',
callback: function (url, result, key) {
alert('script.js has loaded!');
}
});
參數中 url 是檔名、result 是 test 的結果、key 是使用 key mapping 時的縮寫名稱。
yepnope({
test : window.JSON,
nope : 'json2.js',
complete: function () {
var data = window.JSON.parse('{ "json": "string" }');
}
});
yepnope({
test : Modernizr.geolocation,
yep : { 'rstyles': 'regular-styles.css' },
nope : { 'mstyles': 'modified-styles.css', 'geopoly': 'geolocation-polyfill.js' },
callback: function (url, result, key) {
if (key === 'geopoly') {
alert('This is the geolocation polyfill!');
}
}
});
callback 也可以改成以 key 為屬名的物件,讓每個檔案各自配一個回呼:
yepnope({
test : Modernizr.geolocation,
yep : { 'rstyles': 'regular-styles.css' },
nope : { 'mstyles': 'modified-styles.css', 'geopoly': 'geolocation-polyfill.js' },
callback: {
'rstyles': function (url, result, key) { alert('This is the regular styles!'); },
'mstyles': function (url, result, key) { alert('This is the modified styles!'); },
'geopoly': function (url, result, key) { alert('This is the geolocation polyfill!'); }
},
complete: function () {
alert('Everything has loaded in this test object!');
}
});
css! — 把任意副檔名的資源當 CSS 載入:
yepnope('css!mystyles.php?version=1532');
preload! — 只預先放進快取,不執行,等真正載入時才跑:
yepnope({
load: 'preload!jquery.1.5.0.js',
callback: function (url, result, key) {
window.jQuery; // undefined
yepnope('jquery.1.5.0.js');
window.jQuery; // object
}
});
ie! — 只在特定 IE 版本載入,除 ie! 外還支援 ie5、ie6、ie7、ie8、ie9、iegt5~iegt8、ielt7~ielt9 等寫法:
yepnope({
load: ['normal.js', 'ie6!ie7!ie-patch.js'] // 僅 IE6 或 IE7 載入
});
條件式載入的代價是「執行時點被推遲」。若某支腳本必須在任何使用者互動之前就位,用它反而容易出現時序問題。另外 yepnope 已停止維護,新專案若需要條件載入,可考慮 SystemJS、動態 import() 或打包工具提供的 dynamic import 機制。