如何从需要构建步骤的 github 安装 npm 包,例如分叉图书馆时? [英] How to install a npm package from github requiring a build step, e.g. when forking a library?
问题描述
假设您使用像 vue3-datepicker
.你意识到你需要自定义一些东西,作为第一步,你想使用它的自定义分支.
Assume you use a library like vue3-datepicker
. You realize you need to customize something, and as as first step you want to use a custom fork of it.
问题是,当包被推送到 npm 的注册表时有一个构建步骤,因为该项目不使用纯 JavaScript,但可能有 vue 或 typescript 文件.
The issue is, there is a build step when the package is pushed to npm's registry since the project doesn't use plain JavaScript, but may have vue or typescript files.
在这种情况下,这将是 npm run build:component
,尽管这取决于项目.
In this case, that would be npm run build:component
, though that depends on the project.
只需通过以下方式从 github 安装 fork:
Just installing the fork from github via:
yarn add <GitHub user name>/<GitHub repository name>#<branch/commit/tag>
因此还不够,因为 ./dist
文件夹不存在.
hence doesn't suffice as then the ./dist
folder doesn't exist.
您会遇到非常奇怪的错误,例如:
You'll get really strange errors like:
error: [plugin: vite:dep-scan] Failed to resolve entry for package "vue3-datepicker". The package may have incorrect main/module/exports specified in its package.json: Failed to resolve entry for package "vue3-datepicker". The package may have incorrect main/module/exports specified in its package.json.
作为一个快速而肮脏的解决方案,我在我的 fork 中删除了 ./dist/
.gitignore
文件夹中,运行 npm i &&npm 在我的 fork 中运行 build:component
,并推送它.
As a quick and dirty solution, I removed in my fork the ./dist/
folder from the .gitignore
, ran the npm i && npm run build:component
in my fork, and pushed it.
巨大的缺点是,./dist/
文件夹现在是该存储库的一部分,在我的 fork 中每次更改后,我还必须再次构建文件并推送这些文件.
Huge downside is, the ./dist/
folder is now part of that repository, after each change in my fork I also have to build the files again and push those as well.
我宁愿在我的应用程序中使用我的 fork 来触发构建过程.有没有办法从我的申请中说:
I rather have the build process triggered in my application using my fork. Is there a way from my application to say:
当你安装那个库时,你必须在下载所有文件后运行某个脚本?
When you install that library, you have to run a certain script once you downloaded all the files?
该解决方案应该对 npm
和 yarn
都可用,从某种意义上说,fork 可以由任何一个安装在不同的应用程序中.
The solution should be usable for both npm
and yarn
, in the sense that the fork my be installed by either one in different applications.
推荐答案
引自 npm-install 文档
如果正在安装的包包含一个 prepare
脚本,它的 dependencies
和 devDependencies
将被安装,并且准备脚本将被运行,在打包安装之前.
If the package being installed contains a
prepare
script, itsdependencies
anddevDependencies
will be installed, and the prepare script will be run, before the package is packaged and installed.
所以在你的 fork 的 package.json
中你可以添加
so in your fork's package.json
you can add
"scripts": {
// ...
"build:component": "rollup -c build/rollup.config.js",
"prepare": "yarn build:component || npm run build:component"
}
这篇关于如何从需要构建步骤的 github 安装 npm 包,例如分叉图书馆时?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!