๐Ÿ“’[TypeScript ๋งˆ์Šคํ„ฐ with Webpack & React] Step16.Webpack๊ณผ TypeScript

๊ถŒ์šฉ์ค€ยท2023๋…„ 12์›” 6์ผ
0
post-thumbnail

1. Webpack์€ ์™œ ์“ธ๊นŒ์š”?


Webpack์€ ์˜์กด์„ฑ์„ ๊ฐ€์ง„ ์ˆ˜์‹ญ,์ˆ˜๋ฐฑ ๊ฐœ์˜ ํŒŒ์ผ๋กœ ๊ตฌ์„ฑ๋œ ๋ณต์žกํ•œ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ์ฒ˜๋ฆฌ๋ฅผ ๋•๋Š”๋‹ค.

Webpack์€ ๊ฐ๊ธฐ ๋‹ค๋ฅธ ์—์…‹์„ ๋ชจ์•„ ์ •์  ์—์…‹์œผ๋กœ ๋ฒˆ๋“ค๋งํ•œ๋‹ค. ์ˆ˜๋งŽ์€ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ํฌํ•จํ•˜๋Š” ๋ฐฑ์—ฌ ๊ฐœ์˜ JavaScript ํŒŒ์ผ์ด ํ•˜๋‚˜๋กœ ํ•ฉ์ณ์ ธ์„œ ํ•œ ์Šคํฌ๋ฆฝํŠธ๋กœ ๋ถˆ๋Ÿฌ์˜ฌ ์ˆ˜ ์žˆ๋Š” ๋ฒˆ๋“ค ํŒŒ์ผ์ด ๋œ๋‹ค.

2. Webpack ์˜์กด์„ฑ ์„ค์น˜ํ•˜๊ธฐ


Webpack ํŒจํ‚ค์ง€ ์„ค์น˜

Terminal
npm install --save-dev webpack webpack-cli typescript ts-loader

Webpack์€ ๋ชจ๋“ˆ ๋ฒˆ๋“ค๋Ÿฌ์ด์ง€๋งŒ Webpack์„ ๋ช…๋ น์ค„์—์„œ ์‚ฌ์šฉํ•˜๊ฑฐ๋‚˜ package.json ํŒŒ์ผ ๋‚ด์—์„œ ํ˜ธ์ถœํ•˜๋ ค๋ฉด
webpack-cli๊ฐ€ ํ•„์š”ํ•˜๋‹ค. ๊ทธ๋ž˜์„œ ๋‘˜์€ ํ•จ๊ป˜ ์‚ฌ์šฉ๋œ๋‹ค.

webpack-cli ๋Š” Webpack์˜ ๋ช…๋ น์ค„ ์ธํ„ฐํŽ˜์ด์Šค๋กœ Webpack ์—†์ด๋Š” ์‚ฌ์šฉํ•  ์ˆ˜ ์—†์œผ๋‚˜ Webpack์— ํฌํ•จ์ด ๋˜์–ด ์žˆ์ง€๋Š” ์•Š๋Š” ๊ฐœ๋ณ„ ํŒจํ‚ค์ง€์ด๋‹ค.

typescript๋Š” package.json์— TypeScript๋ฅผ ํฌํ•จ์‹œํ‚ค๊ธฐ์œ„ํ•ด์„œ ๋‹ค์šด๋กœ๋“œ ๋ฐ›์•˜๋‹ค. ์ด์œ ๋Š” ํ˜„์žฌ ์ž‘์—…ํ•˜๊ณ  ์žˆ๋Š” ๋ฒ„์ „์„ ๋ช…์‹œํ•˜๊ธฐ์œ„ํ•ด์„œ์ด๋‹ค.

ts-loader๋Š” TypeScript์™€ Webpack ์‚ฌ์ด์˜ ์ค‘๊ฐ„์ž ์—ญํ• ์„ ํ•œ๋‹ค. TypeScript๋ฅผ ํ˜ธ์ถœํ•ด์„œ ์ฆ‰, tsc ๋ช…๋ น์–ด๋ฅผ ํ˜ธ์ถœํ•ด TypeScript๋ฅผ Javascript๋กœ ์ปดํŒŒ์ผ๋งํ•œ ๋’ค ์ด๋ฅผ ๋ชจ๋‘ ๋ฒˆ๋“ค๋งํ•˜๊ฒŒ ๋  Webpack์œผ๋กœ ์ „๋‹ฌํ•˜๋Š” ์—ญํ• ์„ ํ•œ๋‹ค.

