简体   繁体   English

如何使用 TypeScript 在 package.json 上正确使用导出和子路径?

[英]How to correctly use exports and subpaths on package.json using TypeScript?

It's my first time trying to create a npm package using TypeScript, and I've been experiencing some troubles, probably because I missunderstood something following the documentation.这是我第一次尝试使用 TypeScript 创建 npm package,我遇到了一些麻烦,可能是因为我误解了文档后面的内容。 I'm using Node 16.16.0 with npm 8.13.2 at the moment of writing this.在撰写本文时,我正在使用 Node 16.16.0 和 npm 8.13.2。

First of all, my project structure looks like this.首先,我的项目结构是这样的。

src/
├─ module1/
│  ├─ index.ts
├─ module2/
│  ├─ index.ts
package.json
tsconfig.json

tsconfig.json tsconfig.json

{
  "compilerOptions": {
    "target": "es2017",
    "module": "commonjs",
    "esModuleInterop": true,
    "declaration": true,
    "outDir": "./lib",
    "strict": true
  },
  "include": ["src"],
  "exclude": ["node_modules", "**/__tests__/*"]
}

package.json package.json

{
  "name": "@owner/mypackage",
  "version": "v1.0.0",
  ...
  "files": [
    "lib/**/*"
  ],
  "type": "module",
  "exports": {
    "./module1": "./lib/module1/index.js",
    "./module2": "./lib/module2/index.js",
    "./package.json": "./package.json"
  }
}

The thing is that, when publishing the module, it generates the following structure:问题是,在发布模块时,它会生成以下结构:

lib/
├─ module1/
│  ├─ index.js
├─ module2/
│  ├─ index.js
package.json

Leaving me with the following importing structure:给我留下以下导入结构:

import {Foo} from "@owner/mypackage/lib/module1";

Instead of the desired one:而不是所需的:

import {Foo} from "@owner/mypackage/module1";

How can I achieve that pattern?我怎样才能实现这种模式? And, in case there is, can I improve it or follow better practices?而且,如果有,我可以改进它或遵循更好的做法吗?

Personally, in a project I have running Typescript 4.7+ , the following has worked: what you need is the typesVersions property in package.json instead.就我个人而言,在我运行Typescript 4.7+的项目中,以下方法有效:您需要的是package.json中的typesVersions属性。 (see https://www.typescriptlang.org/docs/handbook/declaration-files/publishing.html#version-selection-with-typesversions ) (参见https://www.typescriptlang.org/docs/handbook/declaration-files/publishing.html#version-selection-with-typesversions

I'll pull this quote out of the documentation above as relevance:我将从上面的文档中提取这句话作为相关性:

Well, if none of the fields in typesVersions get matched, TypeScript falls back to the types field , so here TypeScript 3.0 and earlier will be redirected to [...]/node_modules/package-name/index.d.ts.好吧,如果 typesVersions 中的任何字段都没有匹配,TypeScript 会回退到types 字段,所以这里 TypeScript 3.0 和更早版本将被重定向到 [...]/node_modules/package-name/index.d.ts。

Which essentially means, if the typesVersions field isn't defined in package.json , the types field is used, but the types field isn't helpful to us when when have multiple export paths with exports .这实质上意味着,如果typesVersions字段未在package.json中定义,则使用types字段,但当具有多个导出路径时, types字段对我们没有帮助exports

Hope this helps!希望这可以帮助!

{
  "exports": {
    "./module1": "./lib/module1/index.js",
    "./module2": "./lib/module2/index.js",
  },
  "typesVersions": {
     "*": {
       "module1": ["lib/module1/index.js"],
       "module2": ["lib/module2/index.js"]
    }
  }
}

暂无
暂无

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

相关问题 如何使用“exports”键正确暴露 package.json 中的子路径? - How to properly expose subpaths in package.json using the ”exports” key? 今天如何在 package.json 中为嵌套子模块和 typescript 使用“导出” - How can I use "exports" in package.json today for nested submodules and typescript 如何从 package.json 有条件地导出 - How to conditional exports from package.json 如何解决包子路径 './package.json' is not defined by "exports" 错误 - How to resolve Package subpath './package.json' is not defined by "exports" error 无法在 React 中使用 package.json“导出”导入模块 - Can't import module using package.json "exports" in React 如何使用package.json中的'main'参数? - How to use the 'main' parameter in package.json? 为什么 package.json 导出字段在 npm package 中不起作用 - why package.json exports field not working in npm package 如何运行 TypeScript function 从 package.Z466DEEC76ECDF624FCA6D3844 脚本57DF - How to run a TypeScript function from a package.json script `main` 和 `module` 与 package.json 中的 `exports` 有什么区别? - What is the difference between `main` and `module` vs `exports` in package.json? 弃用警告:在 node_modules\postcss\package.json 的 package 的“导出”字段模块解析中使用弃用的文件夹映射“./” - DeprecationWarning: Use of deprecated folder mapping "./" in the "exports" field module resolution of the package at node_modules\postcss\package.json
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM