It turns out that RequireJS optimization support does not apply to all Webjars, but rather limited to Classic Webjars.
Even then, a webjar build file has to be included with the regular module in order for rjs to work.
If you look at the jQuery classic webjar, for example, you will see that a special webjar build instruction is included. Take a look at that file for your information.
一旦你确定了一个支持 RequireJS 的 webjar,你就可以让 sbt-rjs 来做这件事。这是我的设置供参考:
/** javascripts/main.js **/
'use strict';
requirejs.config({
paths:{
'jquery': ['../lib/jquery/jquery'],
'react': ['../lib/react/react'],
'bootstrap': ['../lib/bootstrap/js/bootstrap'],
'react-bootstrap': ['../lib/react-bootstrap/react-bootstrap']
},
shim: {
'bootstrap': {
deps: ['jquery']
},
'react-bootstrap': {
deps: ['react']
}
}
});
记住要有方括号,否则CDN替换不会发生。
对于非 requirejs 就绪的脚本,在声明时不应该有方括号paths
。否则,rjs 将拒绝构建并出现错误path fallback not supported
。当然,您不会获得 CDN 的好处。
顺便说一句,RequireJS css 优化也有效。但仅限于内联 css,就像常规的 Requirejs 那样。