3. Webpack ๊ธฐ์ดˆ ๊ตฌ์„ฑ


entry

entry ํ”„๋กœํผํ‹ฐ๋Š” Webpack์—๊ฒŒ ๋ฒˆ๋“ค๋ง์„ ์‹œ์ž‘ํ•  ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์˜ ์‹œ์ž‘์ ์„ ์ง€์ •ํ•ด ์ค€๋‹ค.

module

module ํ”„๋กœํผํ‹ฐ๋Š” ๊ฐ์ฒด์ธ๋ฐ ์—ฌ๊ธฐ์—๋Š” ๋ฐฐ์—ด์ด ๋  rules ํ”„๋กœํผํ‹ฐ๊ฐ€ ํฌํ•จ๋˜๋ฉฐ, ์—ฌ๋Ÿฌ ๊ฐœ์˜ ๊ทœ์น™์„ ์ถ”๊ฐ€ํ•  ์ˆ˜ ์žˆ๋‹ค.

ts๋‚˜ tsx๋กœ ๋๋‚˜๋Š” ํŒŒ์ผ์ด์˜ค๋ฉด ts-loader๋ฅผ ์‚ฌ์šฉํ•˜๋ผ๋Š” ์˜๋ฏธ.

resolve

resolve ํ”„๋กœํผํ‹ฐ๋Š” ๊ฐ์ฒด๋กœ extensions์—๋Š” Webpack์ด resolveํ•  ์ˆ˜ ์žˆ๋Š” ํ™•์žฅ์ž์˜ ๋ฆฌ์ŠคํŠธ๊ฐ€ ๋“ค์–ด๊ฐ„๋‹ค.

webpack.config.js
const path = require('path');

module.exports = {
  entry: './src/index.ts',
  module: {
    rules: [
      {
        test: /\.tsx?$/,
        use: 'ts-loader',
        exclude: /node_modules/,
      },
    ],
  },
  resolve: {
    extensions: ['.tsx', '.ts', '.js'],
  },
  output: {
    filename: 'bundle.js',
    path: path.resolve(__dirname, 'dist'),
  },
};

4. ์†Œ์Šค๋งต ์ถ”๊ฐ€ํ•˜๊ธฐ

์†Œ์Šค ๋งต์€ ๋ฐฐํฌ์šฉ์œผ๋กœ ๋นŒ๋“œํ•œ ํŒŒ์ผ๊ณผ ์›๋ณธ ํŒŒ์ผ์„ ์„œ๋กœ ์—ฐ๊ฒฐ์‹œ์ผœ์ฃผ๋Š” ๊ธฐ๋Šฅ์ด๋‹ค.
๋ณดํ†ต ์„œ๋ฒ„์— ๋ฐฐํฌ๋ฅผ ํ•  ๋•Œ ์„ฑ๋Šฅ ์ตœ์ ํ™”๋ฅผ ์œ„ํ•ด HTML, CSS, JS์™€ ๊ฐ™์€ ์›น ์ž์›๋“ค์„ ์••์ถ•ํ•˜๋Š”๋ฐ ๋งŒ์•ฝ ์••์ถ•ํ•˜์—ฌ ๋ฐฐํฌํ•œ ํŒŒ์ผ์—์„œ ์—๋Ÿฌ๊ฐ€ ๋‚œ๋‹ค๋ฉด ์–ด๋–ป๊ฒŒ ๋””๋ฒ„๊น…์„ ํ•  ์ˆ˜ ์—†์–ด ์†Œ์Šค ๋งต์„ ์ด์šฉํ•ด ๋ฐฐํฌ์šฉ ํŒŒ์ผ์˜ ํŠน์ • ๋ถ€๋ถ„์ด ์›๋ณธ ์†Œ์Šค์˜ ์–ด๋–ค ๋ถ€๋ถ„์ธ์ง€ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค.

tsconfig.json
"compilerOptions": {
  "sourceMap": true
}
webpack.config.js
module.exports = {
    devtool: 'inline-source-map'
}
profile
Brendan Eich, Jordan Walke, Evan You, ๊ถŒ์šฉ์ค€

0๊ฐœ์˜ ๋Œ“๊ธ€