[英]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.