我正在使用RequireJS加載一些模塊。RequireJS:相對於數據主文件加載模塊
我正在使用鏡像CDN提供靜態資產。問題是,模塊相對加載到網站域名,而不是數據,主文件,使模塊加載這樣:
my-website.com/
↳ cdn.com/assets/js/require-main.min.js
↳ my-website.com/assets/js/helper-module.js
↳ my-website.com/assets/js/utility-module.js
有沒有一種辦法相對加載模塊的數據,主文件,以便爲使用CDN的模塊提供服務?
my-website.com/
↳ cdn.com/assets/js/require-main.min.js
↳ cdn.com/assets/js/helper-module.js
↳ cdn.com/assets/js/utility-module.js
我不能硬編碼的CDN域中的任何位置,因爲它並不總是相同的。
這是require-main.js
requirejs.config({
baseUrl: "../js/",
waitSeconds: 15,
paths: {
helper: "helper-module",
...
}
});
的樣本,這是rjs.optimize
功能I中縮小的全部資產,並結合一些模塊
rjs.optimize({
appDir: './assets/js/',
baseUrl: ".",
mainConfigFile: './assets/js/require-main.js',
dir: './public/assets/js/',
preserveLicenseComments: true,
optimize: 'uglify2',
findNestedDependencies: true,
logLevel: 0,
uglify2: {
mangle: false
},
modules: [
{
name: 'require-main'
...
}
],
removeCombined: true,
writeBuildTxt: false
});
你的構建是否應該將所有**模塊合併到一個包中?如果是,那麼問題不在於CDN,而在於RequireJS需要在包之外獲取文件。 – Louis
@Louis我編輯了我的問題:一些核心模塊被組合,但我仍然需要一些未捆綁的額外模塊。 – webpn