-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgulpfile.js
154 lines (149 loc) · 3.39 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
// gulp modules
const gulp = require('gulp');
const clean = require('gulp-clean');
const sass = require('gulp-sass');
sass.compiler = require('node-sass');
const data = require('gulp-data');
const mergeJson = require('gulp-merge-json');
const nunjucksRender = require('gulp-nunjucks-render');
const stripComments = require('gulp-strip-comments');
const minify = require('gulp-minifier');
// other modules
const browserSync = require('browser-sync').create();
const config = require('./_config.json')._config;
const buildDir = config.build.path;
const assetsDir = `${buildDir}/${config.site.paths.assets}`;
const stylesDir = `${buildDir}/${config.site.paths.styles}`;
const scriptsDir = `${buildDir}/${config.site.paths.scripts}`;
const imagesDir = `${buildDir}/${config.site.paths.images}`;
const cleanBuildData = function() {
return gulp
.src(`${buildDir}/data.json`, { allowEmpty: true, read: false })
.pipe(clean());
};
const cleanBuild = function() {
return gulp.src(buildDir, { allowEmpty: true, read: false }).pipe(clean());
};
const buildAssets = gulp.parallel(
function() {
return gulp
.src('./src/assets/images/**/*.+(png|jpg|jpeg|gif|svg)')
.pipe(gulp.dest(imagesDir));
},
function() {
return gulp.src('./src/assets/**/*.ico').pipe(gulp.dest(assetsDir));
}
);
const buildStyles = function() {
return gulp
.src('./src/styles/**/*.scss')
.pipe(sass().on('error', sass.logError))
.pipe(gulp.src('./src/styles/**/*.css'))
.pipe(stripComments.text({ ignore: /url\([\w\s:\/=\-\+;,]*\)/g }))
.pipe(
minify({
minify: true,
minifyCSS: true
})
)
.pipe(gulp.dest(stylesDir));
};
const buildScripts = function() {
return gulp
.src('./src/scripts/**/*.js')
.pipe(stripComments())
.pipe(
minify({
minify: true,
minifyJS: {
sourceMap: false
}
})
)
.pipe(gulp.dest(scriptsDir));
};
const buildData = function() {
return gulp
.src(['./_config.json', './src/**/*.json'])
.pipe(
mergeJson({
fileName: 'data.json'
})
)
.pipe(gulp.dest(buildDir));
};
const buildHtml = function() {
return gulp
.src('./src/pages/**/*.+(html|njk)')
.pipe(
data(function() {
return require(`${buildDir}/data.json`);
})
)
.pipe(
nunjucksRender({
path: ['./src/templates']
})
)
.pipe(stripComments.html())
.pipe(
minify({
minify: true,
minifyHTML: {
collapseWhitespace: true,
conservativeCollapse: true
}
})
)
.pipe(gulp.dest(buildDir));
};
const build = gulp.series(
cleanBuild,
gulp.parallel(
buildAssets,
buildStyles,
buildScripts,
gulp.series(buildData, buildHtml)
),
cleanBuildData
);
let browserReloadTimeout;
module.exports = {
clean: cleanBuild,
build: build,
serve: gulp.series(build, function() {
browserSync.init({
ui: config.serve.ui.allow
? {
port: config.serve.ui.port
}
: false,
server: {
baseDir: buildDir
},
port: config.serve.server.port,
reloadDelay: 2000,
logLevel: 'debug'
});
function browserSyncReload(done) {
browserSync.reload();
done();
}
gulp.watch(
'./src/styles/**/*.+(scss|css)',
gulp.series(buildStyles, browserSyncReload)
);
gulp.watch(
'./src/scripts/**/*.js',
gulp.series(buildScripts, browserSyncReload)
);
gulp.watch(
[
'./_config.json',
'./src/**/*.json',
'./src/(pages|templates)/**/*.+(html|njk)'
],
gulp.series(buildData, buildHtml, cleanBuildData, browserSyncReload)
);
})
};