cevek / ttypescript

Over TypeScript tool to use custom transformers in the tsconfig.json
1.53k stars 56 forks source link

Support running transforms on all referenced projects with the --build option #28

Closed purplemana closed 5 years ago

purplemana commented 5 years ago

I have a monorepo where I have configured each package to be built by ttsc -b. I need tsconfig alias paths to be transformed to relative paths using https://www.npmjs.com/package/@zerollup/ts-transform-paths module. Currently building a dependent project with ttsc doesn't run the transforms on the dependency packages. The only way is to manually build all packages in the order of dependencies. Considering tsc already builds them in the order, adding this support will be a huge step in solving the dreaded mono-repo holy grail problems.

Please let me know if there are any known workarounds.

cevek commented 5 years ago

I've just tested project references, and it works. testTtsc.zip (npm i && npm start)

Can you provide some example project where it doesn't work?

cevek commented 5 years ago

Maybe you put all transformers into main project which gather other projects via references? Transformers should be in every project's tsconfig.json

purplemana commented 5 years ago

Please access it from the branch ttypescript in the git repo https://github.com/purplemana/alchemist. Few points:

  1. This is a lerna, yarn & TS mono-repo.
  2. The important package dependencies are in this order - drivers < models < services.
  3. Every package has a top level src folder. The baseUrl points to .. Paths is set to resolve @src/* to src/*, so inside the package code, you can reference any local file from the top, e.g. @src/types instead of relative ../../types.
  4. All packages build to their own build folder.
  5. Every package has its own build script. This is different from your example zip where there's only one at the top. The idea is that you can build any package instead of building the whole mono-repo, and it will only build that package and its dependencies.

Repro steps:

  1. Checkout branch ttypescript & yarn install.
  2. cd packacges/drivers and build with yarn build.
  3. Notice that src/fixtures/db/index.ts contains import { DB } from "@src/db"; which is remapped correctly to const db_1 = require("../../db"); in the output file located at build/src/fixtures/db/index.js.
  4. Now clean the build assets using yarn clean.
  5. cd packages/models and build with yarn build. This will trigger re-building of drivers.
  6. Check the file packages/drivers/build/src/fixtures/db/index.js. This time it is not remapped by the transform and still contains const db_1 = require("@src/db");.

So when models is being built, the transform doesn't run on the output of drivers.

cevek commented 5 years ago

Your project doesn't build correctly: after yarn install it prints

yarn install v0.16.1
info No lockfile found.
success Nothing to install.
✨  Done in 0.08s.

and after yarn build a lot of ts errors:

../resources/src/errors/auth/index.test.ts:2:24 - error TS2307: Cannot find module 'chai'.

2 import { expect } from "chai";
                         ~~~~~~

../resources/src/errors/auth/index.test.ts:6:1 - error TS2582: Cannot find name 'describe'. Do you need to install type definitions for a test runner? Try `npm i @types/jest` or `npm i @types/mocha`.

6 describe("errors/auth tests", () => {
purplemana commented 5 years ago

Your yarn version seems to not support workspaces. Could you try with the latest version 1.12.1 please? Also as a precaution, check if the older yarn created an empty lockfile and delete it.

cevek commented 5 years ago

I found your problem: plugins section must be in the compilerOptions, but you set it in the root of tsconfig.json

purplemana commented 5 years ago

Thanks for the super quick help @cevek! It's working as expected. Wow, this has been the end of a long list of trial and errors to get this mono-repo setup working without heart burns.