Giter Site home page Giter Site logo

[Question] ES5 about gatsby-typescript HOT 2 CLOSED

d4rekanguok avatar d4rekanguok commented on May 31, 2024
[Question] ES5

from gatsby-typescript.

Comments (2)

ricokahler avatar ricokahler commented on May 31, 2024 1

This plugin actually combines with the default "JS loader" that Gatsby uses. If you're unfamiliar, Gatsby uses webpack behind the scenes and we simply add the ts-loader in the chain before Gatsby's default JS loader.

test: /\.tsx?$/,
exclude: /node_modules/,
use: [
jsLoader,
{
loader: requireResolve('ts-loader'),
options: {
...tsLoader,
// use ForkTsCheckerWebpackPlugin for typecheck
transpileOnly: true,
},
},
],

So the way this works is:

  1. webpack loads a resource and tries to find matching rule for the file extension (in our case, it's .ts or .tsx files)
  2. webpack finds our rule for .ts/.tsx files and runs our loader chain.
  3. in our loader chain, first the typescript loader is run which compiles typescript with the typescript compiler (via the ts-loader). In this step we don't actually want to compile es2015 to es5, we just want to compile typescript-only features like const enums and namespaces so that the next gatsby-provided loader can accept our input (e.g. ts => js)
  4. by default, the gatsby-provided loader is the babel-loader which uses babel to compile es2015 to whatever target your specify (e.g. ES2015 => ES5)

TL;DR you actually want the target to remain as ES2015 for typescript because we feed that result into Gatsby's default JS loader which handles ES2015 to ES5.

If you're looking to support older browsers, there's actually a much simpler way to do so via browerslist. Using browserlist is actually more powerful than specifying a compile target because other loaders like postcss can read from it too and automatically add CSS vendor prefixes.

Check out the official Gatsby docs for more info.

from gatsby-typescript.

d4rekanguok avatar d4rekanguok commented on May 31, 2024

Thanks for clarifying Rico, I'll add this to the docs.

from gatsby-typescript.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.