2013-09-30 37 views
2

好的,所以我的问题是我希望能够自动删除我为RequireJS设置的shim配置的一部分;而不是加载整个缩小的Bootstrap文件,我将其分解到不同的插件中,以便在应用程序使用较少的Bootstrap组件时获得较少文件大小的好处。 E.g:在RequireJS中删除未使用的shim配置文件?

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,它仍包括不生产代码的最终文件的垫片配置。所以我的问题是我可以自动摆脱未使用的垫片文件?我正在使用grunt-contrib-requirejs进行实际优化,并且没有任何问题。一个基于Grunt的解决方案将是可取的,但我对任何其他方面都是开放的。谢谢。

+0

只是好奇......为什么?是不是只有12(或更少)行 – explunit

+0

和〜700字节的代码,是的,但在一个约550KB(引导,requirejs,下划线,主干,d3,jquery和自定义代码)的“优化”可以做的减少文件大小是值得做的。 – Ben

+0

因此,为了减少你的代码,为什么不从每个jQuery插件中移除'exports'?对我来说似乎没用。 – gustavohenke

回答

2

总结上面作出了巨大的点,并希望关闭问题

  • 未使用的垫片CONFIGS不会被删除r.js,因为它不知道,如果你需要他们在运行时
  • 〜175字节(gzipped)不值得手动优化,尤其是作为有效负载200倍的一部分,并且不会导致明显的改善
  • AMDClean可能是一种自动化的方式来彻底解除您的脚本的性能,从而满足您的需求大小节省的欲望
+0

感谢您写这篇文章。 :-) – Ben