forked from Tenkue/tenkue_hotel_lp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.js
151 lines (139 loc) · 4.54 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
const gulp = require('gulp');
const sass = require('gulp-sass');
const autoprefixer = require('gulp-autoprefixer');
const gcmq = require('gulp-group-css-media-queries');
const cleanCSS = require("gulp-clean-css");
const ejs = require('gulp-ejs');
const rimraf = require('rimraf');
const replace = require('gulp-replace');
const rename = require('gulp-rename');
const prettify = require('gulp-prettify');
const sourcemaps = require('gulp-sourcemaps');
const browserSync = require('browser-sync').create();
const imagemin = require('gulp-imagemin');
const mozjpeg = require('imagemin-mozjpeg');
const pngquant = require('imagemin-pngquant');
const plumber = require('gulp-plumber');
const uglify = require('gulp-uglify');
const notify = require('gulp-notify');
const changed = require('gulp-changed');
const mode = require("gulp-mode")({
modes: ["production", "development"],
default: "development",
verbose: false,
});
var paths = {
srcDir: './src',
dstDir: './dist',
};
// ブラウザの立ち上げ
function sync(done) {
browserSync.init({
server: {
baseDir: paths.dstDir,
index: 'index.html',
},
open: 'external',
reloadOnRestart: true,
});
done();
}
// ブラウザをリロード
function browserReload(done) {
browserSync.reload();
done();
}
// 立ち上げた際にdistを一旦クリーンにする
function clean(cb) {
return rimraf(paths.dstDir, cb);
}
// EJSをHTMLにコンパイルしてdistに吐き出し
function ejsCompile() {
return gulp
.src([paths.srcDir + '/ejs/**/*.ejs', '!' + paths.srcDir + '/ejs/**/_*.ejs'])
.pipe(plumber())
.pipe(ejs({}, { rmWhitespace: true }, {}))
.pipe(
prettify({
indent_size: 4,
indent_with_tabs: true,
})
)
.pipe(replace(/[\s\S]*?(<!DOCTYPE)/, '$1'))
.pipe(rename({ extname: '.html' }))
.pipe(gulp.dest(paths.dstDir));
}
// SassをCSSにコンパイルしてdistに吐き出し
function sassCompile() {
return (
gulp
.src(paths.srcDir + '/asset/sass/**/*.{scss,sass}')
.pipe(plumber(notify.onError('Error: <%= error.message %>')))
.pipe(sourcemaps.init())
.pipe(
sass({
outputStyle: 'expanded',
})
)
.on('error', sass.logError)
.pipe(
autoprefixer({
cascade: false,
})
)
.pipe(sourcemaps.write())
.pipe(gcmq())
.pipe(mode.production(cleanCSS()))
.pipe(gulp.dest(paths.dstDir + '/asset/css'))
);
}
// imageをMinifyしてdistに吐き出し
function imageMin() {
return gulp
.src(paths.srcDir + '/asset/image/*.{jpg,jpeg,png,gif,svg}')
.pipe(changed(paths.dstDir + '/asset/image'))
.pipe(
imagemin([
pngquant({
quality: [0.7, 0.85],
speed: 1,
}),
mozjpeg({
quality: 85,
progressive: true,
}),
imagemin.svgo(),
imagemin.optipng(),
imagemin.gifsicle(),
])
)
.pipe(gulp.dest(paths.dstDir + '/asset/image'));
}
// JSをdistに吐き出し
function js() {
return gulp
.src(paths.srcDir + '/asset/js/**/*.js')
.pipe(changed(paths.dstDir + '/asset/js'))
.pipe(mode.production(uglify()))
.pipe(gulp.dest(paths.dstDir + '/asset/js'));
}
// srcのファイルに変更があれば自動でリロード
function watchFile(done) {
gulp.watch(paths.srcDir + '/ejs/**/*.ejs', ejsCompile).on('change', gulp.series(browserReload));
gulp.watch(paths.srcDir + '/asset/sass/**/*.{scss,sass}', sassCompile).on('change', gulp.series(browserReload));
gulp.watch(paths.srcDir + '/asset/js/**/*.js', js).on('change', gulp.series(browserReload));
gulp.watch(paths.srcDir + '/asset/image/*.{jpg,jpeg,png,gif,svg}', imageMin).on('change', gulp.series(browserReload));
gulp.series(browserReload);
done();
}
// タスクの実行
exports.default = gulp.series(
clean,
gulp.parallel(ejsCompile, sassCompile, imageMin, js),
sync,
watchFile
);
exports.build = gulp.series(
clean,
gulp.parallel(ejsCompile, sassCompile, imageMin, js),
);