你可以在发布到 npm 之前如何测试我的库?

huangapple go评论70阅读模式
英文:

How can I test my library before publishing to npm?

问题

我有两个Angular项目:主项目和库项目。我偶尔会对库项目进行一些更改。在将其上传到NPM之前,我想检查它是否能在我的主项目中与其他包正常运行。我所做的就是在对库项目进行更改后构建它,获取那些构建后的文件(来自dist文件夹),然后用它们替换我的主项目的node_modules中的文件。但我的Angular应用程序看不到这些更改。我知道修改node_modules并不是一个好的做法,但还是想知道是否有其他方法可以在发布到NPM之前测试我的库,或者是否有可能让Angular看到node_modules中的这些更改?

英文:

I have two Angular projects: main and library. Once in a time I make some changes to the library. And before uploading it to NPM I would like to check if it will run well on my main project with other packages. And what I am doing is just building my lib project after changes, take those built files(from dist folder) and replace them with one in node_modules of my main project. But my Angular app doesn`t see those changes. I know that its not good practice at all to modify node_modules but still.

I have playground in my library project where I test it but I would like to see how it works in my main project with other packages.


Is there any other ways how I can test my library BEFORE publishing it to NPM? OR Is it possible to make Angular see those changes in node_modules?

答案1

得分: 1

问题在于Angular会进行一些缓存以提高构建性能。当修改node_modules内的文件,但包的版本保持不变时,Angular会使用缓存版本,不会应用对文件的更改。

解决这个问题的正确方法是在本地打包该包,然后在项目中安装已打包的包,如此答案中所述。

或者,您可以通过运行ng cache clean来清除缓存。如果您想完全禁用缓存,可以运行ng cache disable。请注意,使用此方法会导致后续构建的构建时间较长。

英文:

The problem is that Angular does some caching to improve the build performance. When modifying files inside the node_modules, but the version of the package stays the same, Angular uses the cached version and will not respect the changes to the files.

The proper solution to this problem is to locally pack the package, and installed the packed package in your project, as described in this answer.

Alternatively you can clean the cache by running ng cache clean. If you want to completely disable the cache, you can run ng cache disable. Please note that with this approach you will see higher build times of subsequent builds.

huangapple
  • 本文由 发表于 2023年5月17日 15:58:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/76269769.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定