웹팩으로 번들링하기

김남경·2023년 1월 18일

react

목록 보기
21/37

기초

📗 npm init

npm init -y

📗 package.json 확인

//package.json
{
  "name": "webpack",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
}

파일 생성

📗 내용 만들기
src 폴더 아래 html, css, js 파일 생성

📗 확인

node src/index.js

웹팩

📗 설치하기

npm install -D webpack webpack-cli

devDependency 옵션을 설정한 상태

📗 webpack.config.js 생성하기

const path = require('path');

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

package.json과 같은 폴더 아래

번들링하기

📗 번들링

npx webpack

📗 확인

node dist/app.bundle.js

📗 단축 명령어 설정

//package.json
{
  //...
  "scripts": {
	"build": "webpack",
  },
  //...
}

CSS

📗 index.html 실행해보기
index.html 라이브 서버로 실행

Uncaught ReferenceError: require is not defined

📗 CSS 추가
dist/index.html 로 이동

// src/index.js
require('./style.css');

📗 확인

node src/index.js
SyntaxError: Unexpected token “*

📗 로더 설치

npm i -D css-loader style-loader

📗 webpack.config.js 로더 추가

// webpack.config.js
const path = require("path");

module.exports = {
  entry: "./src/index.js",
  output: {
    path: path.resolve(__dirname, "dist"),
    filename: "app.bundle.js",
  },
  module: {
    rules: [
      {
        test: /\.css$/,
        use: ["style-loader", "css-loader"],
        exclude: /node_modules/,
      },
    ],
  },
};

HTML

📗 플러그인 설치

npm i -D html-webpack-plugin

📗 webpack.config.js 플러그인 추가

//webpack.config.js
const path = require("path");
const HtmlWebpackPlugin = require('html-webpack-plugin')

module.exports = {
  entry: "./src/index.js",
  output: {
    path: path.resolve(__dirname, "dist"),
    filename: "app.bundle.js",
  },
  module: {
    rules: [
      {
        test: /\.css$/,
        use: ["style-loader", "css-loader"],
        exclude: /node_modules/,
      },
    ],
  },
  plugins: [new HtmlWebpackPlugin({
    template: path.resolve(__dirname, "src", "index.html")
  })]
};

dev(개발) & prod(배포) 모드 설정

📗 플러그인 설치

npm i webpack-merge —save-dev

📗 파일 생성(common, dev, prod)

common
webpack.config.js 와 동일

dev

//webpack.config.dev.js
const { merge } = require('webpack-merge');
const common = require('./webpack.common');

module.exports = merge(common, {
  mode: 'development',
  devtool: 'inline-source-map',
  devServer: {
    //contentBase: './dist'
    port: 3001,
  }
});

prod

//webpack.config.prod.js
const { merge } = require('webpack-merge');
const common = require('./webpack.common');

module.exports = merge(common, {
  mode: 'production'
});

📗 모드에 따라 설정 다르게 하기

//package.json
{
  //...
  "scripts": {
    "build": "webpack --config webpack.config.prod.js",
    "dev": "webpack-dev-server --open --config webpack.config.dev.js",
  },
  //...
}

참고

코드스테이츠 교과서

-빌드후 WARNING 해결하기
WARNING in asset size limit: The following asset(s) exceed the recommended size
Performance


-모드 설정
실행 모드에 따라 웹팩 설정 달리하기
Webpack Merge
The 'mode' option has not been set, webpack will fallback to 'production' for this value. 해결하기
프론트엔드 개발환경의 이해: 웹팩(심화)

-개발용 서버
Using webpack-dev-server

-그외
[바닐라JS] 웹팩으로 빌드하고 깃허브 페이지 배포하기
[error] Uncaught ReferenceError: DOMPurify is not defined
CSS 파일 크기 최적화
디렉토리에 웹팩 설치 및 기본 설정

profile
기본에 충실하며 앞으로 발전하는

0개의 댓글