-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgulpfile.js
More file actions
197 lines (182 loc) Β· 5.28 KB
/
gulpfile.js
File metadata and controls
197 lines (182 loc) Β· 5.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
import gulp from 'gulp';
import ejs from 'gulp-ejs';
import prettier from 'gulp-prettier';
import using from 'gulp-using';
import newer from 'gulp-newer';
import { deleteSync } from 'del';
import gulpSass from 'gulp-sass';
import * as sass from 'sass';
import sourcemaps from 'gulp-sourcemaps';
import imagemin from 'gulp-imagemin';
import mozjpeg from 'imagemin-mozjpeg';
import optipng from 'imagemin-optipng';
import giflossy from 'imagemin-giflossy';
import { rollup } from 'rollup';
import resolve from '@rollup/plugin-node-resolve';
import commonjs from '@rollup/plugin-commonjs';
import babel from '@rollup/plugin-babel';
import terser from '@rollup/plugin-terser';
import { glob } from 'glob';
import browserSync from 'browser-sync';
import fs from 'fs';
const bs = browserSync.create();
// κ²½λ‘ μ€μ
const pathSRC = './src/';
const pathSRCResource = `${pathSRC}resource/`;
const pathDIST = './dist/';
const pathDISTResource = `${pathDIST}resource/`;
const paths = {
src: {
html: [`${pathSRC}page/**`, `!${pathSRC}page/include/**`],
include: `${pathSRC}page/include`,
js: `${pathSRCResource}js/**/*.js`,
css: `${pathSRCResource}sass/**/*.scss`,
image: `${pathSRCResource}image/**/*`,
font: `${pathSRCResource}font/**/*`,
plugin: `${pathSRCResource}plugin/**/*`
},
dist: {
root: pathDIST,
html: `${pathDIST}page`,
js: `${pathDISTResource}js`,
css: `${pathDISTResource}css`,
sass: `${pathDISTResource}sass`,
image: `${pathDISTResource}image`,
font: `${pathDISTResource}font`,
plugin: `${pathDISTResource}plugin`
}
};
// 'dist' λλ ν 리λ₯Ό μμ νλ ν¨μ
export async function clean() {
deleteSync([paths.dist.html, `${pathDIST}resource`]);
}
clean.displayName = `π CleanAll`;
// 'dist/page' λλ ν 리λ₯Ό μμ νλ ν¨μ
export async function cleanHtml() {
deleteSync(paths.dist.html);
}
cleanHtml.displayName = `π CleanHTML`;
// EJS ν
νλ¦Ώμ μ»΄νμΌνλ ν¨μ
export function buildHtml() {
const gnbData = JSON.parse(fs.readFileSync(`${pathSRCResource}js/menu-gnb.json`, 'utf8'));
return gulp.src(paths.src.html)
.pipe(newer(paths.dist.html))
.pipe(using({ prefix: 'π Processing', path: 'relative', color: 'yellow' }))
.pipe(ejs({ menu: gnbData }, { views: paths.src.include }).on('error', console.error))
.pipe(prettier({
printWidth: 200,
tabWidth: 4,
useTabs: true,
bracketSameLine: true,
}))
.pipe(gulp.dest(paths.dist.html))
.pipe(bs.stream());
}
// JavaScript νμΌμ λ²λ€λ§νλ ν¨μ
export function buildJS() {
return rollup({
input: glob.sync(paths.src.js),
plugins: [
resolve(),
commonjs(),
babel({
babelrc: false,
exclude: 'node_modules/**',
presets: [
[
'@babel/preset-env',
{
useBuiltIns: 'usage',
corejs: {
version: 3,
proposals: false,
},
},
],
],
babelHelpers: 'bundled',
extensions: ['.js'],
}),
terser(),
],
})
.then(bundle => {
return bundle.write({
file: `${paths.dist.js}/main.js`,
format: 'iife',
name: 'UI',
sourcemap: true,
});
})
.then(() => {
bs.reload();
});
}
// Sass νμΌμ μ»΄νμΌνλ ν¨μ
export function compileSass() {
return gulp.src(paths.src.css)
.pipe(sourcemaps.init())
.pipe(gulpSass(sass)({
outputStyle: 'compressed' /* expanded:νμ₯, compressed:μμΆ */
}).on('error', gulpSass(sass).logError))
.pipe(sourcemaps.write('../scss'))
.pipe(gulp.dest(paths.dist.css))
.pipe(bs.stream());
}
// μ΄λ―Έμ§ νμΌμ μ΅μ ννκ³ λ³΅μ¬νλ ν¨μ
export function optimizeImage() {
deleteSync(paths.dist.image);
return gulp.src(paths.src.image, { buffer: true, encoding: false })
.pipe(newer(paths.dist.image))
.pipe(imagemin([
mozjpeg({ quality: 75, progressive: true }),
optipng({ optimizationLevel: 5 }),
giflossy({ optimizationLevel: 3 })
]))
.pipe(gulp.dest(paths.dist.image))
.pipe(bs.stream());
}
// ν°νΈ νμΌμ μ΄κΈ°ννκ³ λ³΅μ¬νλ ν¨μ
export function copyFont() {
deleteSync(paths.dist.font);
return gulp.src(paths.src.font, { buffer: true, encoding: false })
.pipe(newer(paths.dist.font))
.pipe(gulp.dest(paths.dist.font))
.pipe(bs.stream());
}
// νλ¬κ·ΈμΈ νμΌμ μ΄κΈ°ννκ³ λ³΅μ¬νλ ν¨μ
export function copyPlugin() {
deleteSync(paths.dist.plugin);
return gulp.src(paths.src.plugin, { buffer: true, encoding: false })
.pipe(newer(paths.dist.plugin))
.pipe(gulp.dest(paths.dist.plugin))
.pipe(bs.stream());
}
// λΈλΌμ°μ λκΈ°νλ₯Ό μν μλ² μμ ν¨μ
export function serve(done) {
bs.init({
startPath: 'page/main/index.html',
server: {
baseDir: paths.dist.root
},
notify: false
});
done();
}
// νμΌ λ³κ²½μ κ°μνκ³ κ΄λ ¨ μμ
μ μννλ ν¨μ
export function watchFiles() {
gulp.watch(paths.src.html, buildHtml);
gulp.watch(paths.src.include, gulp.series(cleanHtml, buildHtml));
gulp.watch(paths.src.css, compileSass);
gulp.watch(paths.src.js, buildJS);
gulp.watch(paths.src.image, optimizeImage);
gulp.watch(paths.src.font, copyFont);
gulp.watch(paths.src.plugin, copyPlugin);
}
// κΈ°λ³Έ μμ
μΌλ‘ 'clean' μμ
ν 'buildJS', 'compileSass', 'buildHtml', 'optimizeImage' μμ
μ μ€ν
export default gulp.series(
clean,
gulp.parallel(buildJS, compileSass, buildHtml, optimizeImage, copyFont, copyPlugin),
serve,
watchFiles
);