static 폴더 생성
images 폴더 생성
logo.png
facivon.ico
<img src="./images/logo.png" alt="HEROPY" />
상대경로상 html 근처에는 images 가 없다. static 폴더 안에 있기 때문이다. 그렇다면 왜?
index.html 은 번들링의 결과로 dist 파일로 내용들이 복사되어지는데 이 때 저 경로가 된다.
$ npm i -D copy-webpack-plugin
const path = require('path');
const HtmlPlugin = require('html-webpack-plugin');
const CopyPlugin = require('copy-webpack-plugin');
// export
module.exports = {
entry: './js/main.js',
// 결과물(번들)을 반환하는 설정
output: {
clean: true,
},
// 번들링 후 결과물의 처리 방식 등 다양한 플러그인들을 설정
plugins: [
new HtmlPlugin({
template: './index.html',
}),
// static 폴더 안에 있는 내용이 copy 되어서 dist 폴더로 들어간다.
new CopyPlugin({
patterns: [{
from: 'static'
}],
}),
],
};
// copy plugin 을 통해서 복사 한다.
$ npm run build
// 폴더가 이렇게 정리 된다.
dist
images
logo.png
favicon.ico
index.html (html 경로 주변에서 images 폴더를 찾을 수 있다.)
main.js