Export multiple modules from NPM package

折月煮酒 提交于 2019-12-24 19:48:08

问题


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.

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"]
}

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
    • ....

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",

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?


回答1:


Would simply consolidating all exports in one index.ts do the trick for you?

package.json (projectA):

{
  "main": "dist/index.js",
  "typings": "dist/index.d.ts",
  ...
}

index.ts (projectA):

// Adjust the relative import paths 
// and identifiers to your structure
export { ModuleA } from "./moduleA";
export { ModuleB } from "./moduleB";

Some module in projectB:

import {ModuleA, ModuleB} from 'projectA'


来源:https://stackoverflow.com/questions/57513973/export-multiple-modules-from-npm-package

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!