node 프로그램 시작! -> package.json 파일 생성됨
- package.json에는 해당 프로그램의 기본정보
$ npm init -y
-> package.json 생성해줌
webpack을 설치
$ npm i -D webpack webpack-cli webpack-dev-server@next
package.json scripts 설정
"scripts": {
"dev": "webpack-dev-server --mode development",
"build": "webpack --mode production"
}
// import
// 절대경로를 가져오기 위한 전역 모듈인 path 가져옴
const path = require('path')
// export
module.exports = {
// 파일을 읽어들이기 시작하는 진입점 설정
entry: './js/main.js',
// 결과물(번들)을 반환하는 설정
output: {
// 절대 경로를 명시해야함(path 모듈 사용) / __dirname은 node 환경 전역 변수로 현재 파일이 있는 그 경로를 지칭함
// __dirname은 root경로이고 그 위치에 dist 폴더를 생성한다는 뜻
path: path.resolve(__dirname, 'dist'),
filename: 'main.js',
clean: true // 구성옵션을 바꿨을 때 이전 값들을 지우고 새로 만듦
}
}
dist 폴더 안에 Index.html 도 넣어주기 위한 플러그인 설정
$ npm i -D html-webpack-plugin
...
// 설치한 htmlplugin 가져옴
const HtmlPlugin = require('html-webpack-plugin')
...
// 번들링 후 결과물의 처리 방식 등의 다양한 플러그인들을 설정
plugins: [
new Htmlplugin({
template: './index.html'
})
]
개발 서버 오픈할 때 구성을 localhost로 변경
- 포트번호 앞 localhost가 들어가야할 자리가 알수없는 값으로 들어가 있음
plugins: [
new HtmlPlugin({
template: './index.html'
})
],
devServer: {
host: 'localhost'
}
프로젝트에서 사용할 favicon 설정 - static 폴더
dist 폴더 안으로 복사해서 밀어 넣어주기(image 같은 파일들을)
$ npm i -D copy-webpack-plugin
plugins: [
new HtmlPlugin({
template: './index.html'
}),
new CopyPlugin({
patterns: [{ from: 'static' }] // static 폴더 안에 파일이 복사 되어서 dist 폴더 안으로 넣어줌
})
]
CSS, SCSS 처리 - webpack이 css를 읽고 bundle 할 수 있도록!
// output 이후 콤마찍고, module로 객체 형태로 CSS 처리
module: {
rules: [
{
test: /\.s?css$/,
use: [
'style-loader', // 실행순서 중요!(밑에서 부터 실행됨)
'css-loader',
// css loader가 먼저 실행됨 webpack이 main.js에 연결된 css 파일을 해석해서 htmldp style 태그로 삽입해줌
'sass-loader'
]
}
]
},
Autoprefixer(Postcss) 적용 - 공급업체접두사(webkit, ms 같은 이전 버전의 css)
module: {
rules: [
{
test: /\.s?css$/,
use: [
'style-loader'
'css-loader',
'postcss-loader', // sass-loader가 먼저 수행되고!
'sass-loader'
]
}
]
}
// package.json 파일 내부
"browserlist": [
"> 1%",
"last 2 versions"
]
// .postcssrc.js 파일 내부
module.exports = {
plugins: [require('autoprefixer')]
}
Babel 설정! -> 이전 버전의 JS에서도 돌아갈 수 있게 compile 해줌
module: {
rules: [
{
test: /\.s?css$/,
use: [
'style-loader', // 실행순서 중요!(밑에서 부터 실행됨)
'css-loader', // css loader가 먼저 실행됨 main.js에 연결된 css 파일을 해석하는 역할
'postcss-loader',
'sass-loader'
]
},
{ test: /\.js$/, use: ['babel-loader'] }
]
}
// babelrc.js 파일 내부
module.exports = {
presets: ['@babel/preset-env'], // 자바스크립트의 모든 기능 지원
plugins: [['@babel/plugin-transform-runtime']] // for 비동기처리
}
번외) .ignore 파일 생성하여 추가
- .cache .DS_Store(맥일 경우) node_modules dist
$ npx digit {깃헙계졍/깃헙저장소 다운받을폴더} - {} 중괄호는 빼고 입력
- 깃헙 원격저장소를 다운 받을 수 있는 명령어
- $ git clone은 해당 저장소의 버전 관리 내역까지 같이 받아옴.
- $ npx digit은 저장소 안에 파일만 가져오기 때문에 git init으로 버전 관리를 새로 시작할 수 있음.
프로젝트 시작 다른 방법 깃헙 저장소에서 파일을 다운로드 받으면됨 이것을 쉽게 해준 것이 npx digit이다!