webpack使用tree shaking的問題。及關於UglifyJs不支援ES6的解決方案。

來源:互聯網
上載者:User

標籤:web   輸出   esc   var   module   ...   配置   dead code   get   

webpack:

plugins:[ new webpack.optimize.UglifyJsPlugin({  compress:{warning:true}  })       ]

  是的,一些dead code 在打包後會被移除.比如沒用的varible ,function。

但是classes會被UglifyJs作為side effect,然後跳過它。

 

我的TypeScript配置tsconfig.json :

compilerOptions:{   target:‘es5‘,   module:‘es2015‘    .....}

所以~

typescript=>es5 bundle =>UglifyJs  

通過這條途徑,classes是不會被刪除的。

   typescript=>es6 bundle =>UglifyJs

這條途徑不行,UglifyJs不支援直接處理ES6檔案。

 

------------------------------------------

好吧,搞了很久,我才找到babel為webpack提供了babili-webpack-plugin這麼一個外掛程式。

於是用它直接替換 UglifyJs就好了:

npm install babili-wepback-plugin -save-dev    //   babel-core >= 6.19.0

npm install [email protected]      

plugins:[  new require(‘babili-webpack-plugin‘)()   ]

當然,tsconfig.json中target得設定為es2015(按es5輸出,classes還是會存在)。

如此才是真正的tree shaking。

    不過得到的是es6檔案,也就是還得轉一次es5。 - -!

 

webpack使用tree shaking的問題。及關於UglifyJs不支援ES6的解決方案。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.