<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Manuel Beaudru</title>
    <description>The latest articles on DEV Community by Manuel Beaudru (@mbeaudru).</description>
    <link>https://dev.to/mbeaudru</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F129026%2Fd015f331-2cfc-456c-b3f2-54465da61942.jpeg</url>
      <title>DEV Community: Manuel Beaudru</title>
      <link>https://dev.to/mbeaudru</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/mbeaudru"/>
    <language>en</language>
    <item>
      <title>Is babel still relevant for TypeScript projects ?</title>
      <dc:creator>Manuel Beaudru</dc:creator>
      <pubDate>Wed, 02 Jun 2021 18:03:25 +0000</pubDate>
      <link>https://dev.to/mbeaudru/is-babel-still-relevant-for-typescript-projects-36a7</link>
      <guid>https://dev.to/mbeaudru/is-babel-still-relevant-for-typescript-projects-36a7</guid>
      <description>&lt;p&gt;Since TypeScript has the ability to generate a JavaScript bundle using the TypeScript compiler, are there still reasons to bother using babel and webpack when you want to create a new ts-based project ?&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Unsatisfying answers&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;My 5min google search on the topic didn't give me satisfying answers:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;on reddit, there are many voices arguing that the &lt;code&gt;tsc&lt;/code&gt; is enough (&lt;a href="https://www.reddit.com/r/typescript/comments/d80xz4/is_babel_redundant_if_typescript_is_involved/"&gt;1&lt;/a&gt;, &lt;a href="https://www.reddit.com/r/typescript/comments/6pdcy2/transpiling_typescript_right_to_es5_or_combined/"&gt;2&lt;/a&gt;)&lt;/li&gt;
&lt;li&gt;but most project scaffolding tools (create-react-app, nextjs, tsdx to name a few) still rely on babel + webpack or rollup.&lt;/li&gt;
&lt;li&gt;Besides, TypeScript folks themselves state that "TypeScript code is transformed into JavaScript code via the TypeScript compiler &lt;strong&gt;or&lt;/strong&gt; Babel" (&lt;a href="https://www.typescriptlang.org/"&gt;1&lt;/a&gt;), or that "You might use [babel] for speed or consistency with your existing build tooling"(&lt;a href="https://www.typescriptlang.org/download"&gt;2&lt;/a&gt;).&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Why is that ? Should you solely rely on &lt;code&gt;tsc&lt;/code&gt; at your company or on your next pet project ?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;I've done the work for you, so let's dive in 🙂&lt;/p&gt;

&lt;p&gt;👉 Want to see more ? &lt;a href="https://twitter.com/mbeaudru"&gt;Follow me on Twitter&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  TypeScript is a self-sufficient build tool
&lt;/h2&gt;

&lt;p&gt;The TypeScript compiler has the ability to produce a JavaScript bundle. It is capable of generating an &lt;a href="https://www.typescriptlang.org/tsconfig#target"&gt;"older" JS for specific targets&lt;/a&gt; like ES5 or specific nodeJS versions.&lt;/p&gt;

&lt;p&gt;For instance, &lt;a href="https://gist.github.com/mbeaudru/24055dd6042e90d95642c5fc48856454"&gt;I created the most minimalistic TypeScript project&lt;/a&gt; without configuring any options, and this is what I got:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Input (index.ts):&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;helloWorld&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;hello world&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Output (index.js):&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;use strict&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;helloWorld&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;hello world&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://gist.github.com/mbeaudru/881046f1c03467a2bf2e85007d8bb178"&gt;TypeScript has generated a default tsconfig.json&lt;/a&gt; that targets ES5, so the arrow function has been transpiled to a regular function and the &lt;code&gt;const&lt;/code&gt; has been changed for a &lt;code&gt;var&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;As you can see, &lt;code&gt;tsc&lt;/code&gt; has done a pretty good job at transpiling the code. Indeed, it is a legit transpiler and it might be enough for you. Besides, we didn't needed to get webpack into the mix and it was very easy to set up.&lt;/p&gt;

&lt;p&gt;At this point you might be wondering, what does the TypeScript compiler is missing that Babel isn't ?&lt;/p&gt;

&lt;h2&gt;
  
  
  Babel comes with many benefits
&lt;/h2&gt;

&lt;p&gt;If you look at the surface, indeed Babel and TypeScript are both capable of transpilation, but babel is capable of covering a much broader range of scenarios and the power to fine-tune what code is generated.&lt;/p&gt;

&lt;h3&gt;
  
  
  Optimized build for legacy browsers and modern browsers 🚀
&lt;/h3&gt;

&lt;p&gt;With Babel + &lt;a href="https://babeljs.io/docs/en/babel-preset-env"&gt;babel-preset-env&lt;/a&gt;, you have more control on what your target is. For instance you can target every browser above IE11, or &amp;gt; 0.25% of browsers that are not dead.&lt;/p&gt;

&lt;p&gt;Going further, you can even generate two bundles: one for legacy browsers, and one for modern browsers that will be much lighter &amp;amp; easy to process.&lt;/p&gt;

&lt;p&gt;You can learn more about this topic on the two following articles, that I particularly enjoyed:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://www.smashingmagazine.com/2018/10/smart-bundling-legacy-code-browsers/"&gt;Smart Bundling: How to serve legacy code only to legacy browsers&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://medium.com/@suncommander/differential-serving-serve-legacy-code-to-old-browsers-and-es6-code-to-modern-browsers-bbd4152cf979"&gt;Differential Serving — Serve legacy code to old browsers and ES6 code to modern browsers&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Tree-shaking and Quality of life improvements ⛱️
&lt;/h3&gt;

