Webpack Basic - ChoDragon9/posts GitHub Wiki

Install

$ mkdir hello-webpack
$ cd hello-webpack
$ npm init
$ npm i webpack webpack-cli -D

Getting start

webpack.config.js

const path = require('path')

module.exports = {
  entry: './entry.js',
  output: {
    path: path.resolve(__dirname,'dist'),
    filename: 'bundle.js'
  }
}

์ƒ˜ํ”Œ ์ฝ”๋“œ ๋งŒ๋“ค๊ธฐ

entry.js
import { foo } from './foo'
import { bar } from './bar'

foo()
bar()
bar.js
export const bar = {
  bar () {
    console.log("bar")
  }
}
foo.js
export const foo = {
  foo () {
    console.log("foo")
  }
}

build

package.json ์— build๋ช…๋ น์–ด ์ถ”๊ฐ€
"scripts": {
  "build": "webpack --config webpack.config.js"
}
webpack build
$ npm run build
โ”œโ”€โ”€ dist
     โ””โ”€โ”€ bundle.js

Webpack Nodejs

webpack์—์„œ nodejs ์ฝ”๋“œ๋ฅผ ๋นŒ๋“œํ•˜๋ ค๋ฉด target๋ฅผ ์ง€์ •ํ•ด์ค˜์•ผ ํ•œ๋‹ค.

module.exports = {
  entry: './entry-server.js',
  target: 'node'
}