2016-08-13 3 views
0

재사용 가능한 commonjs 라이브러리 (npm 패키지)를 올바르게 개발하려면 몇 가지 지침이 필요합니다. 그래서Commonjs 라이브러리의 index.ts를 외부 모듈

—lib 
——Helper.ts 
——Serialization.ts 
——meta 
——— Entity.ts 
—index.ts 

그리고 : 라이브러리의 구조는 다음과 같다 말할 수 있습니다. 각 파일은 외부 모듈입니다.

이제 라이브러리의 기능을 노출하고 엔트리 포인트 역할을 할 항목 파일 (예 : index.ts)을 만들어야합니다. 여기에 머리를 감쌀 수 없습니다. 나는 수출 모든하여 평탄화없이 중첩 된 구조를 유지하고 싶은 :

export * from './lib/Helper’; 
export * from './lib/Serialization’; 
export * from './lib/meta/Entity’; 
… 

원인이 논리적 그룹을 제거하고 가능한 이름으로 미래의 리드에 나는 또한 색인을 위해 노력했다

를 충돌 할 수 있습니다. 이 같은 TS :

import {Helper} from './lib/Helper'; 
import * as Serialization from './lib/Serialization'; 
import * as Decorators from './lib/meta/Decorators'; 

let core = { 
    Helper: Helper, 
    Serialization: Serialization, 
    Meta: { 
     Entity: Entity, 
    } 
} 

export default core; 

이 같은 수입 유형의 소비 올 때까지 그것은 완벽하게 작동합니다

import Core from ’nameOfTheNpmPackage'; 

let Entity = Core.Meta.Entity; 

function f(e: Entity)//<—This produce error cannot find name ‘Entity' 
{ 

} 
,369을

타입 선언 공간에 없기 때문에 (네임 스페이스에없는 경우 거기에 복사하는 방법이 있습니까?)

또 다른 옵션은 전체 라이브러리에 대해 하나의 d.ts 파일을 생성하는 것입니다. outFile + amd를 사용합니다. 그러나 그 파일은 외부 모듈이 아닙니다.

그래서 내 질문은 - 그것은 외부 모듈 및 라이브러리에 의해 노출 된 모든 기능을 내보낼 수 있도록 index.ts를 작성하는 것이 가능합니다.

미리 감사드립니다.

답변

2

언젠가 하루의 낭비와 나는 못생긴 해결책을 생각해 냈습니다. 내 보낸 commonjs 라이브러리의 중첩 구조를 유지하려면 각 '레벨'내부에 'index.ts'을 넣고 동일한 '레벨'의 모든 기능을 집계하고 다시 내 보냅니다.

—lib ——Helper.ts ——Serialization.ts ——meta ——— index.ts ——— Entity.ts —index.ts

경우 루트 index.ts :

export * from './lib/Helper'; 
export * from './lib/Serialization'; 

import * as S from './lib/Serialization'; //This is case where one file contains several exported classes that we want to keen as separate module 
export {S as Serialization}; 

import * as Meta from './lib/meta/index'; 
export {Meta}; 

그리고 메타/index.ts :

export * from './Entity'; 

난 정말 기쁠 위 내 샘플은 모양을 위해 이 공통적 인 (?) 작업이 '레벨'당 여분의 파일없이 해결할 수 있는지 파악하기 위해.

관련 문제