[TypeScript] Barrel의 장점과 단점

2023. 10. 23.·🎨 프론트엔드 공부/JS & TS

📍Barrel

여러 모듈의 export 를 하나의 모듈(주로 index.ts)에서 import로 받아 다시 export 해주는 파일

 

예를 들어 아래와 같은 3개의 모듈이 각각 존재할 때

// src/lib/foo.ts
export class Foo {}

// src/lib/bar.ts
export class Bar {}

// src/lib/baz.ts
export class Baz {}

 

barrel이 없으면 3개 라인의 import가 필요함

// src/pages/page.ts
import { Foo } from '../lib/foo';
import { Bar } from '../lib/bar';
import { Baz } from '../lib/baz';

 

barrel 파일(lib/index.ts) 을 추가하여 import + export를 해주면

// src/lib/index.ts
export * from './foo'; // re-export all of its exports
export * from './bar'; // re-export all of its exports
export * from './baz'; // re-export all of its exports

 

consumer 위치에서는 각 모듈 대신 barrel에서 import하여 import를 간소화할 수 있다

// src/pages/page.ts
import { Foo, Bar, Baz } from '../lib';

 

📍Named exports

export * ~ 대신, 특정 이름으로 export할 수도 있다

 

아래처럼 baz.ts 가 함수를 가지고 있다고 할 때

// src/lib/foo.ts
export class Foo {}

// src/lib/bar.ts
export class Bar {}

// src/lib/baz.ts
export function getBaz() {}
export function setBaz() {}

 

export * from './baz'; 했으면 아래처럼 import 했을 테지만

// src/pages/page.ts
import { Foo } from '../lib/foo';
import { Bar } from '../lib/bar';
import { getBaz, setBaz } from '../lib/baz';

 

barrel 에서 as 키워드로 name을 지정해주면

// src/lib/index.ts
export * from './foo'; // re-export all of its exports
export * from './bar'; // re-export all of its exports

import * as baz from './baz'; // import as a name baz
export { baz }; // export the name

 

counsumer 위치에서 아래처럼 활용할 수 있다

// src/pages/page.ts
import { Foo, Bar, baz } from '../lib'; // src/lib/index.ts is implied

// usage
baz.getBaz();
baz.setBaz();
// etc. ...

 

⭐️참고로 TypeScript 말고 JavaScript 에서도 잘 작동한다

 

📍단점

대규모 프로젝트에서는 성능 저하를 초래할 수 있다. 그래서 일반적으로는 사용이 권장되지 않는다

 

참고

https://basarat.gitbook.io/typescript/main-1/barrel

 

Barrel - TypeScript Deep Dive

Instead of exporting *, you can choose to export the module in a name. E.g., assume that baz.ts has functions:

basarat.gitbook.io

 

https://marvinh.dev/blog/speeding-up-javascript-ecosystem-part-7/

 

Speeding up the JavaScript ecosystem - The barrel file debacle

Many projects are littered with files that just re-export other files. These so called "barrel files" are one of the key reasons why JavaScript tooling is slow in bigger projects.

marvinh.dev

 

'🎨 프론트엔드 공부/JS & TS' 카테고리의 다른 글
  • [MacOS] 크롬 브라우저에서 그래픽 가속 사용 감지하기
  • [TypeScript] 객체의 배열에서 특정 키의 모든 밸류를 union type으로 추출하기
  • ES2023 새로운 Array 메서드 알아보기
  • #51 의존성 분리를 위해 미러 타입 사용하기
지식물원
지식물원
지식이 자라는 식물원!
  • 지식물원
    지식물원
    지식물원
  • 전체
    오늘
    어제
    • 분류 전체보기 (510)
      • 🎨 프론트엔드 공부 (247)
        • JS & TS (86)
        • HTML & CSS (22)
        • React & Next (49)
        • Vue & Nuxt (22)
        • 기타 (68)
      • 🤓 기술 학습 & 공부 기록 (116)
        • Node.js (0)
        • Python (37)
        • 백엔드 (0)
        • 딥러닝 (1)
        • 컴퓨터 일반 (72)
        • 개발 인프라 (6)
      • 👨‍💻 프로젝트 경험 (6)
        • Work (0)
        • Toy (6)
      • ⚙️ 개발 팁 & 노하우 (21)
        • 프론트엔드 (6)
        • 기타 (15)
      • ☕️ 커리어 & 인터뷰 준비 (88)
        • 코딩 테스트 (88)
      • 📰 기술 트렌드 & 생각 정리 (4)
      • 📚 기타 (25)
        • 마케팅 (15)
        • 비개발서적 (10)
  • 블로그 메뉴

    • 태그
  • 링크

  • 공지사항

    • 모바일 접속 시 코드 하이라이팅 깨질 때
  • 인기 글

  • hELLO· Designed By정상우.v4.10.3
지식물원
[TypeScript] Barrel의 장점과 단점
상단으로

티스토리툴바