-
Notifications
You must be signed in to change notification settings - Fork 1
/
gulpfile.js
215 lines (194 loc) · 5.44 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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
/**
*
* 19 December, 2019
*
* Deepraj Pandey
*
* Gulpfile for Project Wisteria
*/
'use strict';
var browserSync = require('browser-sync').create();
var del = require('del')
var gulp = require('gulp');
var sass = require('gulp-sass');
var sourcemaps = require('gulp-sourcemaps');
var autoprefixer = require('gulp-autoprefixer');
var concat = require('gulp-concat');
var cleanCSS = require('gulp-clean-css');
var uglify = require('gulp-uglify');
var cache = require('gulp-cache');
var imagemin = require('gulp-imagemin');
var inject = require('gulp-inject');
var pump = require('pump');
const reload = browserSync.reload;
var Paths = {
dev: {
here: './dev',
js: './dev/assets/js/core',
img: './dev/assets/img',
fonts: './dev/assets/fonts',
scss: './dev/assets/scss/**/**/*.scss',
scss_toolkit: './dev/assets/scss/blk-design-system.scss',
css: './dev/assets/css'
},
prod: {
here: './prod',
css: './prod/assets/css',
js: './prod/assets/js',
img: './prod/assets/img',
fonts: './prod/assets/fonts'
}
}
// Function to inject relative css and js file paths
// into html files in the directory (json) passed as argument
function injectPaths(allPaths) {
// Where the injections will happen
var target = gulp.src(allPaths.here+'/*.html');
// All css and js sources
// String concat since the path variables do not include the wildcards
var sources = gulp.src([allPaths.css+'/*.css', allPaths.js+'/*.js'], {read: false})
return target.pipe(inject(sources, {relative: true}))
.pipe(gulp.dest(allPaths.here));
}
// Compiles `blk-design-system.scss` and puts the `blk-design-system.css` in dev/assets/css
// Then calls injectPaths with dev directory
gulp.task('compile-toolkit', function genCss(cb) {
pump([
gulp.src(Paths.dev.scss_toolkit, {sourcemaps: true}),
// Generate Source Maps
//.pipe(sourcemaps.init())
sass({
precision: 10,
onError: console.error.bind(console, 'Sass error:')
}),
autoprefixer(),
// injectPaths is called on end to make sure sass compiles before injecting new file
gulp.dest(Paths.dev.css).on('end', function injectNewP() {
// injection into dev folder
return injectPaths(Paths.dev);
})
],
cb
);
});
// Copies the html files from dev to prod
function copyHtml() {
gulp.src(Paths.dev.here+'/*.html')
.pipe(gulp.dest(Paths.prod.here));
}
// Concats, minifies and copies the resultant css file into production
gulp.task('styles', function prepStyles(cb) {
copyHtml();
pump([
gulp.src(Paths.dev.css+'/*.css'),
sourcemaps.init(),
autoprefixer(),
concat('main.min.css'),
cleanCSS({debug: true}, function minify(deets) {
console.log(`${deets.name}: ${deets.stats.originalSize}`);
console.log(`${deets.name}: ${deets.stats.minifiedSize}`);
}),
sourcemaps.write('.'),
gulp.dest(Paths.prod.css).on('end', function injectNewSt() {
return injectPaths(Paths.prod);
})
],
cb
);
});
// Concats, minifies, and copies js files to production
gulp.task('scripts', function prepScripts(cb) {
copyHtml();
pump([
gulp.src(Paths.dev.js+'/*.js'),
sourcemaps.init(),
// Concatenate all js files
concat('main.js'),
// Minify
uglify(),
concat('main.min.js'),
sourcemaps.write('.'),
gulp.dest(Paths.prod.js).on('end', function injectNewSc() {
return injectPaths(Paths.prod)
})
],
cb
);
});
// Optimises images
gulp.task('images', function imgOptim(cb) {
pump([
gulp.src(Paths.dev.img+'/*.{svg,png,jpg,gif,ico}'),
cache(imagemin([
imagemin.gifsicle({interlaced: true}),
imagemin.jpegtran({progressive: true}),
imagemin.optipng({optimizationLevel: 5}),
imagemin.svgo({
plugins: [
{removeViewBox: true},
{cleanupIDs: false}
]
})
])),
gulp.dest(Paths.prod.img)
],
cb
);
});
// Misc - copy license to production
gulp.task('license', function copy() {
return gulp.src('LICENSE')
.pipe(gulp.dest(Paths.prod.here));
});
// Copy the fonts
gulp.task('fonts', function copyFonts() {
return gulp.src(Paths.dev.fonts+'/*')
.pipe(gulp.dest(Paths.prod.fonts));
});
// Clears production directory
gulp.task('clean', function rmRf() {
return del('./prod').then(function log(paths) {
console.log('Cleared production directory.');
});
});
// Build production files
gulp.task('build', gulp.series('clean', 'compile-toolkit', 'fonts', 'styles', 'images', 'scripts', 'license'));
// Function to watch the scss variables
function watch() {
gulp.watch(Paths.dev.scss, gulp.series('compile-toolkit'));
}
// SERVERS //
// development server (watches the .css and .js files in `dev/assets`)
gulp.task('serve:dev', function devServer () {
browserSync.init({
port: 5566,
ui: {
port: 5567
},
server: {
baseDir: "./dev",
index: "index.html"
},
watch: true,
files: [Paths.dev.css, Paths.dev.js, Paths.dev.img]
});
watch();
});
// serve from `prod`
// but first build production files
gulp.task('serve:prod', function devServer () {
browserSync.init({
port: 8080,
ui: {
port: 8081
},
server: {
baseDir: "./prod"
},
watch: true,
files: [Paths.prod.css, Paths.prod.js, Paths.prod.img]
});
watch();
});
// default task is running dev-server
gulp.task('default', gulp.series('compile-toolkit', 'serve:dev'));