ํ์ ์คํฌ๋ฆฝํธ๋ฅผ ์ด์ฉํด์ ๋ชจ๋ ธ๋ฆฌํฌ๋ฅผ ๊ตฌ์ฑํ๋ค๋ณด๋ฉด ์์กดํ๊ณ ์๋ ๋ชจ๋๋ค์ ์ด๋ป๊ฒ ๋น๋ํ ๊น๊ฐ ๋ฌธ์ ๊ฐ ๋๋ค.
๊ฐ ๋ชจ๋์ 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'...