我正在向npm注册表发布一个名为foo的npm包。 我使用compile-to-js语言编写了包。 为了理智,我将编译后的输出放入项目目录的dist/文件夹中。 我的package.json将入口点列为dist/entry.js :
{ "name": "foo", "main": "dist/entry.js", }有时,我想使用包中不属于入口点的文件。 例如,有一个非常有用的导出叫做dist/util.js里面的whatever :
import { whatever } from "foo/dist/util";这是有效的,但是强制我的包的用户在所有import语句中键入dist/是不方便的。
此外,重新导出每个可能的util函数不是DRY。 我不想从入口点重新导出 。
理想情况下,我想使用以下语法从dist/导入文件:
import { whatever } from "foo/util"如何配置我的package.json来搜索项目的dist/文件夹中的文件?
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", }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";This works, but forcing the users of my package to type dist/ in all import statements is inconvenient.
Furthermore, re-exporting every possible util function is not DRY. I do not want to re-export from the entrypoint.
Ideally, I would like to import files from dist/ using the following syntax:
import { whatever } from "foo/util"How do I configure my package.json to search for files in the dist/ folder of my project?
最满意答案
这是不可能做到的。
这就是为什么有些软件包具有重新导出所有公共导出的入口点文件(并非所有驻留在dist都是由最终用户使用)的原因,例如@angular/core 。
以及为什么某些包具有不合适的文件结构的原因,该文件结构发布到NPM注册表并且支持正确的导入路径,例如rxjs 。
This cannot be done.
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.
And the reason why some packages have unsuitable file structure that is published to NPM registry and favours proper import paths, e.g. rxjs.
更多推荐
发布评论