2013-09-30 79 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