Webpack๊ณผ Babel

๊ฐ•์€๋น„ยท2022๋…„ 1์›” 10์ผ
0

React

๋ชฉ๋ก ๋ณด๊ธฐ
3/36
post-custom-banner

๐Ÿ“Œ Webpack์ด๋ž€?

  • ์˜คํ”ˆ ์†Œ์Šค ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ๋ชจ๋“ˆ ๋ฒˆ๋“ค๋Ÿฌ์ด๋‹ค.
  • ์ด๋•Œ ๋ชจ๋“ˆ์€ ์›น ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์„ ๊ตฌ์„ฑํ•˜๋Š” ๋ชจ๋“  ์ž์› (HTML, CSS, JS, Images ๋“ฑ)์— ํ•ด๋‹นํ•œ๋‹ค. (์ฆ‰, ์›น ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์„ ์ œ์ž‘ํ•˜๋Š”๋ฐ ์‚ฌ์šฉ๋˜๋Š” ํŒŒ์ผ์„ ๋งํ•œ๋‹ค.)
  • Entry point๋ถ€ํ„ฐ ์‹œ์ž‘ํ•˜์—ฌ dependency graph๋ฅผ ๋งŒ๋“ค๊ณ  ์ด๋ฅผ ๊ธฐ๋ฐ˜์œผ๋กœ ๋ชจ๋“ˆ๋“ค์„ bundlingํ•œ๋‹ค.


๐Ÿ’ก Webpack์€ ์™œ ์‚ฌ์šฉํ•ด์•ผ ํ• ๊นŒ?

  • ์ฝ”๋“œ๊ฐ€ ๊ธธ๊ณ  ๋งŽ์•„์งˆ์ˆ˜๋ก ๊ด€๋ฆฌํ•˜๊ธฐ๊ฐ€ ์–ด๋ ต๋‹ค.
  • ๊ทธ๋ฆฌ๊ณ  ๋งŽ์€ ์–‘์˜ ํŒŒ์ผ(์ฝ”๋“œ)์„ ๋ธŒ๋ผ์šฐ์ €์— ๋กœ๋“œํ•˜๋Š”๋ฐ ์‹œ๊ฐ„์ด ์˜ค๋ž˜ ๊ฑธ๋ฆฌ๊ณ , ํŠน์ • ์ƒํ™ฉ์— ๋„คํŠธ์›Œํฌ ๋น„์šฉ์ด ๋ฐœ์ƒํ•  ์ˆ˜ ์žˆ๋‹ค.
  • Webpack์„ ์‚ฌ์šฉํ•˜๊ธฐ ์ „ CommonJS๋‚˜ AMD๋กœ ํŒŒ์ผ๋ณ„๋กœ ๋ชจ๋“ˆ์„ ๊ด€๋ฆฌํ•  ์ˆ˜ ์žˆ์—ˆ์ง€๋งŒ, ์—ฌ์ „ํžˆ ๋ธŒ๋ผ์šฐ์ €์—์„œ๋Š” ์‰ฝ์ง€ ์•Š๋‹ค๊ณ  ํ•œ๋‹ค.
  • SPA(Single Page Application)์ด ์ถ”์„ธ์ด๊ณ , ๋ธŒ๋ผ์šฐ์ €๋ฅผ ์ตœ์ ํ™” ํ•˜๋Š” ๊ฒƒ์€ ์›น ํ”„๋ก ํŠธ์—”๋“œ์—๊ฒŒ ๊ผญ ํ•„์š”ํ•œ ์š”๊ตฌ ์กฐ๊ฑด์ด๋‹ค.

๐Ÿ’ก Webpack์˜ ์ฃผ์š” ๊ฐœ๋…

  • entry
    • Webpack์€ ๋ชจ๋“  ํŒŒ์ผ์„ ๋ชจ๋“ˆ๋กœ ๊ด€๋ฆฌํ•˜๊ณ  ๋ชจ๋“ˆ์ด ๋งŽ์•„์งˆ์ˆ˜๋ก ์˜์กด์„ฑ์ด ๋†’์•„์ง„๋‹ค.
    • ๊ทธ ์˜์กด์„ฑ์˜ ์‹œ์ž‘์ ์„ entry ๋ผ๊ณ  ํ•œ๋‹ค.
    • ์‰ฝ๊ฒŒ ๋งํ•˜์ž๋ฉด ํ”„๋กœ์ ํŠธ๋ฅผ ๋ถˆ๋Ÿฌ์˜ฌ ๋•Œ ๊ฐ€์žฅ ๋จผ์ € ๋ถˆ๋Ÿฌ์˜ค๋Š” ํŒŒ์ผ
const config: webpack.Configuration = merge(baseConfig, {
    mode: 'production',

    entry: './src/index.tsx',
    
  • output: ๋ฒˆ๋“ค๋œ ํŒŒ์ผ์˜ ์œ„์น˜์™€ ํŒŒ์ผ๋ช… ์ง€์ •
    output: {
        path: path.resolve(__dirname, '..', 'dist'),
        publicPath: '../dist/',
        filename: 'bundle.js'
    },
  • loader
    • Webpack์€ ์‚ฌ์‹ค javascript์™€ JSON ๋งŒ ๋นŒ๋“œํ•  ์ˆ˜ ์žˆ๋‹ค.
    • ๋‹ค๋ฅธ ์ž์› (HTML, CSS, Image)์„ ๋นŒ๋“œํ•  ์ˆ˜ ์žˆ๋„๋ก ๋„์™€์ฃผ๋Š” ์†์„ฑ
    • css-loader, file-loader, babel-loader
            /**
             * Transfile TS files or TSX files to JS files.
             * And run babel at JS files to transfile ES6+ code to ES5.
             */
            {
                test: /\.tsx?$/,
                use: {
                    loader: 'awesome-typescript-loader',
                    options: {
                        silent: true,
                        useBabel: true,
                        useCache : true,
                        babelCore: '@babel/core',
                        babelOptions: {
                            babelrc: false,
                            presets: babelPresets
                        }
                    }
                }
            },
  • plugin
    • Webpack์˜ ๊ธฐ๋ณธ์ ์ธ ๋™์ž‘์— ์ถ”๊ฐ€์ ์ธ ๊ธฐ๋Šฅ์„ ์ œ๊ณตํ•˜๋Š” ์†์„ฑ์ด๋‹ค.
    • loader๋Š” ํŒŒ์ผ์„ ํ•ด์„ํ•˜๊ณ  ๋ณ€ํ™˜ํ•˜๋Š” ๊ณผ์ •์— ๊ด€์—ฌํ•˜๋Š” ๋ฐ˜๋ฉด, plugin์€ ํ•ด๋‹น ๊ฒฐ๊ณผ๋ฌผ์˜ ํ˜•ํƒœ๋ฅผ ๋ฐ”๊พธ๋Š” ์—ญํ• ์„ ํ•œ๋‹ค.
    • ๊ฒฐ๊ณผ๋ฌผ์—์„œ ์Šคํƒ€์ผ ์ฝ”๋“œ๋งŒ ๋ฝ‘์•„์„œ ๋ณ„๋„ css ํŒŒ์ผ๋กœ ๋งŒ๋“ค์–ด ์—ญํ• ์— ๋”ฐ๋ผ์„œ ํŒŒ์ผ์„ ๋ถ„๋ฆฌํ•˜๋Š” ๊ฒƒ์ด ๊ฐ€๋Šฅํ•˜๋‹ค.
    plugins: [
        /**
         * Add production environment.
         * Ref: https://webpack.js.org/plugins/define-plugin/
         */
        new webpack.DefinePlugin({
            'process.env.NODE_ENV': JSON.stringify('production'),
            'process.env.FLUENTFFMPEG_COV': false
        }),

๐Ÿ“Œ Babel์ด๋ž€?

  • ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ES6 ๋ฌธ๋ฒ•์„ ES5๋กœ ๋ณ€ํ™˜ํ•ด์ฃผ๋Š” ํŠธ๋ Œ์ŠคํŒŒ์ผ๋Ÿฌ(transpiler)์ด๋‹ค.
  • ๋ธŒ๋ผ์šฐ์ €๋“ค์ด ์ตœ์‹  Javscript ์ฝ”๋“œ๋ฅผ ์ฝ์ง€ ๋ชปํ•˜๋Š” ๊ฒƒ์„ ์œ„ํ•ด ์ตœ์‹  ๋ฒ„์ „์˜ Javascript๋ฅผ ๋ธŒ๋ผ์šฐ์ €๊ฐ€ ์ฝ์„ ์ˆ˜ ์žˆ๋„๋ก ์˜›๋‚  ๋ฒ„์ „์˜ Javascript ์ฝ”๋“œ๋กœ ๋ณ€ํ™˜ํ•ด์ฃผ๋Š” ์—ญํ• ์„ ํ•œ๋‹ค.
  • Babel ๋™์ž‘์€ ์„ธ ๋‹จ๊ณ„๋กœ ๊ตฌ๋ถ„๋œ๋‹ค.
    • ํŒŒ์‹ฑ (Parsing) : ์ฝ”๋“œ๋ฅผ ์ฝ๊ณ  ์ถ”์ƒ ๊ตฌ๋ฌธ ํŠธ๋ฆฌ(AST)๋กœ ๋ณ€ํ™˜ํ•˜๋Š” ๋‹จ๊ณ„
    • ๋ณ€ํ™˜ (Transsforming) : ์ถ”์ƒ ๊ตฌ๋ฌธ ํŠธ๋ฆฌ๋ฅผ ๋ณ€๊ฒฝํ•˜๋Š” ๋‹จ๊ณ„
    • ์ถœ๋ ฅ (Printing) : ๋ณ€๊ฒฝ๋œ ๊ฒฐ๊ณผ๋ฌผ์„ ์ถœ๋ ฅํ•˜๋Š” ๋‹จ๊ณ„
    • Babel์ด ๋‹ด๋‹นํ•˜๋Š” ๊ฒƒ์€ ํŒŒ์‹ฑ๊ณผ ์ถœ๋ ฅ์ด๊ณ  ๋ณ€ํ™˜ ๋‹จ๊ณ„๋Š” Babel Plugin์ด ๋‹ด๋‹นํ•œ๋‹ค.
    • ํ”Œ๋Ÿฌ๊ทธ์ธ์€ ์–ด๋–ค ์ฝ”๋“œ๋ฅผ ์–ด๋–ป๊ฒŒ ๋ณ€ํ™˜ํ• ์ง€์— ๋Œ€ํ•œ ๊ทœ์น™์„ ์ •์˜ํ•œ๋‹ค. (ํ”Œ๋Ÿฌ๊ทธ์ธ๋“ค์„ ๋ชฉ์ ์— ๋”ฐ๋ผ ๋ฌถ์–ด๋†“์€ ์„ธํŠธ๋ฅผ preset์ด๋ผ๊ณ  ํ•œ๋‹ค.)
    • ์ถ”์ƒ ๊ตฌ๋ฌธ ํŠธ๋ฆฌ: ํ”„๋กœ๊ทธ๋žจ ๋‚ด์—์„œ ๋ฐœ์ƒํ•˜๋Š” ๊ธฐ๋Šฅ์„ ๋‚˜ํƒ€๋‚ด๊ธฐ ์œ„ํ•ด ๋งŒ๋“ค์–ด์ง„ ๊ตฌ๋ฌธ ๊ตฌ์กฐ์ด๋‹ค. ์ฝ”๋“œ๋ฅผ ๊ตฌ์กฐํ™”๋œ ํŠธ๋ฆฌ๋กœ ๋ณ€ํ™˜
post-custom-banner

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