从NPM包中导出多个模块 [英] Export multiple modules from NPM package
问题描述
我有一个使用Node和Typescript的相当大的项目A.在项目A中,我有很多不同的模块,希望在另一个项目B中重用.
I have a rather large project A using Node and Typescript. In project A I have a lot of different modules that I would like to reuse in another project B.
因此,我使用以下tsconfig.json构建了项目A:
Therefore I have built the project A with this tsconfig.json:
{
"compilerOptions": {
"target": "es2017",
"module": "commonjs",
"declaration": true,
"outDir": "./dist",
"sourceMap": true,
"strict": true,
"noImplicitAny": true,
"strictNullChecks": true,
"typeRoots": ["./node_modules/@types", "./modules/@types"]
},
"exclude": ["node_modules"]
}
因此,所有文件都以这种方式内置到/dist文件夹中:
So all the files are built into the /dist folder this way:
- dist
- moduleA.js
- moduleA.map
- moduleA.d.ts
- moduleB.js
- moduleB.map
- moduleB.d.ts
- ....
- dist
- moduleA.js
- moduleA.map
- moduleA.d.ts
- moduleB.js
- moduleB.map
- moduleB.d.ts
- ....
要在另一个项目中使用这些moduleA和moduleB,请将以下内容添加到Project A中的package.json中:
To use these moduleA and moduleB in another project, I add the following to the package.json in Project A:
"name": "projectA", "version": "1.0.0", "description": "...", "main": "dist/moduleA.js", "typings": "dist/moduleA.d.ts",
我使用纱线工作区作为项目B中的程序包访问项目A.但是问题是,在新项目B中使用
import {ModuleA} from 'projectA'
时,我只能访问模块A?那么如何从ProjectA访问更多模块?I use yarn workspaces to access Project A as a package in Project B. But problem is that I can only access moduleA, when using
import {ModuleA} from 'projectA'
in my new project B? So how can I access more modules from ProjectA?推荐答案
将所有出口合并为一个
index.ts
会为您解决这个问题吗?Would simply consolidating all exports in one
index.ts
do the trick for you?package.json(projectA):
package.json (projectA):
{ "main": "dist/index.js", "typings": "dist/index.d.ts", ... }
index.ts(projectA):
index.ts (projectA):
// Adjust the relative import paths // and identifiers to your structure export { ModuleA } from "./moduleA"; export { ModuleB } from "./moduleB";
projectB中的某些模块:
Some module in projectB:
import {ModuleA, ModuleB} from 'projectA'
这篇关于从NPM包中导出多个模块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!