Project references: Option to bundle dependencies with multi-file output
See original GitHub issueSearch Terms
“project references” bundle copy dependencies
Suggestion
When using project references, add an option to bundle a project’s dependencies when using outDir
. This would be the analogue of prepend
, which only works with outFile
. This would be useful when targeting a module format that doesn’t work with outFile
, such as commonjs
.
Use Cases
Two Stack Overflow questions today: https://stackoverflow.com/questions/51938528 https://stackoverflow.com/questions/51939395
For the specific case of CommonJS output, I had the idea to generate single-file AMD output and then use amdefine
to wrap the result as a CommonJS module. This sort of works but is extremely ugly.
Edit: I realized that both the prepend
option and the suggested option are of limited use for composite projects with external dependencies, because a separate tool is needed to aggregate the external dependencies, and that tool may be able to aggregate the components too. Concretely, if the components have external dependencies in node_modules
, then the inter-component dependencies can be added to package.json
using relative paths and npm install
will make symlinks under node_modules
, so if the packaging tool is capable of following those symlinks, it will pack everything up. (A similar technique to make symlinks is possible using yarn workspaces instead of dependencies with relative paths, and has in fact been used to solve https://stackoverflow.com/questions/51939395 .) However, symlink support is still uneven on Windows, as pointed out here.
I’m still interested in the rationale for offering prepend
and not the analogous thing for multi-file output. Was it just that prepend
was designed to suit the needs of the TypeScript compiler?
Examples
Straw-man proposal:
{
"references": [
{ "path": "foo", "copyOutDirTo": "some/path" },
]
}
Copies the outDir
of the referenced project to some/path
. By specifying copyOutDirTo
and the project’s own outDir
in the correct relationship, one can produce a single output tree in which modules will be able to find each other.
Checklist
My suggestion meets these guidelines:
- This wouldn’t be a breaking change in existing TypeScript / JavaScript code
- This wouldn’t change the runtime behavior of existing JavaScript code
- This could be implemented without emitting different JS based on the types of the expressions
- This isn’t a runtime feature (e.g. new expression-level syntax)
Possibly related
Issue Analytics
- State:
- Created 5 years ago
- Reactions:36
- Comments:5 (1 by maintainers)
Creating individual ts bundles for microservices in a monorepo has become a little bit of a nightmare due to recent improvements in dependency management, ironically. It had been as simple as copying node_modules into dist, zipping, and deploying to aws lambda.
Yarn workspaces, project references, and lerna all bring useful tooling but move dependencies into hard-to-reach places via hoisting or other magic. I expect a lot of people to be confused about how to build -> bundle w/ dependencies. Project references and workspaces are alluring but come with some possibly surprising hazards
Any updates on whether outDir based projects can be prepended?