forked from qrac/yakuhanjp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgulpfile.js
185 lines (163 loc) · 4.27 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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
//----------------------------------------------------
// gulp: Setting
//----------------------------------------------------
const gulp = require("gulp")
const fs = require("fs")
const notify = require("gulp-notify")
const plumber = require("gulp-plumber")
const rename = require("gulp-rename")
const header = require("gulp-header")
const gulpif = require("gulp-if")
const nunjucks = require("gulp-nunjucks-render")
const data = require("gulp-data")
const htmlBeautify = require("gulp-html-beautify")
const sass = require("gulp-sass")
const cleanCSS = require("gulp-clean-css")
const browserSync = require("browser-sync")
// Read File
const files = {
pkg: "./package.json",
pjt: "./project.json"
}
const pkg = JSON.parse(fs.readFileSync(files.pkg))
const pjt = JSON.parse(fs.readFileSync(files.pjt))
// Banner
const banner = {
basic: [
"/*! <%= pjt.setting.name %> v<%= pkg.version %> <%= pkg.license %> by <%= pkg.author.name %> */",
""
].join("\n"),
visible: pjt.setting.banner
}
// Paths
const paths = {
dist: {
dir: pjt.setting.dist + "/",
css: pjt.setting.dist + "/css/",
font: pjt.setting.dist + "/fonts/"
},
test: {
dir: pjt.setting.test + "/",
html: pjt.setting.test + "/",
css: pjt.setting.test + "/",
font: pjt.setting.test + "/fonts/"
},
src: {
dir: pjt.setting.src + "/",
scss: pjt.setting.src + "/scss/"
}
}
const htmlBeautifyOptions = {
indent_size: 2,
max_preserve_newlines: 0,
indent_inner_html: true,
extra_liners: []
}
// Sass Options
const sassOptions = {
outputStyle: "expanded",
includePaths: "./node_modules/"
}
// BrowserSync Options
const browserSyncOptions = {
server: {
baseDir: paths.test.html
},
startPath: "index.html",
open: false,
notify: false
}
//----------------------------------------------------
// gulp: Task
//----------------------------------------------------
// Nunjucks > HTML (test)
function htmlTest() {
return gulp
.src("index.njk")
.pipe(
data(function() {
return { pkg, pjt }
})
)
.pipe(nunjucks())
.pipe(htmlBeautify(htmlBeautifyOptions))
.pipe(gulp.dest(paths.test.html))
}
// SCSS > CSS (Test)
function scssTest() {
return gulp
.src(paths.src.scss + "**/*.scss")
.pipe(
plumber({ errorHandler: notify.onError("Error: <%= error.message %>") })
)
.pipe(sass(sassOptions))
.pipe(gulpif(banner.visible, header(banner.basic, { pkg, pjt })))
.pipe(gulp.dest(paths.test.css))
}
// Copy Fonts (test)
function copyFontsTest() {
return gulp.src(paths.dist.font + "**/*").pipe(gulp.dest(paths.test.font))
}
// SCSS > CSS (Build)
function scssBuild() {
return gulp
.src(paths.src.scss + "**/*.scss")
.pipe(
plumber({ errorHandler: notify.onError("Error: <%= error.message %>") })
)
.pipe(sass(sassOptions))
.pipe(gulpif(banner.visible, header(banner.basic, { pkg, pjt })))
.pipe(gulp.dest(paths.dist.css))
}
// CSS Minify
function cssMin() {
return gulp
.src([paths.dist.css + "**/*.css", "!" + paths.dist.css + "**/*.min.css"])
.pipe(
plumber({ errorHandler: notify.onError("Error: <%= error.message %>") })
)
.pipe(cleanCSS())
.pipe(rename({ suffix: ".min" }))
.pipe(gulp.dest(paths.dist.css))
}
// BrowserSync Init
function browserSyncInit(done) {
browserSync.init(browserSyncOptions)
done()
}
// BrowserSync Reload
function browserSyncReload(done) {
browserSync.reload()
done()
}
// Watch
function watchFiles() {
gulp.watch("index.njk",
gulp.series(htmlTest, browserSyncReload)
)
gulp.watch(
paths.src.scss + "**/*.scss",
gulp.series(scssTest, browserSyncReload)
)
}
//----------------------------------------------------
// gulp: Default
//----------------------------------------------------
gulp.task(
"default",
gulp.series(
gulp.parallel(htmlTest, scssTest, copyFontsTest),
gulp.parallel(browserSyncInit, watchFiles)
)
)
//----------------------------------------------------
// gulp: test
//----------------------------------------------------
gulp.task(
"test",
gulp.series(gulp.parallel(htmlTest, scssTest, copyFontsTest))
)
//----------------------------------------------------
// gulp: Build
//----------------------------------------------------
gulp.task("build", gulp.parallel(gulp.series(scssBuild, cssMin)))