INEEDACHACHA

Flutter Pub Workspaces 구조 – 현재 프로젝트 기준 본문

Flutter/전환실록

Flutter Pub Workspaces 구조 – 현재 프로젝트 기준

INEEDACHACHA 2025. 7. 15. 10:09

개요

  • doctorvice_pro_flutter 프로젝트는 Flutter 공식 Pub Workspaces를 기반으로 모듈화된 멀티 패키지(monorepo) 구조로 구성.
  • 이를 통해 각 기능, 도메인, 공통 코드 등을 독립적인 패키지로 나누고, 통합된 앱(apps/doctorvice)에서 이들을 조합해 사용.

Pub Workspaces의 목적

  • 각 레이어를 명확히 분리하여 기능 단위 개발 가능
  • 공통 코드는 공유 패키지로 재사용
  • DI, 테스트, 코드 생성(build_runner) 을 패키지 단위로 관리 가능
  • 루트에서 전체 의존성 통합 관리

디렉토리 구조

doctorvice_pro_flutter/
├── pubspec.yaml            
├── dart_tool/              
├── apps/
│   └── doctorvice/       
│       └── pubspec.yaml
├── packages/
│   ├── core/
│   │   └── network_foundation/
│   ├── data/
│   │   ├── base_data/
│   │   ├── network_data/
│   │   └── repository_data/
│   ├── domain/
│   │   ├── base_domain/
│   │   ├── entity/
│   │   └── usecase/
│   ├── feature/
│   │   ├── home_feature/
│   │   ├── auth_feature/
│   │   └── ... (기능별 분리)
│   ├── shared/
│   │   ├── design_system/
│   │   ├── platform/
│   │   ├── navigation/
│   │   └── global_third_party_library/
└── melos.yaml (도입 예정)

루트 pubspec.yaml 설정

name: doctorvice_pro_flutter
publish_to: 'none'

environment:
  sdk: ^3.8.0

dependencies:
  flutter:
    sdk: flutter

  # Network
  dio: ^5.4.0
  retrofit: ^4.4.0

  # DI
  injectable: ^2.4.0
  get_it: ^7.6.0

  # Bloc
  bloc: ^9.0.0

  # Router 
  go_router: ^16.0.0

  # Json Serialization
  json_annotation: ^4.8.1

  # Environment
  flutter_dotenv: ^5.1.0

  # Preference
  shared_preferences: ^2.5.3

  # Resource
  flutter_svg: ^2.2.0
  lottie: ^3.3.1

dev_dependencies:
  # Lint
  flutter_lints: ^5.0.0

  # Network
  retrofit_generator: '>=8.0.0 <10.0.0'

  # Json Serialization
  json_serializable: ^6.7.1

  # DI
  injectable_generator: ^2.4.0

  # Resource
  flutter_gen_runner: ^5.10.0

  # Code Generation
  build_runner: ^2.4.8

workspace:
  - apps/doctorvice
  - packages/core/network_foundation
  - packages/core/persistence_foundation
  - packages/data/base_data
  - packages/data/network_data
  - packages/data/repository_data
  - packages/domain/base_domain
  - packages/domain/entity
  - packages/domain/usecase
  - packages/feature/auth_feature
  - packages/feature/base_feature
  - packages/feature/feed_feature
  - packages/feature/home_feature
  - packages/feature/my_page_feature
  - packages/feature/notification_feature
  - packages/feature/record_feature
  - packages/shared/design_system
  - packages/shared/global_third_party_library
  - packages/shared/navigation
  - packages/shared/platform_shared                                                                              

  • workspace.members에는 포함할 모든 패키지를 glob 패턴으로 명시
  • 실제 앱 실행은 apps/doctorvice 기준으로 진행됨

각 패키지 pubspec.yaml 예시

예: packages/feature/home_feature/pubspec.yaml

name: home_feature
version: 0.0.1
publish_to: 'none'

environment:
  sdk: ^3.8.0

dependencies:
  flutter:
    sdk: flutter
  get_it:
  injectable:

  feed_feature: 
  record_feature:
  my_page_feature:
  notification_feature:
  auth_feature:
  base_feature:
  design_system:
  usecase: 
  repository_data:
  base_domain: 
  network_data: 
  base_data:
  network_foundation: 
  navigation:
  entity: 
  platform_shared:
  persistence_foundation:
  global_third_party_library:

dev_dependencies:
  flutter_test:
    sdk: flutter
  flutter_lints: 

resolution: workspace
  • 루트 yaml에서 Dependency들의 version을 작성했기 때문에 하위 yaml에서는 version과 path가 생략됨

주요 명령어 (루트에서 실행)

flutter pub get
flutter pub run build_runner build --delete-conflicting-outputs
  • 루트에서 실행 시 모든 패키지의 의존성을 한 번에 설치
  • 각 패키지마다 build_runner를 따로 실행할 수도 있음

장점 요약

항목 설명
모듈 분리 기능, 도메인, UI, 플랫폼, 외부 라이브러리 등 분리 개발
의존성 일관성 루트에서 한번에 pub 관리 가능
테스트 최적화 각 패키지 단위로 테스트 가능
자동화 도입 용이 melos, CI 등과 궁합 좋음
협업 친화적 여러 개발자가 동시에 다른 feature 작업 가능

주의 사항

  • 패키지 생성시 path 의존관계 설정 주의
  • 순환 참조가 발생하기 쉬운 구조
  • 공통 의존성은 shared/ 또는 global_third_party_library에 모아야 유지보수 편함