-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgulpfile.js
170 lines (153 loc) · 3.85 KB
/
gulpfile.js
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
const gulp = require("gulp");
const pug = require("gulp-pug");
const sass = require("gulp-sass");
const notify = require("gulp-notify");
const sourcemaps = require("gulp-sourcemaps");
const concat = require("gulp-concat");
const rename = require("gulp-rename");
const plumber = require("gulp-plumber");
const del = require("del");
const browserSync = require("browser-sync");
// js babel
const babel = require("gulp-babel");
const uglify = require("gulp-uglify-es").default;
// image compressor
const imagemin = require("gulp-imagemin");
const imageminMozjpeg = require("imagemin-mozjpeg");
const imageminPngquant = require("imagemin-pngquant");
const imageminSvgo = require("imagemin-svgo");
// webpack
const webpackStream = require("webpack-stream");
const webpack = require("webpack");
const webpackConfig = require("./webpack.config");
const paths = {
root: "./dist/",
pug: "./src/pug/**/*.pug",
scssSrc: "./src/scss/**/*.scss",
cssDist: "./dist/css/",
jsSrc: "./src/js/**/*.js",
jsDist: "./dist/js/",
imageSrc: "./src/images/**/*",
imageDist: "./dist/images/",
};
const PATHS = {
pug: {
src: "./src/pug/**/!(_)*.pug",
dest: "./dist",
},
scss: {
src: "./src/scss/**/*.scss",
dest: "./dist/css",
},
js: {
src: "./src/js/**/*.js",
dest: "./dist/js",
},
image: {
src: "./src/images/**/*",
dest: "./dist/images",
},
};
const { watch, series, src, dest, parallel } = require("gulp");
const errorHandler = (err, stats) => {
if (err || (stats && stats.compilation.error.length > 0)) {
const error = err || stats.compilation.errors[0].error;
notify.onError({ message: "<%= error.message %>" })(error);
this.emit("end");
}
};
const pug2Html = () => {
return src(PATHS.pug.src)
.pipe(plumber({ errorHandler: errorHandler }))
.pipe(
pug({
pretty: true,
basedir: "./pug",
})
)
.pipe(dest(PATHS.pug.dest));
};
const scss2Css = () => {
return src(PATHS.scss.src)
.pipe(plumber({ errorHandler: errorHandler }))
.pipe(
sass({
outputStyle: "expanded", // Minifyするなら'compressed'
})
)
.pipe(dest(PATHS.scss.dest));
};
const jsBabel = () => {
return src(PATHS.js.src)
.pipe(plumber({ errorHandler: errorHandler }))
.pipe(sourcemaps.init())
.pipe(
babel({
presets: ["@babel/preset-env"],
})
)
.pipe(webpackStream(webpackConfig, webpack))
.pipe(concat("main.js"))
.pipe(uglify())
.pipe(
rename({
extname: ".min.js",
})
)
.pipe(sourcemaps.write("../maps"))
.pipe(dest(PATHS.js.dest));
};
const image = () => {
return src(PATHS.image.src)
.pipe(plumber({ errorHandler: errorHandler }))
.pipe(
imagemin(
[
imageminMozjpeg({
quality: 80,
}),
imageminPngquant(),
imageminSvgo({
plugins: [
{
removeViewbox: false,
},
],
}),
],
{
verbose: true,
}
)
)
.pipe(gulp.dest(PATHS.image.dest));
};
const browserSyncFunc = () => {
return browserSync.init({
server: {
baseDir: "./dist",
},
port: 3000,
reloadOnRestart: true,
});
};
const browserReload = (done) => {
browserSync.reload();
done();
console.info("Browser reload completed");
};
const clean = (done) => {
del.sync("./dist/**/*", "!./dist");
done();
};
const watchFiles = () => {
watch(paths.pug, series(pug2Html, browserReload));
watch(paths.scssSrc, series(scss2Css, browserReload));
watch(paths.jsSrc, series(jsBabel, browserReload));
watch(paths.imageSrc, series(image, browserReload));
};
exports.build = series(parallel(clean, pug2Html, scss2Css, jsBabel, image));
exports.default = series(
series(clean, pug2Html, scss2Css, jsBabel, image),
parallel(watchFiles, browserSyncFunc)
);