如何让npm从GitHub网址安装Typescript依赖项? [英] How to have npm install a typescript dependency from a GitHub url?

查看:260
本文介绍了如何让npm从GitHub网址安装Typescript依赖项?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请考虑以下情形:

  • 有一个代码库.该库用TypeScript编写,而打字稿代码则在GitHub上发布. package.json文件具有一个基于TypeScript代码创建JavaScript文件的构建脚本,以及一个将最终的JS文件放置在npm上的发布脚本.
  • 我创建了GitHub存储库的分支,对打字稿文件进行了一些修改,然后将这些更改推送到GitHub. (我还为原始的GitHub存储库打开了PR,但是在合并这些更改之前需要花费一些时间.)
  • 我希望在下游NPM软件包中使用这些代码更改,因此在下游软件包中,我将对修改后的软件包的引用(在下游的package.json文件中)更改为fork的GitHub URL,然后执行npm install
  • There is a code library. The library is written in TypeScript and the typescript code is published in GitHub. The package.json file has a build script which creates JavaScript files based on the TypeScript code and a publish script which then places the resulting JS files on npm.
  • I make a fork of the GitHub repo, make some modifications to the typescript files and push those changes to GitHub. (I also open a PR to the original GitHub repo but there is a time lage before these changes can be merged.)
  • I wish to consume these code changes in a downstream NPM package so in the downstream packages I change the reference (in the downstream's package.json file) to the modified package to the GitHub URL of my fork and do an npm install.

这不起作用,因为:

  • 修改后的软件包的package.json文件未在dist字段中列出打字稿文件,仅列出了自动生成的JS文件,因此在npm安装期间不会提取TypeScript文件.
  • 由于未检入GitHub,因此不存在已编译的JS文件.
  • The package.json file of the modified package does not list the typescript files in the dist field, only the automatically generated JS files so the TypeScript files are not pulled during the npm install.
  • The compiled JS files aren't present since they aren't checked in to GitHub.

我该如何解决?有没有一种方法可以修改npm install的行为,以便它可以获取回购中不在dist中的文件,然后在安装过程中运行构建脚本?

How can I solve this? Is there a way that I can modify the behavoir of npm install so that it fetches files in the repo that aren't in dist and then runs the build script during the install?

推荐答案

我遇到了同样的问题.看到了很多有关monorepos的文章(下面的链接),但是关于如何将TypeScript项目拆分为单独的存储库的文章却很少.

I had the same problem. Saw a lot of articles about monorepos (links below), but not much about how to split a TypeScript project into separate repositories.

简而言之,您需要一步建立JS文件.

In short, you need to build JS files at one step or the other.

请参见 https://github.com/stared/yarn-adding -pure-typescript-package-example 获取有效的代码示例.

See https://github.com/stared/yarn-adding-pure-typescript-package-example for a working code example.

因此,有一些解决方案.假设存储库为someuser/package-to-import

So, there are a few solutions. Let's say that the repository is someuser/package-to-import

使用 yarn ,您可以直接从GitHub存储库获取项目:

Using yarn you can get the project directly from a GitHub repository:

yarn add someuser/package-to-import#master

(或与npm install someuser/package-to-import#master相同)

如果使用此存储库的分支,则可以通过添加到package-to-import中的package.json回购此部分来简化工作:

If you work on a fork of this repository, you can make your life easier by adding to package.json in package-to-import repo this part:

  "scripts": {
    ...,
    "postinstall": "tsc --outDir ./build"
  }

现在,它可以与yarn add/upgrade一起使用,而无需任何额外的脚本. 另外,您也可以半手动进行操作,即在主存储库package.json

Now it just works with yarn add/upgrade with no extra scripts. Alternatively, you can do it semi-manually, i.e. create a script in your main repository package.json

  "scripts": {
    ...,
    "upgrade-package-to-import": "yarn upgrade package-to-import && cd node_modules/package-to-import && yarn build"
  }

链接

有另一种方法可以将存储库克隆到另一个文件夹中,构建文件并将其链接.

Link

There is a different approach to clone the repository into a different folder, build the files, and link it.

git clone git@github.com:someuser/package-to-import.git
cd package-to-import
npm run build  # or any other instruction to build
npm link

如果使用毛线,最后两行将是:

If you use yarn, the two last lines would be:

yarn build
yarn link

然后,输入项目的文件夹并编写

Then, enter the folder of your project and write

npm link package-to-import

或在纱线情况下

yarn link package-to-import

这些是符号链接,因此,当您从存储库中提取并构建文件时,将使用更新的版本.

These are symlinks, so when you pull from the repo and build files, you will use the updated version.

另请参阅:

  • npm link andyarn link official documentations
  • The magic behind npm link

一种完全不同的方法. 结合使用git子模块的建议:

An entirely different approach. With mixed advice for using git submodules:

  • 4 Ways to Go Monorepo in 2019
  • 4 Git Submodules Alternatives You Should Know
  • A (multi-) monorepo setup with Git Submodules

这篇关于如何让npm从GitHub网址安装Typescript依赖项?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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