π Deploy Site
Learn Gulp by building an awesome development environment
yarn
: Install dependenciesyarn dev
: Start live server for developmentyarn build
: Build this projectyarn deploy
: Deploy this project
Gulpλ₯Ό νμ΅νκΈ° μν΄ μΌλ°μ μΈ κ°λ° νκ²½μ ꡬμ±νμ΅λλ€. Pug, SCSS κ·Έλ¦¬κ³ μ΅μ JS λ¬Έλ²μ μ¬μ©νλ νκ²½μμ λΈλΌμ°μ μμ ꡬλλ μ μλλ‘ gulpλ‘ λΉλνλ κ² λͺ©μ μ λλ€.
.
βββ .gitignore
βββ README.md
βββ package.json
βββ yarn.lock
βββ src
βββ index.pug
βββ img
β βββ logo.png
βββ js
β βββ main.js
β βββ util.js
βββ partials
β βββ footer.pug
β βββ header.pug
βββ scss
β βββ _reset.scss
β βββ _variables.scss
β βββ style.scss
βββ templates
βββ layout.pug
gulp
λͺ¨λμ μ€μΉνκ³ gulp
λͺ
λ Ήμ΄λ₯Ό μ€ννκΈ° μν΄μ package.json
μ scripts
λ₯Ό μΆκ°ν΄μ£Όμμ΅λλ€.
{
"scripts": {
"dev": "gulp dev",
"build": "gulp build"
},
}
gulp
λͺ
λ Ήμ΄λ₯Ό μ€ννκΈ° μν΄μ μΆκ°λ‘ gulpfile.js
νμΌμ΄ νμν©λλ€. gulpfile.js
μμ import/export
λ¬Έλ²μ μ¬μ©νκΈ° μν΄ babelμ μ€μΉν©λλ€.
$ yarn add -D @babel/core @babel/register @babel/preset-env
κ·Έλ¦¬κ³ gulpfile.babel.js
λ‘ μ΄λ¦μ λ³κ²½ν©λλ€. μΆκ°λ‘ .babelrc
μ μμ±ν©λλ€. μ΄λ‘μ¨, gulpfile
μμ μ΅μ JS λ¬Έλ²μ μμ±ν μ μκ² λμμ΅λλ€.
scripts
μμ μμ±ν λͺ
λ Ήμ΄λ₯Ό μ€ννλ©΄ src
ν΄λ λ΄μ νμν νμΌμ μ»΄νμΌν΄μΌ ν©λλ€. dev
λͺ
λ Ήμ΄λ₯Ό μ€ννκ³ μΆλ€λ©΄ gulp.babel.js
μμ dev
λ₯Ό export
ν΄μΌν©λλ€.
import gulp from 'gulp';
export const dev = console.log('scripts:dev');
κ·ΈλΌ λ¨Όμ pug νμΌμ μ»΄νμΌνκΈ° μν΄ gulp-pug
λ₯Ό μ€μΉνκ³ μλ λ΄μ©μ μμ±ν©λλ€. μ΅μμ pug νμΌ νλλ§ λΉλν©λλ€.
import pug from 'gulp-pug';
const html = () => gulp.src('src/index.pug').pipe(pug()).pipe(gulp.dest('dist'));
export const dev = gulp.series(html);
src()
λ νμΌ μμ€ν
μΌλ‘λΆν° globs
λ‘ μ νν νμΌμ μ½μ΅λλ€. μ νν νμΌμ pipe()
λ©μλλ₯Ό μ°κ²°νμ¬ λ³νν©λλ€. pug
νμΌμ μ»΄νμΌνκΈ° μν΄ gulp-pug
λ₯Ό μ°κ²°ν©λλ€. μ»΄νμΌν κ²°κ³Όλ₯Ό dest()
λ‘ μνλ ν΄λμ μ μ₯ν©λλ€.
λ€μ λΉλκ° νμν λ dist
ν΄λλ₯Ό μμ νλ €λ©΄ del
λͺ¨λμ μ€μΉνκ³ μλ λ΄μ©μ μΆκ°ν©λλ€.
import del from 'del';
const clean = () => del('dist');
export const dev = gulp.series(
gulp.parallel(clean),
gulp.parallel(html),
);
μν μ λ°λΌ λ¨κ³λ₯Ό λλκΈ° μν΄ parallel()
λ©μλλ‘ μμλ₯Ό ꡬλΆνμ΅λλ€.
λΉλ κ²°κ³Όλ¬Όμ μ€ννκΈ° μν΄ gulp-webserver
λ₯Ό μ€μΉνκ³ gulpfile
μ μΆκ°ν©λλ€.
import webserver from 'gulp-webserver';
const ws = () => gulp.src('dist').pipe(webserver({ livereload: true }));
export const dev = gulp.series(
gulp.parallel(clean),
gulp.parallel(html),
gulp.parallel(ws),
);
dev
λͺ
λ Ήμ΄λ₯Ό ν΅ν΄ μλ²λ₯Ό μ€νν μ μμ΅λλ€.
νμΌ λ΄μ©μ λ³κ²½νμ λ μμμ λΉλνκ³ μλ²λ₯Ό μ¬μμν μ μλλ‘ watch()
λ©μλλ₯Ό μ΄μ©ν©λλ€.
const watch = () => gulp.watch('src/**/*.pug', html);
export const dev = gulp.series(
gulp.parallel(clean),
gulp.parallel(html),
gulp.parallel(ws, watch),
);
src
ν΄λ λ΄ λͺ¨λ pug
νμΌμ λν΄ λ³κ²½μ¬νμ΄ λ°μνλ€λ©΄ pug
λ₯Ό λ€μ μ€ννμ¬ λΉλν©λλ€.
gulp-image
λͺ¨λλ‘ μ΅μ νν μ΄λ―Έμ§λ₯Ό λΉλμ μ λ¬ν μ μμ΅λλ€.
import image from 'gulp-image';
const img = () => gulp.src('src/img/*').pipe(image()).pipe(gulp.dest('dist/img'));
export const dev = gulp.series(
gulp.parallel(clean),
gulp.parallel(html, img),
gulp.parallel(ws, watch),
);
SCSSλ₯Ό λΉλνκΈ° μν΄ gulp-sass
(μΆκ°λ‘ sass
λ)λ₯Ό μ€μΉνκ³ μλ λ΄μ©μ μΆκ°ν©λλ€.
import dartSass from 'sass';
import gulpSass from 'gulp-sass';
const sass = gulpSass(dartSass);
const css = () =>
gulp
.src('src/scss/style.scss')
.pipe(sass().on('error', sass.logError))
.pipe(gulp.dest('dist/css'));
const watch = () => {
gulp.watch('src/**/*.scss', css);
};
export const dev = gulp.series(
gulp.parallel(clean),
gulp.parallel(html, css, img),
gulp.parallel(ws, watch),
);
SCSS νμΌμ λ³κ²½μ¬νλ μΆμ νκΈ° μν΄ watch
μ μΆκ°ν΄λ‘λλ€.
CSSμ λν΄ λΈλΌμ°μ νΈνμ±μ λμ΄κΈ° μν΄ gulp-autoprefixer
λ₯Ό μΆκ°ν©λλ€. λμμ CSS νμΌ μμΆμ μν΄ gulp-csso
λ₯Ό μ€μΉνκ³ μλ λ΄μ©μ μΆκ°ν©λλ€.
import autoprefixer from 'gulp-autoprefixer';
import csso from 'gulp-csso';
const css = () =>
gulp
.src('src/scss/style.scss')
.pipe(sass().on('error', sass.logError))
.pipe(autoprefixer())
.pipe(csso())
.pipe(gulp.dest('dist/css'));
autoprefixerλ browserslist
λ₯Ό μ§μν©λλ€. μ΄ μ€ νλλ₯Ό μ ννκ³ .browserslistrc
νμΌμ μμ±νμ¬ μ§μ λ²μλ₯Ό μ νν μ μμ΅λλ€.
import/export
λ¬Έλ²κ³Ό λλΆμ΄ μ΅μ JS λ¬Έλ²μ λν΄ λΈλΌμ°μ νΈνμ±μ λμ΄κΈ° μν΄μ gulp-bro
, babelify
, uglifyify
λ₯Ό μ€μΉνκ³ λ€μ λ΄μ©μ μΆκ°ν©λλ€.
import browserify from 'gulp-bro';
import babelify from 'babelify';
const js = () =>
gulp
.src('src/js/main.js')
.pipe(
browserify({
debug: process.env.NODE_ENV === 'development',
transform: [
babelify.configure({ presets: ['@babel/preset-env'] }),
['uglifyify', { global: true }],
],
}),
)
.pipe(gulp.dest('dist/js'));
const watch = () => {
gulp.watch('src/**/*.js', js);
};
export const dev = gulp.series(
gulp.parallel(clean),
gulp.parallel(html, css, js, img),
gulp.parallel(ws, watch),
);
browserify
λ λΈλΌμ°μ κ°require
λ¬Έλ²μ μ΄ν΄ν μ μλλ‘ ν©λλ€.babelify
λbrowserify
μ© transformμΌλ‘ μ΅μ JS λ¬Έλ²μ΄ λλΆλΆμ λΈλΌμ°μ μ μ€ν κ°λ₯νλλ‘ νΈλμ€νμΌλ§ν©λλ€.uglifyify
λ μ½λλ₯Ό μμΆνμ¬ μ©λμ μ€μ λλ€.
κ°λ° λͺ¨λμΌ κ²½μ°, debug
μ΅μ
μ μΆκ°νμ¬ SourceMap νμΌμ΄ μμ±λλλ‘ νμ¬ κ°λ° νκ²½μμ λλ²κ·Έλ₯Ό μ©μ΄νκ² ν©λλ€.
gulp-gh-pages
λ‘ νλ‘μ νΈλ₯Ό λ°°ν¬ν©λλ€.
import pages from 'gulp-gh-pages';
const clean = () => del(['dist', '.publish']);
const publish = () => gulp.src('dist/**/*').pipe(pages());
const prepare = gulp.parallel(clean);
const assets = gulp.parallel(html, css, js, img);
const live = gulp.parallel(ws, watch);
export const build = gulp.series(prepare, assets);
export const dev = gulp.series(build, live);
export const deploy = gulp.series(build, publish, clean);
package.json
μ scripts:deploy
λ₯Ό μΆκ°ν©λλ€. gh-pages
λ‘ λ°°ν¬νκ³ μμ±λ dist
ν΄λμ .publish
ν΄λλ₯Ό μ 리ν©λλ€.