删除RequireJS中未使用的填充程序配置?

时间:2013-09-30 22:17:04

标签: javascript twitter-bootstrap requirejs gruntjs shim

好的,我的问题是我希望能够自动删除我为RequireJS设置的部分shim配置;而不是加载整个缩小的Bootstrap文件,我把它拆分成不同的插件,这样当我的应用程序使用较少的Bootstrap组件时,我可以获得更少文件大小的好处。 E.g:

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的解决方案会更好,但我对其他任何事情都持开放态度。感谢。

1 个答案:

答案 0 :(得分:2)

总结上面提到的重点,并希望关闭这个问题

  • r.js不会删除未使用的填充程序配置,因为它无法在运行时知道是否需要它们
  • ~175字节(gzip)不值得手动优化,特别是作为有效载荷的一部分200倍,并且不会引起明显的改善
  • AMDClean可以是一种完全取消AMD脚本的自动化方式,从而满足您的节省大小的愿望
相关问题