2018年9月2日:开源日报第178期

Themainfieldofthepackage。

jsonfileisusuallyusedtopointtotheUMDversionofthelibrary/package。

Youmightbewondering?

—?

howcanIreleasetheESversionofmylibrary/package?

Themodulefieldofthepackage。

jsonisusedtopointtotheESversionofthelibrary/package。

Previously,manyfieldswereusedlikejs:nextandjs:main?

butmoduleisnowstandardizedandisusedbybundlersasalookupfortheESversionofthelibrary/package。

Lesswell-knownfact:Webpackusesresolve。

mainfieldstodeterminewhichfieldsinpackage。

jsonarechecked。

PerformanceTip:AlwaystrytopublishtheESversionofyourlibrary/packageaswell,becauseallthemodernbrowsersnowsupportESmodules。

Soyoucantranspileless,andultimatelyyou’llendupshippinglesscodetoyourusers。

Thiswillboostyourapplication’sperformance。

Sonowwhat’snext?

TranspilationorBundling?

Whattoolsshouldweuse?

Ah,herecomesthetrickiestpart!

Let’sdivein?

WebpackvsRollupvs?

Babel?

Theseareallthetoolsweuseinourdaytodaylivestoshipourapplications/libraries/packages。

Icannotimaginemodernwebdevelopmentwithoutthem?

—?

#blessed。

Therefore,wecannotcomparethem,sothatwouldbethewrongquestiontoask!

Eachtoolhasit’sownbenefitsandservesdifferentpurposebasedonyourneeds。

Let’slookateachofthesetoolsnow:

Webpack

Webpackisagreatmodulebundler?

thatiswidelyacceptedandmostlyusedforbuildingSPAs。

Itgivesyouallthefeaturesoutoftheboxlikecodesplitting,asyncloadingofbundles,treeshaking,andsoon。

ItusestheCommonJSmodulesystem。

PS:Webpack-4。

0。

0alphaisalreadyout?

Hopefullywiththestablereleaseitwillbecometheuniversalbundlerforalltypesofmodulesystems。

RollupJS

RollupisalsoamodulebundlersimilartoWebpack。

However,themainadvantageofrollupisthatitfollowsnewstandardizedformattingforcodemodulesincludedintheES6revision,soyoucanuseittobundletheESmodulevariantofyourlibrary/package。

Itdoesn’tsupportasyncloadingofbundles。

Babel

BabelisatranspilerforJavaScriptbestknownforitsabilitytoturnES6codeintocodethatrunsinyourbrowser(oronyourserver)today。

Rememberthatitjusttranspilesanddoesn’tbundleyourcode。

Myadvice:useRollupforlibrariesandWebpackforapps。

Transpile(Babel-ify)thesourceorBundle?

it

Againthere’sastorybehindthisone?