问题描述
我正在将名为foo
的npm程序包发布到npm注册表.我使用编译成js语言编写了该程序包.为了保持理智,我将编译后的输出放入项目目录的dist/
文件夹中.我的package.json
将入口点列为dist/entry.js
:
I'm publishing an npm package named foo
to the npm registry.I wrote the package using a compile-to-js language.For sanity, I put the compiled output into the dist/
folder of the project directory.My package.json
lists the entrypoint as dist/entry.js
:
{
"name": "foo",
"main": "dist/entry.js",
}
有时候,我想使用软件包中不属于入口点的文件.例如,在dist/util.js
内有一个非常有用的导出,称为whatever
:
Sometimes, I want to use files within the package that are not part of the entry point. For example, there is a very useful export called whatever
inside of dist/util.js
:
import { whatever } from "foo/dist/util";
这可行,但是强制我的软件包的用户在所有import
语句中键入dist/
是不方便的.
This works, but forcing the users of my package to type dist/
in all import
statements is inconvenient.
此外,重新导出每个可能的util函数都不是DRY. 我不想从入口点重新导出.
Furthermore, re-exporting every possible util function is not DRY. I do not want to re-export from the entrypoint.
理想情况下,我想使用以下语法从dist/
导入文件:
Ideally, I would like to import files from dist/
using the following syntax:
import { whatever } from "foo/util"
如何配置package.json
在项目的dist/
文件夹中搜索文件?
How do I configure my package.json
to search for files in the dist/
folder of my project?
推荐答案
此操作无法完成.
这就是为什么某些软件包具有重新导出所有公共出口的入口点文件的原因(并非最终用户打算使用dist
中的所有内容). @angular/core
.
This is the reason why some packages have entry point file that re-exports all public exports (not everything that resides in dist
is intended to be used by end user), e.g. @angular/core
.
以及某些程序包具有不适当的文件结构的原因,该文件结构会发布到NPM注册表并偏向于正确的导入路径,例如rxjs
.
And the reason why some packages have unsuitable file structure that is published to NPM registry and favours proper import paths, e.g. rxjs
.
这篇关于设置"root"为"root".已发布的npm项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!