如何使用 Typescript 的引用和构建模式将单声道 repo 编译为无服务器包? [英] How to use Typescript's references and build mode to compile a mono repo to a serverless bundle?

查看:47
本文介绍了如何使用 Typescript 的引用和构建模式将单声道 repo 编译为无服务器包?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Typescript 最近引入了一个与 monorepos 一起使用的新功能:引用.通过指定引用,您可以使用 tsc -b 构建所有相互依赖的包(另请参阅此 博客文章)

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)

这似乎非常适合与大型单一存储库一起使用,其中一些软件包是无服务器服务.这些服务通常依赖于一个或多个 monorepo 包.我在这里创建了一个示例:

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

虽然使用 tsc -b 构建效果很好,但问题是无服务器框架需要上传单个工件 .zip(在我的情况下为 AWS).构建无服务器服务时,例如这个例子,只捆绑最近源文件的编译文件.

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.

如何将 typescript 的引用功能用于无服务器单存储库?

How would one use typescript's references feature for use with serverless mono repos?

推荐答案

如果它适合您生成单个输出文件并使用模块加载器,您可以使用 outFileprepend.

If it works for you to generate a single output file and use a module loader, you can use outFile and prepend.

如果您想要多个输出文件,也许值得提出建议以在这种情况下要求捆绑依赖项的选项;你将是今天在 Stack Overflow 上提出这个问题的第二个人.建议在这里.

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. Suggestion is here.

编辑 2:经过广泛讨论,得出的结论是在最终的 Yarn 工作区启用 nohoist 选项,这为我们提供了从 node_modules 到其他工作区的符号链接.在我们在最终工作区中调用 tsc -b 之后,无服务器打包工具遵循符号链接并生成具有正确结构的 zip 文件.在 TypeScript 级别不需要捆绑.警告:Yarn 似乎在最终工作区中安装了依赖项的 devDependencies,这在我看来是错误的,并且使 Serverless 混淆为不必要地将这些模块包含在包中.

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.

这篇关于如何使用 Typescript 的引用和构建模式将单声道 repo 编译为无服务器包?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