Typescript recently introduced a new feature to work with monorepos: references. By specifying references you can build all interdependent packages using tsc -b
(see also this blog post)
This seems ideal for use with a large mono repo where some packages are serverless services. These services typically depend on one or more packages of the monorepo. I've created an example here:
https://github.com/tommedema/serverless-mono-example
While building works well with tsc -b
, the problem is that the serverless framework needs to upload a single artifact .zip (to AWS in my case). When building a serverless service, like this example, only the compiled files of the nearest source files will be bundled.
How would one use typescript's references feature for use with serverless mono repos?
If it works for you to generate a single output file and use a module loader, you can use outFile
and prepend
.
If you want multiple output files, maybe it's worth filing a suggestion to ask for an option to bundle dependencies in that case; you would be the second person who has asked about this on Stack Overflow today. Edit: Suggestion is here.
Edit 2: After extensive discussion, the conclusion was to enable the nohoist
option on the final Yarn workspace, which gives us symlinks from node_modules
to the other workspaces. After we call tsc -b
in the final workspace, the Serverless packaging tool follows the symlinks and produces a zip file with the correct structure. No bundling is needed at the TypeScript level. Caveat: Yarn seems to install devDependencies of dependencies in the final workspace, which seems wrong to me and confuses Serverless into unnecessarily including those modules in the bundle.
来源:https://stackoverflow.com/questions/51939395/how-to-use-typescripts-references-and-build-mode-to-compile-a-mono-repo-to-a-se