简体   繁体   English

今天如何在 package.json 中为嵌套子模块和 typescript 使用“导出”

[英]How can I use "exports" in package.json today for nested submodules and typescript

Latest Update (2022-06-06): TS 4.7 supports "exports"最新更新 (2022-06-06):TS 4.7 支持"exports"

tl;dr tl;博士

// package.json
"type": "module"

// tsconfig.json
"module": "node12" // or "nodenext"

Update: TS 4.5 does not support "exports" (also see this issue):更新:TS 4.5 不支持"exports" (另见此问题):

... support for Node.js 12 has been deferred to a future release, and is now only available as an experimental flag in nightly releases. ... 对 Node.js 12 的支持已推迟到未来版本,现在仅在夜间版本中作为实验标志提供。 This was not an easy decision, but our team had a combination of concerns around ecosystem readiness and general guidance for how/when to use the feature.这不是一个容易的决定,但我们的团队对生态系统的准备情况和如何/何时使用该功能的一般指导有共同的担忧。

[2022-04-01] The feature is still not available in TS 4.6. [2022-04-01]该功能在 TS 4.6 中仍然不可用。


Original question:原始问题:

I am wanting to take advantage of the new-ish "exports" feature of NodeJS/package.json so that I can do the following:我想利用 NodeJS/package.json 的新“导出”功能,以便我可以执行以下操作:

"exports": {
  ".": "./dist/index.js",
  "./foo": "./dist/path/to/foo.js"
}

And users can do the following:用户可以执行以下操作:

import { foo } from 'my-package/foo';

Typescript 4.5 should support the "exports" field, yet it does not seem to work. Typescript 4.5 应该支持“导出”字段,但它似乎不起作用。 I am building a simple package using TS 4.5.2, and I am consuming that package in a project using TS 4.5.2.我正在使用 TS 4.5.2 构建一个简单的 package,并且我正在使用 TS 4.5.2 的项目中使用 package。 I have looked at other SO questions and this github thread and this bug report but can't seem to find a consensus on the issue and whether it should work today.我已经查看了其他SO 问题以及此 github 线程此错误报告,但似乎无法就该问题以及它今天是否应该工作达成共识。

Note 1: I am still able to import using the more verbose syntax:注意 1:我仍然可以使用更详细的语法进行导入:

 import { foo } from 'my-package/dist/path/to/foo.js';

Note 2: I have also tried the object notation for exports, to no avail:注 2:我还尝试了用于导出的 object 表示法,但无济于事:

 "exports": { ".": { "require": "./dist/index.js", "import": "./dist/index.js" }, "./foo": { "require": "./dist/path/to/foo.js", "import": "./dist/path/to/foo.js" } }

Question(s):问题):

  1. Is this feature ready to be used with typescript projects today?此功能是否已准备好用于今天的 typescript 项目? If not, I just want to know.如果没有,我只想知道。
  2. If yes to #1, what am I missing?如果#1是,我错过了什么? Specifics about tsconfig would be useful for both the source project and consuming project.关于 tsconfig 的细节对于源项目和消费项目都很有用。 The TS compiler complains about node12/nodenext being used for either the module or moduleResolution fields (I am definitely using TS 4.5.2). TS 编译器抱怨node12/nodenext被用于modulemoduleResolution字段(我肯定使用 TS 4.5.2)。

I've been looking to use nested folders for a design system package we created at Pipefy, and after deep research, I found how to do it.我一直在寻找将嵌套文件夹用于我们在 Pipefy 创建的设计系统 package,经过深入研究,我找到了方法。

The package exports React components, and we use to import them like this import { Button } from '@mypackage/design-system; package 导出 React 组件,我们像这样导入它们import { Button } from '@mypackage/design-system;

Later on, we've added tokens to our design system library, like Colors , Spacing , and Fonts , but we don't want to import everything from the index, it isn't productive.稍后,我们在设计系统库中添加了标记,例如ColorsSpacingFonts ,但我们不想从索引中导入所有内容,因为它没有效率。

After some exhaustive research, I found how to export nested folders using TypeScript.经过一番详尽的研究,我发现了如何使用 TypeScript 导出嵌套文件夹。 My purpose is to use tokens like this import { Colors } from '@mypackage/design-system/tokens;我的目的是使用这样的令牌import { Colors } from '@mypackage/design-system/tokens;

To use your TypeScript lib like this you should use the typesVersions inside the package.json file.要像这样使用 TypeScript 库,您应该使用typesVersions文件中的package.json

Here I use it like this在这里我是这样使用的

"typesVersions": {
    "*": {
      "index": [
        "lib/components/index.d.ts"
      ],
      "tokens": [
        "lib/tokens/index.d.ts"
      ]
    }
  },

It worked like a charm for me, it would work for you too!它对我来说就像一个魅力,它也对你有用!

Without knowing what error you are getting, or in what other way TypeScript doesn't seem to be working for you (not sure why you would not want to share such crucial information), I can tell that your exports section appears to be missing types information.在不知道您遇到什么错误或以其他方式 TypeScript 似乎对您不起作用的情况下(不知道您为什么不想分享此类重要信息),我可以说您的exports部分似乎缺少类型信息。 Typically, if your.d.ts files were located next to their respective.js files, your exports section would look like this:通常,如果您的 .d.ts 文件位于它们各自的 .js 文件旁边,您的导出部分将如下所示:

"exports": {
  ".": {
    "types": "./dist/index.d.ts",
    "default": "./dist/index.js"
  },
  "./foo": {
    "types": "./dist/path/to/foo.d.ts",
    "default": "./dist/path/to/foo.js"
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM