All Articles

ํƒ€์ž…์Šคํฌ๋ฆฝํŠธ์˜ project reference ๋ง›๋ณด๊ธฐ

์–ธ์ œ ์“ฐ๋‚˜?

ํƒ€์ž…์Šคํฌ๋ฆฝํŠธ๋ฅผ ์ด์šฉํ•ด์„œ ๋ชจ๋…ธ๋ฆฌํฌ๋ฅผ ๊ตฌ์„ฑํ•˜๋‹ค๋ณด๋ฉด ์˜์กดํ•˜๊ณ  ์žˆ๋Š” ๋ชจ๋“ˆ๋“ค์„ ์–ด๋–ป๊ฒŒ ๋นŒ๋“œํ• ๊นŒ๊ฐ€ ๋ฌธ์ œ๊ฐ€ ๋œ๋‹ค.

๊ฐ ๋ชจ๋“ˆ์˜ package.json์˜ main, type ํ•„๋“œ๋ฅผ ํŠธ๋žœ์ŠคํŒŒ์ผ๋˜์ง€ ์•Š์€ ํƒ€์ž…์Šคํฌ๋ฆฝํŠธ ํŒŒ์ผ๋กœ ์ง€์ •ํ•˜๊ณ  ์ผ๋‹จ ๊ฐœ๋ฐœ์„ ํ•˜๊ณ , ํŠธ๋žœ์ŠคํŒŒ์ผ๋œ ๊ฒฐ๊ณผ๋ฅผ ์‹ค์ œ js๋กœ ๋Œ๋ฆด ๋•Œ๋Š” ๋ณ„๋„๋กœ ๋ชจ๋“ˆ๋งคํ•‘์„ ํ•ด์ฃผ๋Š” ๋ฐฉ๋ฒ•๋„ ์žˆ์ง€๋งŒ ์ผ๋‹จ ์ด ๊ธ€์—์„œ๋Š” ๊ฐ ํŒจํ‚ค์ง€๋ฅผ js๋กœ ๋นŒ๋“œํ•œ ํ›„ ์ผ๋ฐ˜์ ์ธ node.js ๋ชจ๋“ˆ์ฒ˜๋Ÿผ importํ•ด์„œ ์‚ฌ์šฉํ•˜๋Š” ๊ฒฝ์šฐ๋ฅผ ๋‹ค๋ฃฌ๋‹ค.

yarn workspace ์•„๋ž˜์— @shik/a, @shik/b, @shik/c 3๊ฐœ ํ”„๋กœ์ ํŠธ๊ฐ€ ์žˆ๊ณ  (์ดํ•˜ a, b, c), a ํ”„๋กœ์ ํŠธ๊ฐ€ b, c ํ”„๋กœ์ ํŠธ๋ฅผ ๋ถˆ๋Ÿฌ์™€์„œ ์‚ฌ์šฉํ•˜๋Š” ๊ฒฝ์šฐ๋ฅผ ๊ฐ€์ •ํ•ด๋ณด์ž ๋ฆฌํฌ ๋งํฌ. a ํ”„๋กœ์ ํŠธ๋ฅผ ํŠธ๋žœ์ŠคํŒŒ์ผ ํ•˜๋ ค๋ฉด b, c ํ”„๋กœ์ ํŠธ๋ฅผ ๋นŒ๋“œํ•ด์•ผ ํ•˜๋Š”๋ฐ ์ด๊ฑธ yarn workspaces run tsc ์ด๋Ÿฐ ์‹์œผ๋กœ ๋™์‹œ์— ๋Œ๋ฆด ์ˆ˜๋„ ์—†๊ณ  (์˜์กดํ•˜๋Š” ํŒจํ‚ค์ง€ ๋นŒ๋“œ๊ฐ€ ๋จผ์ € ๋๋‚˜์•ผ ๋‹ค์Œ ํŒจํ‚ค์ง€ ๋นŒ๋“œ๋ฅผ ํ•  ์ˆ˜๊ฐ€ ์žˆ๋‹ค)โ€ฆ๊ฒฐ๊ตญ ๋นŒ๋“œ ์Šคํฌ๋ฆฝํŠธ๊ฐ€ ์ง€์ €๋ถ„ํ•ด์ง€๊ธฐ ๋งˆ๋ จ์ด๋‹ค.

