简体   繁体   English

如何在 git 中“发布”一个私有的 Typescript npm 包?

[英]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 存储库中的源安装依赖项,这里有其他解决方案:

Solution #1 — Install the dependency from a local folder解决方案 #1 — 从本地文件夹安装依赖项

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 文档

Solution #2 — Install a private npm proxy registry解决方案#2 — 安装私有 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.然后,您将能够发布包含所有编译文件的包。

Solution #3 — Pay NPM and publish a private package解决方案 #3 — 支付 NPM 并发布私有包

You could publish a true package (also with the compiled files) in aprivate repository from a paid account.您可以从付费帐户在私有存储库中发布一个真正的包(也包含已编译的文件)。

Solution #4 — Publish as a release on your GitHub repository (not tested)解决方案 #4 — 在您的 GitHub 存储库上发布为一个版本(未测试)

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:基本上只有几个选择:

  1. The developer of LibraryA runs it and publishes the compiled result (somewhere); LibraryA 的开发者运行它并发布编译结果(某处);
  2. LibraryA is published in source form and compilation happens when installing the package to WebserverB; LibraryA 以源代码形式发布,并在将包安装到 WebserverB 时进行编译;
  3. LibraryA is published in source form and building WebserverB also compiles the library. LibraryA 以源代码形式发布,构建 WebserverB 也会编译该库。

Let's look at each in detail.让我们详细看看每一个。

1. Publish compiled result 1. 发布编译结果

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:因此,再次只有几个选择:

  • npm registry (private, if you can afford it, public if you're OK with it). npm 注册表(私有,如果您负担得起,则公开,如果您可以接受)。 Drawback: well, you have to pay for it.缺点:嗯,你必须为此付出代价。
  • A tarball on some server where you can get a URL to it.某个服务器上的 tarball,您可以在其中获取它的 URL。 Drawback: You have to get a server to host these.缺点:你必须得到一个服务器来托管这些。 And take care of said server.并照顾好上述服务器。
  • A new GIT repository.一个新的 GIT 存储库。 Not tarballed, because npm cannot untar from a git repository.未压缩,因为 npm 无法从 git 存储库中解压缩。 Drawback: you now have two repositories for one project.缺点:您现在有一个项目的两个存储库。
  • The same git repository where LibraryA lives. LibraryA 所在的同一个 git 存储库。 In essence, you not only commit your TypeScript source files, but also the compiled result.从本质上讲,您不仅要提交 TypeScript 源文件,还要提交编译结果。 Drawback: committing compilation artifacts is just wrong .缺点:提交编译工件是错误的 And you're publishing your sources together with the compiled results.并且您将源代码与编译结果一起发布。 You don't need those in WebserverB.您不需要 WebserverB 中的那些。
  • The same git repository where LibraryA lives, but in a separate, disconnected branch. LibraryA 所在的同一个 git 存储库,但在一个单独的、断开连接的分支中。 Drawback: This gets confusing.缺点:这会让人感到困惑。 How many repositories have you seen with two disconnected main branches?您看到有多少个存储库具有两个断开连接的主分支?

Still, despite the drawbacks, they're all serviceable options.尽管如此,尽管有缺点,但它们都是可用的选择。

2. Compile upon install 2.安装时编译

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的回答

3. Compile together with WebserverB 3.与WebserverB一起编译

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.事实上,也许他们甚至会帮助调试。

I guess you could add an install script in the library's package.json that compiles it.我想你可以在库的package.json中添加一个install脚本来编译它。 Then you just need to point main and types to the output files.然后你只需要将maintypes指向输出文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM