Parcel Basic - ChoDragon9/posts GitHub Wiki

์†Œ๊ฐœ

Parcel๋Š” ๋ชจ๋“ˆ ๋ฒˆ๋“ค๋Ÿฌ๋กœ ์‚ฌ์šฉ๋ฅ ์ด ๋†’์€ webpack๊ณผ ๊ฐ™์€ ์—ญํ• ์„ ํ•œ๋‹ค. ํ”Œ๋Ÿฌ๊ทธ์ธ์„ ์ถ”๊ฐ€ํ•ด ์ปดํฌ๋„ŒํŠธ ํ™•์žฅ์ž( vue, react ๋“ฑ)๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.

์žฅ์ 

Parcel๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ์–ป์„ ์ˆ˜ ์žˆ๋Š” ์žฅ์ ์€ ์ดˆ๊ธฐ ํ”„๋กœ์ ํŠธ ์„ธํŒ… ๋น„์šฉ์„ ์ ˆ์•ฝํ•  ์ˆ˜ ์žˆ์œผ๋ฉฐ ์ž์„ธํ•œ ๊ฒƒ์€ ์•„๋ž˜์™€ ๊ฐ™๋‹ค.

  • Zero Configuation : ์„ค์ •์—†์ด TypeScript/ES6+/Hot module ์ง€์›
  • Entry ํฌ์ธํŠธ๋ฅผ HTML๋กœ ์ง€์ • ๊ฐ€๋Šฅ
  • Code Splitting : import() ์‚ฌ์šฉ์œผ๋กœ ์ž๋™์œผ๋กœ ์ฝ”๋“œ ๋ถ„ํ• ํ•ด์คŒ
  • ํŒŒ์ผ๋ช… Hash Code : ์บ์‹œ ๋ฐฉ์ง€์— ํ•„์š”ํ•œ hash code๋ฅผ ์ž๋™์œผ๋กœ ํ•ด์คŒ

๋‹จ์ 

  • ์„ค์น˜๋˜์ง€ ์•Š์€ ๋ชจ๋“ˆ์„ ๋กœ๋“œํ•˜๋ฉด ์„œ๋ฒ„๋ฅผ ์žฌ์‹คํ–‰ํ•ด์•ผ ํ•จ
    • import('./foo')๋ฅผ ํ•ด์•ผํ•˜๋Š” ๋ฐ ์‹ค์ˆ˜๋กœ import('foo')๋ฅผ ํ–ˆ์—ˆ๋‹ค๋ฉด Error ๋ฐœ์ƒ

์‹คํ–‰

์„ค์น˜

$ npm init -y
$ npm install --save-dev parcel-bundler

ํ•ซ๋ชจ๋“ˆ ์‚ฌ์šฉ ๋ฐ ๋นŒ๋“œ

package.json

  "scripts": {
    "dev": "parcel index.html", //ํ•ซ ๋ชจ๋“ˆ
    "build": "parcel build index.html" //๋นŒ๋“œ
  },

index.html

<html>
<body>
  <script src="./index.js"></script>
</body>
</html>

index.js

import foo from './foo'
foo()

foo.js

export default () => {
  console.log("Foo")
}

dev๋‚˜ build๋ฅผ ์‹คํ–‰ํ•˜๋ฉด ์ฝ”๋“œ๊ฐ€ ๋™์ž‘๋˜๋Š” ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค.

์ฝ”๋“œ ๋ถ„ํ• 

import() ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ํ•ด๋‹น ํŒŒ์ผ์€ ๋ฒˆ๋“ค๋ง์— ํฌํ•จํ•˜์ง€ ์•Š๊ณ  ํŒŒ์ผ๋กœ ๋งŒ๋“ค์–ด ์ค€๋‹ค. Promise๋ฅผ ๋ฆฌํ„ดํ•˜๋ฉฐ ์ฒซ๋ฒˆ์งธ๋กœ ๋ฐ›๋Š” argument๋Š” exportํ•œ ๊ฒƒ์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.

import('./foo').then(foo => foo.default())
โš ๏ธ **GitHub.com Fallback** โš ๏ธ