ํƒ€์ž…์Šคํฌ๋ฆฝํŠธ์˜ Project reference๋ผ๋Š” ๊ธฐ๋Šฅ์€ ์ด๋Ÿฐ ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•ด์ค€๋‹ค. a ํ”„๋กœ์ ํŠธ์˜ tsconfig.json์˜ references ํ•„๋“œ์— ์˜์กดํ•˜๋Š” ๋‹ค๋ฅธ ํŒจํ‚ค์ง€ (b,c)๋ฅผ ์ ์–ด์ฃผ๊ณ , ๊ทธ ํŒจํ‚ค์ง€์˜ tsconfig.json์—์„œ composite ์˜ต์…˜์„ ์ผœ์ฃผ๋ฉด ์ด์ œ tsc๋กœ a๋ฅผ ๋นŒ๋“œํ•˜๋ฉด ์•Œ์•„์„œ ์˜์กด ๊ด€๊ณ„์— ๋”ฐ๋ผ ๋จผ์ € b,c๋ฅผ ํŠธ๋žœ์ŠคํŒŒ์ผํ•˜๊ณ  a๋ฅผ ํŠธ๋žœ์ŠคํŒŒ์ผ ํ•ด์ค€๋‹ค. ๋‹ค์Œ ๋ฒˆ์— ๋นŒ๋“œ๋ฅผ ํ•˜๋”๋ผ๋„ ์ƒˆ๋กœ ๋นŒ๋“œ๊ฐ€ ํ•„์š”ํ•œ ๋ถ€๋ถ„๋งŒ ๋‹ค์‹œ ๋นŒ๋“œํ•ด์ค˜์„œ ๋ถˆํ•„์š”ํ•˜๊ฒŒ ๋ชจ๋“  ํ”„๋กœ์ ํŠธ๊ฐ€ ๋‹ค์‹œ ๋นŒ๋“œ๋˜์ง€ ์•Š๊ฒŒ ํ•ด์ค€๋‹ค.

์ฒ˜์Œ ๋นŒ๋“œ ํ•  ๋•Œ

์˜์กดํ•˜๋Š” ํ”„๋กœ์ ํŠธ๋ฅผ ์ฐพ์•„์„œ ์ˆœ์„œ๋Œ€๋กœ ํŠธ๋žœ์ŠคํŒŒ์ผ ํ•ด์ค€๋‹ค.

$ git clone git@github.com:zxzl/tasting-typescript-project-reference.git && cd tasting-typescript-project-reference
$ yarn
$ yarn workspace @shik/a tsc -b -v
[10:50:45 PM] Projects in this build:
    * ../b/tsconfig.json
    * ../c/tsconfig.json
    * tsconfig.json

[10:50:45 PM] Project '../b/tsconfig.json' is out of date because output file '../b/dist/src/index.js' does not exist

[10:50:45 PM] Building project 'somedirectory/packages/b/tsconfig.json'...

[10:50:46 PM] Project '../c/tsconfig.json' is out of date because output file '../c/dist/src/index.js' does not exist

[10:50:46 PM] Building project 'somedirectory/packages/c/tsconfig.json'...

[10:50:46 PM] Project 'tsconfig.json' is out of date because output file 'dist/src/index.js' does not exist

[10:50:46 PM] Building project 'somedirectory/packages/a/tsconfig.json'...

ํŒŒ์ผ์ด ๋ฐ”๋€” ๋•Œ

์—ฌ๊ธฐ์„œ ๋งŒ์•ฝ b๋ฅผ ์ˆ˜์ •ํ•œ๋‹ค๋ฉด..? b, a๋งŒ ๋‹ค์‹œ ํŠธ๋žœ์ŠคํŒŒ์ผ๋˜๊ณ  c๋Š” ํŠธ๋žœ์ŠคํŒŒ์ผ ๋˜์ง€ ์•Š๋Š”๋‹ค.

// packages/b/src/index.ts
export const add = (a: number, b: number) => a + b; // old

export const add = (a: number, b: number, c?: number) => a + b; //new
$ yarn workspace @shik/a tsc -b -v
[10:52:31 PM] Projects in this build:
    * ../b/tsconfig.json
    * ../c/tsconfig.json
    * tsconfig.json

[10:52:31 PM] Project '../b/tsconfig.json' is out of date because oldest output '../b/dist/src/index.js' is older than newest input '../b/src/index.ts'

[10:52:31 PM] Building project 'somedirectory/packages/b/tsconfig.json'...

[10:52:31 PM] Project '../c/tsconfig.json' is up to date because newest input '../c/src/index.ts' is older than oldest output '../c/dist/src/index.js'

[10:52:31 PM] Project 'tsconfig.json' is out of date because oldest output 'dist/src/index.js' is older than newest input '../b'

[10:52:31 PM] Building project 'somedirectory/packages/a/tsconfig.json'...

์ฐธ์กฐํ•œ ๊ธ€๋“ค

Published Jun 17, 2020

If I keep marking the dots, someday they will ๐Ÿ”—๐Ÿ”—