siefkenj / unified-latex

Utilities for parsing and manipulating LaTeX ASTs with the Unified.js framework
MIT License
85 stars 20 forks source link

Switch to nodejs export conditions from TS project references #67

Closed theseanl closed 7 months ago

theseanl commented 7 months ago

Previously, typescript project references were used to wire imports between workspace packages. During compilation, it allows one to resolve any workspace modules imported from another workspace module to their respective declaration files, and if it hasn't been built, it automatically tries to build the dependent module first. Also, in IDE, TS project references allows IDEs to resolve imports to their source .ts file instead of the .d.ts file as during the compilation; this allows one to get feedback directly from IDE when something is changed from a dependent workspace package without the need of re-building it.

However, it seemed that type information available from .ts and .d.ts were sometimes different, so that there was no error caught on IDE but the build fails. I believe it must be a bug of TS, or maybe due to a defective d.ts generated for yet another issue in the meantime, but this has been pretty confusing. Also, having to reference .d.ts to build requires a dependency graph to be acyclic, which hampers quickly implementing and testing features (regardless of the end output dependency graph being acyclic or not).

This PR tries to get rid of such issues by ditching TS project references and utilizing nodejs native package.json export conditions. Note that export conditions were already being used to support consuming the package in both esm and cjs format. We add an artificial condition "_bundle", and use the respective bundler's export conditions feature https://esbuild.github.io/api/#conditions and https://www.typescriptlang.org/tsconfig#customConditions to achieve a similar effect as what was provided by TS project references: IDE resolve any workspace package imports to their respective Typescript source file, not their build output .d.ts files. Moreover, now the compilation also consults the original .ts source files instead of .d.ts, so the build order does not need to be topologically sorted.

We lose an ability to compile only the necessary packages by abandoning TS project references, but I believe the number of packages are not too large, so we can manually accommodate it or just run a full build. If this feature is strictly needed, one may adopt a dedicated task runner such as Make.

+) This PR also tweaks build commands so that it works equally on Windows.

siefkenj commented 7 months ago

Could you reformat the JSON with Prettier, please? It should be 4 space indentation.

theseanl commented 7 months ago

I just learned that Typescript project references are not transitive. Relevant discussions are at https://github.com/microsoft/TypeScript/issues/30608, https://github.com/microsoft/TypeScript/issues/46153. This could be a source of weird baheviors I have been experiencing with tsc -b.