Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ts-loader vs babel-loader in Typescript using webpack

So I was trying to compare the output compiled code by these 2 combinations.

ts-loader

  {
    test: /\.tsx?$/,
    use: 'ts-loader',
  }

babel-loader

  use: {
      loader: 'babel-loader',
      options: {
        presets:
          [
            "@babel/preset-react",
            "@babel/preset-typescript",
          ]
      }
    }
  • I get compiling times for babel-loader lower than for ts-loader.
  • Also, ts-loader looks to be using babel under the hood, at least it's in its dependencies.
  • Also, babel-loader allows to use cache with cacheDirectory

Questions

  1. Is there any way to use cache in ts-loader like the cacheDirectory in babel?
  2. Any other benefits in using ts-loader instead of babel-loader?
like image 814
fjplaurr Avatar asked Feb 01 '21 11:02

fjplaurr


People also ask

Does babel-loader work for TypeScript?

By using babel's support for TypeScript, you get the ability to work with existing build pipelines and are more likely to have a faster JS emit time because Babel does not type check your code.

Should you use Babel with TypeScript?

Babel excels at custom transformationsIf you need custom transformations, you'll need to use Babel. The good news is that most TypeScript tools allow you to both use TypeScript and then run the code through Babel afterwards, to get the best of both worlds.

What is the difference between Webpack and Babel?

Babel can be classified as a tool in the "JavaScript Compilers" category, while Webpack is grouped under "JS Build Tools / JS Task Runners".

Do I need to use Babel with Webpack?

If Babel is a translator for JS, you can think of Webpack as a mega-multi-translator that works with all kinds of languages (or assets). For example, Webpack often runs Babel as one of its jobs. Another example, Webpack can collect all your inline CSS styles in your Javascript files and bundle them into one.


Video Answer


1 Answers

for your questions:

  1. As someone already mentioned, there is an experimental setting in ts-loader to allow cache, you can check out configuration reference for more information: https://github.com/TypeStrong/ts-loader#experimentalfilecaching
  2. Afaik babel does not do typechecking on its own, so you would have to run TSC for that. Also it does not support 'const enum' syntax of ts...

Also you can mix the 2, have ts-loader for dev and babel for prod builds.

like image 148
JJWorren Avatar answered Sep 20 '22 13:09

JJWorren