[英]How to "publish" a private Typescript npm package in git?
I think this should be a standard problem but I can't find any answers...我认为这应该是一个标准问题,但我找不到任何答案......
I have two typescript projects - LibraryA and WebserverB.我有两个打字稿项目 - LibraryA 和 WebserverB。 They are separate projects and each have their own git repository.它们是独立的项目,每个都有自己的 git 存储库。 They are also private projects - I don't want them to be available in public.它们也是私人项目——我不希望它们公开。
Obviously, I want to use LibraryA in WebserverB.显然,我想在 WebserverB 中使用 LibraryA。 The correct way to do it I think would be through npm, since it manages all other libraries.我认为正确的方法是通过 npm,因为它管理所有其他库。 Luckily npm supports git URLs in dependencies, so I can point it to LibraryA's repository directly.幸运的是,npm 支持依赖项中的 git URL,因此我可以将其直接指向 LibraryA 的存储库。
However that repository doesn't contain the compiled Javascript files, only the TypeScript files.但是,该存储库不包含已编译的 Javascript 文件,仅包含 TypeScript 文件。 How can I make this work?我怎样才能使这项工作? Or what is the correct approach in this situation?或者在这种情况下正确的方法是什么?
If you don't want to install your dependency from the sources in a git repository, here are other solutions:如果您不想从 git 存储库中的源安装依赖项,这里有其他解决方案:
A light solution is to install LibraryA
via a git clone
and to build it.一个简单的解决方案是通过git clone
安装LibraryA
并构建它。 Then, in WebserverB/
, you can do a npm install ../path/to/local/LibraryA
:然后,在WebserverB/
,您可以执行npm install ../path/to/local/LibraryA
:
- npm install <folder>: npm install <文件夹>:
Install the package in the directory as a symlink in the current project.将包安装在目录中作为当前项目中的符号链接。 Its dependencies will be installed before it's linked.它的依赖项将在链接之前安装。 If <folder> sits inside the root of your project, its dependencies may be hoisted to the toplevel node_modules as they would for other types of dependencies.如果<folder>位于项目的根目录中,则其依赖项可能会像其他类型的依赖项一样被提升到顶级node_modules 。 ( Source: NPM documentation ) (来源:NPM 文档)
You can install a private npm proxy registry on your server, like Verdaccio .你可以在你的服务器上安装一个私有的 npm 代理注册表,比如Verdaccio 。 Then, you'll be able to publish your package with all compiled files.然后,您将能够发布包含所有编译文件的包。
You could publish a true package (also with the compiled files) in aprivate repository from a paid account.您可以从付费帐户在私有存储库中发布一个真正的包(也包含已编译的文件)。
The NPM documentation indicates another option: NPM 文档指出了另一种选择:
- npm install <tarball url> npm install <tarball url>
Fetch the tarball url, and then install it.获取 tarball url,然后安装它。 In order to distinguish between this and other options, the argument must start with “http://” or “https://”为了区分这个选项和其他选项,参数必须以“http://”或“https://”开头
Then, publish a package consist to build, compress and upload it as a new release in your private Github repository.然后,发布一个包,以构建、压缩并将其作为新版本上传到您的私有 Github 存储库中。 After that, it should be possible to access the tarball through an URL using a personal access token?之后,应该可以使用个人访问令牌通过 URL 访问 tarball 吗?
OK, I'll answer my own question with a summary of the other answers and some extra options.好的,我将用其他答案的摘要和一些额外的选项来回答我自己的问题。
After some thought I came to a realization.经过一番思考,我终于明白了。 The question is - who and when runs tsc
to compile the LibraryA's Typescript to Javascript?问题是 - 谁以及何时运行tsc
将 LibraryA 的 Typescript 编译为 Javascript? Essentially there are just a few choices:基本上只有几个选择:
Let's look at each in detail.让我们详细看看每一个。
The question then is - where is the published result stored?那么问题是 - 发布的结果存储在哪里? It has to be somewhere where NPM can access it.它必须位于 NPM 可以访问它的地方。 As such, there are only a few choices again:因此,再次只有几个选择:
Still, despite the drawbacks, they're all serviceable options.尽管如此,尽管有缺点,但它们都是可用的选择。
The main drawback here is that this immediately puts a dependency on typescript on WebserverB.这里的主要缺点是这会立即依赖于 WebserverB 上的打字稿。 And not just a development-dependency but a full runtime dependency.不仅仅是开发依赖,而是完整的运行时依赖。 This may be OK, but it's getting really weird.这可能没问题,但它变得非常奇怪。 How do you plan on deploying WebserverB?您打算如何部署 WebserverB? Will that too run the compilation?这也会运行编译吗? I don't think this is the way to go.我不认为这是要走的路。 But it's possible.但这是可能的。 See HB
's answer for details.有关详细信息,请参阅HB
的回答。
Well, if you plan only on using it in typescript projects, this might be OK.好吧,如果你只打算在打字稿项目中使用它,这可能没问题。 I'm not sure about installing it though.我不确定是否安装它。 You'll need to modify your tsconfig.json
file to include node_modules/LibraryA
in the compilation.您需要修改tsconfig.json
文件以在编译中包含node_modules/LibraryA
。 That's weird.这很奇怪。 And will your IDE be happy about this?您的 IDE 会对此感到高兴吗? All in all, this doesn't feel like a good idea either.总而言之,这也不是一个好主意。 It feels like fighting/abusing the system and that rarely ends well.感觉就像在对抗/滥用系统,而且很少有好的结局。
In the end, I think I'll go with the "commit compiled JS" approach.最后,我想我会采用“提交编译的 JS”方法。 Because " just wrong " isn't a good argument.因为“只是错了”不是一个好的论点。 And since it's a private project, the extra published sources aren't much of an issue either.而且由于它是一个私人项目,因此额外发布的资源也不是什么大问题。 In fact, maybe they'll even help to debug.事实上,也许他们甚至会帮助调试。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.