728x90
브라우저에서는 캐싱을 파일명으로 한다.
build하면 bundle 이름이
budle.js 혹은 style.css 이런 식으로 되는데
빌드 할 때마다 소스가 바뀐다면
고유한 값을 파일명으로 부여하여
캐싱을 피할 수 있다.
해당 내용을 실습해보자
1. webpack.config.js 수정
// can't use ecma script module. use only common.js
const path = require('path');
const TerserPlugin = require('terser-webpack-plugin');
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
// minimum option
module.exports = {
entry: './src/index.js',
output: {
// ↓↓↓↓↓↓ 여기수정 [contenthash] 추가
filename: 'bundle.[contenthash].js',
path: path.resolve(__dirname, './dist'),
/**
* publicPath
* under version 4: publicPath is empty('').
* over version 4: publicPath is auto
*
* can use list
* 1. 'dist/'
* 2. 'https://image.oliveyoung.co.kr/uploads/contents/202410/25oyDay/pc_visual.jpg'
*/
publicPath: 'dist/'
},
mode: 'none',
module: {
/**
* webpack: I don't know what that menas 'import Kiwi...'
* me: thank you for asking, If you don't know somethig, you should check out the rules.
* webpack: perfect
*/
rules: [
{
test: /\.(png|jpg)$/,
/**
* asset/inline :bake image into base64. could be larger file size(more better)
* asset/resource: request image each of them. cost of call is larger
*/
type: 'asset',
parser: {
dataUrlCondition: {
maxSize: 3 * 1024 // 3kilobyte
}
}
},
{
test: /\.txt$/,
type: 'asset/source'
},
{
// loader should be installed explicitly
test: /\.css$/,
use: [
MiniCssExtractPlugin.loader, 'css-loader'
]
},
{
test: /\.scss$/,
// webpack invoke right to left in array
//
use: [
MiniCssExtractPlugin.loader, 'css-loader', 'sass-loader'
]
},
{
test: /\.js$/,
exclude: /node_modules/,
use: {
loader: 'babel-loader',
options: {
presets: ['@babel/env'],// ecmascript 8,7,6,5 make down version
plugins: ['@babel/plugin-proposal-class-properties']
}
}
}
],
},
plugins: [
new TerserPlugin(),
new MiniCssExtractPlugin({
// ↓↓↓↓↓↓ 여기수정 [contenthash] 추가
filename: 'style.[contenthash].css',
})
]
}
소스에서 보면
[contenthash]만 추가한 것을 볼 수 있는데
이 기능은 webpack에서 미리 제공하는 것으로 보인다.
따라서 따로 package를 install 할 필요 없다.
해당 hash값은
MD4 해시를 사용하여 고유값을 부여함.
빌드 후 모습
'서버 > Webpack' 카테고리의 다른 글
Webpack5 - webpack 기본 설정으로 빌드시 파일 삭제 하거나 유지시키기 (1) | 2024.11.28 |
---|---|
Webpack5 - 빌드 전 bundle, 원하는 디렉토리 삭제하고 빌드하기 (0) | 2024.11.28 |
Webpack5 - build시 css 파일 분리하기 (0) | 2024.11.28 |
Webpack - bundling 사이즈 줄이기 간단하게 실습 (1) | 2024.11.05 |
Webpack - ecmascript 최신 내용 적용 안될 때 babel을 추가하여 빌드하기 (0) | 2024.11.05 |