microsoft / TypeScript-Babel-Starter

A sample setup using Babel CLI to build TypeScript code, and using TypeScript for type-checking.
MIT License
2k stars 229 forks source link

How would this work with project references? #35

Open borekb opened 5 years ago

borekb commented 5 years ago

Let's say I have a monorepo with packages/app and packages/lib. In the tsc world, I'd create app/tsconfig.json with this:

{
  "references": [
    { "path": "../lib" }
  ]
}

And when transpiling, tsc -b would check if source files in lib changed and re-build them if necessary.

If Babel is responsible for transpiling, will it pick up changes in lib? Does it read tsconfig.json configuration about project references?

ghost commented 5 years ago

Hey @borekb , IIRC, babel simply strips off all types and doesn't even read tsconfig.json for that purpose - that means it does not know of typescript project references. Maybe you can use the --watch option from babel cli to rebuild automatically? Project references do not make sense, if used in combination with @babel/preset-typescript and should be replaced by a custom build step - would be happy to to be corrected by someone, if there is still a reason left.

rupurt commented 5 years ago

@borekb my understanding like @ford04 is that this is not supported by @babel/preset-typescript. It would be a really sweet feature though 😄

The way I'm working around this atm is to add an npm script that runs a build watcher from the shared lib + the consuming app. e.g.

// package.json
{
   "scripts: {
      "dev": "concurrently \"cd packages/shared-lib && yarn build:watch\" \"cd packages/main-app && yarn start\""
   }   
}

// packages/main-app/package.json
{
   // ...
   "scripts: {
      "start": "react-scripts start"
   }
}

// packages/shared-lib/package.json
{
   // ...
   "scripts: {
      "build:watch": "tsc -w"
   }
}
kiprasmel commented 4 years ago

/cc @RyanCavanaugh

Would it be hard to support this out-of-the-box?

And has anyone considered this (project references) together with yarn workspaces?

Thanks!


Edit:

I suppose one could currently use babel's project wide configuration and always run the type-check/build tasks from the top of the monorepo atm. Still, a first-class solution would be way better.

jedrichards commented 4 years ago

But as @ford04 says the main benefit of project references in a pure tsc-built project is that you get up-to-date type checking against the public API of any referenced projects when you build an individual project. @babel/preset-typescript does zero type checking, it knows nothing about your types, so building the types of referenced projects before the babel build gets you nowhere.

I would have thought the correct approach surely is to build your @babel/preset-typescript as per normal in isolation, and then if you want to ensure you have no type errors then do a separate type-check step with tsc --noEmit afterwards?

kiprasmel commented 4 years ago

@jedrichards

<...> if you want to ensure you have no type errors then do a separate type-check step with tsc --noEmit afterwards?

what about type-checking custom features / proposals that babel supports with a plugin, but typescript does not?

It seems like there's no way to have both...

jedrichards commented 4 years ago

@sarpik Yeah true, tricky when you're using language features only Babel knows how to deal with 🤔