Terser

Node.js

# Node.js 버전 확인
➜ node -v
v20.9.0

# v20.9.0 가 설치되어 있지 않다면
➜ nvm install 20.9.0

# v20.9.0 를 사용하고 있지 않다면
➜ nvm use 20.9.0

# Node.js 버전 다시 확인
➜ node -v
v20.9.0

npm

# npm 버전 확인
➜ npm -v
10.2.1

# 최신 버전 npm 으로 업그레이드 
➜ npm i [email protected] -g

# npm 버전 다시 확인
➜ npm -v
10.2.1

pnpm

# pnpm 설치 확인 및 버전 확인
➜ pnpm -v
8.10.0

# 최신 버전 pnpm 으로 업그레이드 
➜ npm i [email protected] -g

# pnpm 설치 다시 확인 및 버전 다시 확인
➜ pnpm -v
8.10.0

Create Project

➜ mkdir terser-example

➜ cd terser-example

➜ corepack enable

➜ pnpm init

➜ corepack use [email protected]

➜ pnpm add terser -D

➜ cat package.json
{
  "name": "terser-example",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \\"Error: no test specified\\" && exit 1"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "devDependencies": {
    "terser": "^5.24.0"
  }
}

CLI 실행

function abc(test) {
  const hello = "world";

  console.log(hello, test);
}
➜ pnpm exec terser input.js
function abc(test){const hello="world";console.log(hello,test)}

➜ pnpm exec terser input.js --mangle
function abc(o){const c="world";console.log(c,o)}

➜ pnpm exec terser input.js --compress
function abc(test){console.log("world",test)}

➜ pnpm exec terser input.js --mangle --compress
function abc(o){console.log("world",o)}

➜ mkdir dist

➜ pnpm exec terser input.js --mangle --compress --output dist/output.js

➜ pnpm exec terser input.js --mangle --compress --output dist/output.js --source-map