设置已发布的npm项目的“根”(Setting the “root” of a published npm project)

系统教程 行业动态 更新时间:2024-06-14 16:59:47
设置已发布的npm项目的“根”(Setting the “root” of a published npm project)

我正在向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.

更多推荐

本文发布于:2023-04-17 09:23:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/dzcp/fadf32cc61fb9327c458ba60e938be53.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:项目   Setting   npm   project   published

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!