【发布时间】:2013-10-06 22:39:23
【问题描述】:
好的,所以我的问题是我希望能够自动删除我为 RequireJS 设置的 shim 配置的一部分;我没有加载整个缩小的 Bootstrap 文件,而是将其拆分为不同的插件,这样当我的应用程序使用较少的 Bootstrap 组件时,我就可以从文件大小减少中受益。例如:
require.config({
paths : {
jquery : 'vendor/jquery/jquery.min',
bootstrap : 'vendor/bootstrap-sass/js'
},
shim : {
'bootstrap/affix': { deps: ['jquery'], exports: '$.fn.affix' },
'bootstrap/alert': { deps: ['jquery'], exports: '$.fn.alert' },
'bootstrap/button': { deps: ['jquery'], exports: '$.fn.button' },
'bootstrap/carousel': { deps: ['jquery'], exports: '$.fn.carousel' },
'bootstrap/collapse': { deps: ['jquery'], exports: '$.fn.collapse' },
'bootstrap/dropdown': { deps: ['jquery'], exports: '$.fn.dropdown' },
'bootstrap/modal': { deps: ['jquery'], exports: '$.fn.modal' },
'bootstrap/popover': { deps: ['jquery'], exports: '$.fn.popover' },
'bootstrap/scrollspy': { deps: ['jquery'], exports: '$.fn.scrollspy' },
'bootstrap/tab': { deps: ['jquery'], exports: '$.fn.tab' },
'bootstrap/tooltip': { deps: ['jquery'], exports: '$.fn.tooltip' },
'bootstrap/transition': { deps: ['jquery'], exports: '$.support.transition' },
}
});
虽然r.js 优化器正确识别出我只使用bootstrap/dropdown,但它仍然包含未出现在生产代码中的文件的填充程序配置。所以我的问题是我可以自动摆脱未使用的 shim 文件吗?我正在使用grunt-contrib-requirejs 进行实际优化,并且对此没有任何问题。基于 Grunt 的解决方案会更好,但我对其他任何事情都持开放态度。谢谢。
【问题讨论】:
-
只是好奇...为什么?不就是 12 行(或更少)吗
-
还有大约 700 字节的代码,是的,但是在大约 550KB 的“优化”JavaScript 有效负载中(引导程序、requirejs、下划线、主干、d3、jquery 加上自定义代码)我可以做任何事情来减少文件大小值得做。
-
那么,为了稍微减少代码,为什么不从每个 jQuery 插件中删除
exports呢?对我来说似乎没用。 -
我把它放进去是因为这个线程:stackoverflow.com/questions/13377373/… 说我需要定义一个导出变量。好的,这样会大大减少代码,有没有办法按照我原来的问题摆脱它的其余部分? ;-)
-
可能想看看这个:github.com/gfranko/amdclean 它将消除所有 AMD 配置过载,您需要在代码中限制一些 AMD 模式,但它可能符合您的需要。
标签: javascript twitter-bootstrap requirejs gruntjs shim