본문 바로가기

서버/Webpack

Webpack - 간단하게 config 파일을 만들어서 빌드해보자

728x90

개요)

실습1. webpack.config.js 파일을 생성해 webpack이 빌드할 때 커스텀 요소를 넣어보기

 


실습전 준비사항

1. node설치

2. webpack, webpack-cli 설치

npm install webpack webpack-cli --save-dev

 


 

 

실습1.

 

1. webpack.config.js 파일 생성

// can't use ecma script module. use only common.js
console.log("module >>> ", module);
const path = require('path');

// minimum option
module.exports = {
    entry: './src/index.js',
    output: {
        filename: 'bundle.js',
        path: path.resolve(__dirname, './dist')
    },
    mode: 'none'
}

 

 

entry: 묶기 시작할 시작점 파일

output.filename: 빌드 후 저장할 파일명

output.path: 빌드 후 저장할 디렉토리명

 

2. Script 파일 생성하기 

 

//index.js

import helloWorld from './hello-world.js';

helloWorld();

 

// hello-world.js

function helloWorld() {
    console.log("Hello World");
}

export default helloWorld;

 

index.js는 hell-world.js를 포함하고 있다.

 

표현하자면 아래와 같다. 

index.js = {helloword.js} 

 

그래서 entry에 index.js만 넣은 것이다.

index.js만 빌드하면 

helloworld.js까지 함께 빌드된다. 

 

3. webpack으로 빌드하기 

 

npx webpack

 

4. index.html에 빌드한 bundle.js를 넣어 브라우저로 열어보기

 

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <script src="./dist/bundle.js"></script>
</body>
</html>

 

복사 후 

브라우저 주소에 넣기

 

 

잘 나온다.