누코(nuuco)
오늘도 코딩
누코(nuuco)
전체 방문자
오늘
어제
  • 분류 전체보기
    • TIL
    • 에러 노트
    • 자료구조 & 알고리즘
    • 프로그래밍
    • 프로젝트
    • 한컷코딩
    • 글

공지사항

  • 🚚 (전)노션 ➡️ (현)티스토리로 블로그 이사 오는 중(⋯

최근 글

인기 글

최근 댓글

티스토리

250x250
반응형
hELLO · Designed By 정상우.
누코(nuuco)

오늘도 코딩

[Webpack] optimization 의 minimizer 적용 시, js 번들 파일 경량화(minimize) 가 풀려요
에러 노트

[Webpack] optimization 의 minimizer 적용 시, js 번들 파일 경량화(minimize) 가 풀려요

2022. 11. 17. 16:01

2022.7.28 (목) 작성

1. 에러❗️

mode: “production” 으로 번들링 했는데, js 번들 파일(app.bundle.js) 이 한줄로 경량화 되지 않는다.

2. 상황

// webpack.config.base.js
const path = require('path')
const HtmlWebpackPlugin = require('html-webpack-plugin');
const MiniCssExtractPlugin = require("mini-css-extract-plugin");


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

// webpack.config.prod.js
const { merge } = require("webpack-merge");
const baseConfig = require("./webpack.config.base");
const CssMinimizerPlugin = require("css-minimizer-webpack-plugin");

module.exports = merge(baseConfig, {
  mode: "production",
  optimization: {
    minimizer: [new CssMinimizerPlugin()],
  },
});
  1. 위와 같은 웹팩 설정 파일로 배포 모드 번들링을 실행
  2. npm webpack --config webpack.config.prod.js
  3. css 파일은 잘 경량화되었으나, 기존 js 번들 파일(app.bundle.js) 이 경량화가 되지 않음.
  4. optimization 속성을 삭제하니, app.bundle.js 는 경량화되나, css 파일은 다시 경량화 풀림
  5. 두 파일 모두 경량화 시키는 방법이 필요함.

3. 해결💡

  • minimizer 속성을 주면 기본 옵션을 덮어씌워버리기 때문에 js 파일의 경량화 옵션이 사라지고, 플러그인만 적용이 문제였다.
  • 따라서 웹팩 공식 문서에 따라 뒤에 ‘…’를 넣어주면, 기본 옵션은 유지한 채 플러그인이 적용된다! app.bundle.js 도 css 파일도 모두 경량화 되었다!!

// webpack.config.prod.js
const { merge } = require("webpack-merge");
const baseConfig = require("./webpack.config.base");
const CssMinimizerPlugin = require("css-minimizer-webpack-plugin");

module.exports = merge(baseConfig, {
  mode: "production",
  optimization: {
		// '...'가 없으면 기본 옵션을 덮어씌워버리기 때문에 
		// app.bundle.js 는 경량화 되지 않는다.
    minimizer: [new CssMinimizerPlugin(), '...'],
  },
});

 

728x90
반응형

'에러 노트' 카테고리의 다른 글

[React] CRA 에서 proxy 가 동작하지 않아요!  (0) 2022.11.30
[code runner] /bin/sh: node: command not found (node.js 환경에서 코드 실행 결과 보기 오류)  (0) 2022.11.18
[구글 미트(Google Meet)] “화면을 공유할 수 없음” 해결  (0) 2022.11.17
[React] TypeError: Cannot read properties of undefined (reading 'map')  (0) 2022.11.17
[React] Uncaught ReferenceError: require is not defined  (0) 2022.11.17
    '에러 노트' 카테고리의 다른 글
    • [code runner] /bin/sh: node: command not found (node.js 환경에서 코드 실행 결과 보기 오류)
    • [구글 미트(Google Meet)] “화면을 공유할 수 없음” 해결
    • [React] TypeError: Cannot read properties of undefined (reading 'map')
    • [React] Uncaught ReferenceError: require is not defined
    누코(nuuco)
    누코(nuuco)
    👩🏻‍💻 예비 프론트엔드 개발자 😎 글 쓰고, 그림 그리고, 코딩하는 것을 좋아합니다✨

    티스토리툴바