您可能已經知道這一點,但爲此目的使用||
將無法在普通JavaScript中使用。我不知道JavaScript的「擴展」語言(如CoffeeScript),它將允許您提出的內容。
但是,您傳遞給requirejs的配置對象可以動態構造。所以,你可以這樣做:
var config = {
baseURL: "lib",
paths: { ... },
shim: {
moduleA: { ... }
}
};
var s = config.shim;
s.bsAlert = s.bsTooltip = s.bsDropdown = { deps: ['jquery'] };
require.config(config);
如果你將需要做這個有很多,有可能寫這樣一個配置:
var config = {
baseURL: "lib",
paths: { ... },
shim: {
moduleA: { ... }
"bsAlert || bsTooltip || bsDropdown": { deps: ['jquery'] },
}
};
,然後有一個功能走過config對象將形式爲「A || B」的鍵轉換爲requirejs在將對象傳遞給requirejs之前需要的內容。並且由於requirejs在多次配置時會合並配置,因此您可以使用類似的方法:
require.config({
// ... minimal config allowing to find "convert_config"
});
var convert_config = require("convert_config"); // module returns function
var config = {
baseURL: "lib",
paths: { ... },
shim: {
moduleA: { ... },
"bsAlert || bsTooltip || bsDropdown": { deps: ['jquery'] },
}
};
convert_config(config); // modifies object in-place
require.config(config); // pass the full configuration to requirejs