&lt;p&gt;Let's dive in into the TSDX case:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;TSDX is a zero-config CLI that helps you develop, test, and publish modern TypeScript packages&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;I find this example very interesting, because this tool is solely focused on generating TypeScript packages, but they still use babel anyway.&lt;/p&gt;

&lt;p&gt;In a nutshell, they use babel to:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Generate an optimized production bundle, that will remove development specific instructions like this:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;__DEV__&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;foo&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Prevent you from accidentally import all lodash by rewriting the imports at transpile time + changes lodash to lodash-es to make your library treeshakable to end consumers.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  A rich and powerful plugin ecosystem 📦
&lt;/h3&gt;

&lt;p&gt;For instance, &lt;a href="https://styled-components.com/docs/tooling#babel-plugin"&gt;styled-components provides a babel-plugin&lt;/a&gt; to &lt;code&gt;add support for server-side rendering, minification of styles, and a nicer debugging experience.&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;In this particular case, there is &lt;a href="https://github.com/Igorbek/typescript-plugin-styled-components"&gt;an alternative to make it work without babel&lt;/a&gt;, but:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;it's not as feature-rich as the official babel plugin (&lt;a href="https://github.com/Igorbek/typescript-plugin-styled-components#minify"&gt;ref&lt;/a&gt;)&lt;/li&gt;
&lt;li&gt;it requires to use webpack in combination of the ts compiler, since the tsc doesn't allow plugins to apply code transformers (&lt;a href="https://github.com/Microsoft/TypeScript/issues/16607"&gt;ref&lt;/a&gt;)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Besides, this plugin is one of the most popular ones so there is a babel alternative. But you are not guaranteed to have the same escape-hatch for &lt;a href="https://github.com/jgierer12/awesome-babel-macros"&gt;other transformers&lt;/a&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  Retro-Compatibility 📼
&lt;/h3&gt;

&lt;p&gt;For instance, create-react-app and NextJS still need to support non-TypeScript powered projects (ex: JavaScript or Flow), it makes sense for those tools not to center their entire system around the TypeScript compiler.&lt;/p&gt;

&lt;p&gt;For the same reason, it's also easier in a legacy JavaScript codebase built with babel+webpack to migrate to TypeScript incrementally and with confidence, since it will only consist of adding the &lt;code&gt;@babel/preset-typescript&lt;/code&gt; to the mix for &lt;code&gt;.ts&lt;/code&gt; and &lt;code&gt;.tsx&lt;/code&gt; files.&lt;/p&gt;

&lt;p&gt;You won't need to "remove" babel from your pipeline and touch your existing configuration.&lt;/p&gt;

&lt;h3&gt;
  
  
  Build-time performance ⚡
&lt;/h3&gt;

&lt;p&gt;When using babel to transpile your TS into JS, there is &lt;em&gt;no type-checking that is performed on the TypeScript codebase&lt;/em&gt;: babel simply "removes" every TS-specific instructions and only keeps the JS, then applies its transformations.&lt;/p&gt;

&lt;p&gt;This can arguably be seen as a downside, since TS code with invalid typing will be built without crashing or warning you of issues.&lt;/p&gt;

&lt;p&gt;But you can see it as an upside: you can &lt;strong&gt;run &lt;code&gt;tsc&lt;/code&gt; in a process just for the type-checking phase&lt;/strong&gt;, and &lt;strong&gt;babel in a parallel process&lt;/strong&gt; to generate the build.&lt;/p&gt;

&lt;p&gt;🔥 Besides, there are babel alternatives growing up like &lt;a href="https://swc-project.github.io/docs/installation/"&gt;SWC&lt;/a&gt; or &lt;a href="https://github.com/alangpierce/sucrase"&gt;Sucrase&lt;/a&gt; that are heavily focused on compilation speed and are much faster than the &lt;code&gt;tsc&lt;/code&gt; transpiler or &lt;code&gt;babel&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;TypeScript compiler is capable of transpiling your code, but it is not as flexible, powerful and complete as Babel.&lt;/p&gt;

&lt;p&gt;If you are starting a new project from scratch, you're probably better of using a generator like &lt;a href="https://nextjs.org/"&gt;NextJS&lt;/a&gt; for a web app or &lt;a href="https://tsdx.io/"&gt;TSDX&lt;/a&gt; to build a TS library. TS doc suggests the same thing, the &lt;a href="https://www.typescriptlang.org/docs/bootstrap"&gt;Bootstrap page&lt;/a&gt; is very useful in that regard.&lt;/p&gt;

&lt;p&gt;Such tools are making the choice and configuration of the transpiler behind the scenes, and most of them use Babel for all the reasons we saw together.&lt;/p&gt;

&lt;p&gt;And if you're a company that wants to have all the controls on the tooling, my advice would be to &lt;strong&gt;use TypeScript compiler solely for the type-checking phase and &lt;a href="https://babeljs.io/"&gt;Babel&lt;/a&gt; or &lt;a href="https://swc-project.github.io/docs/installation/"&gt;SWC&lt;/a&gt; for the transpilation phase&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Indeed, babel gives you the fine-tuning control you might need, while SWC gives you the speed if you don't need what babel provides.&lt;/p&gt;

&lt;p&gt;What do you use in your project/company ? Do you have a use case I didn't cover that you want to highlight ? Don't hesitate to react in the comments and I'll gladly respond 👍&lt;/p&gt;

</description>
      <category>typescript</category>
      <category>javascript</category>
      <category>babel</category>
      <category>webpack</category>
    </item>
  </channel>
</rss>
