forked from FrontendMatter/bootstrap-layout
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgulpfile.js
More file actions
347 lines (302 loc) · 8.51 KB
/
Copy pathgulpfile.js
File metadata and controls
347 lines (302 loc) · 8.51 KB
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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
var gulp = require('gulp');
var $ = require('gulp-load-plugins')();
var runSequence = require('run-sequence');
/////////////
// OPTIONS //
/////////////
// enable extra debug information, when possible
var __DEBUG = true;
// enable sourcemaps for Browserify bundles and Sass
var __SOURCEMAPS = true;
// clean dist files before (re)builds
var __CLEAN = true;
// minify .css and .js files
var __MINIFY = true;
// copy vendor assets from node_modules/
var __VENDOR_ASSETS = [
'highlight.js/styles/github-gist.css',
'simplebar/dist/simplebar.css'
];
///////////
// PATHS //
///////////
// SOURCE PATH OPTIONS
var __SRC = './src';
var __SRC_BROWSERIFY = [__SRC + '/js/**/**.browserify.js'];
var __SRC_SASS = [
__SRC + '/sass/**/*.scss',
// Ignore partials (file names prefixed with _)
'!' + __SRC + '/sass/**/_*.scss'
];
var __SRC_HTML = __SRC + '/html/pages/**/*.html';
// WATCH PATHS
var __WATCH_SASS = [
__SRC + '/sass/**/**.scss',
'./node_modules/bootstrap-layout/src/sass/**/**.scss'
];
var __WATCH_BROWSERIFY = __SRC_BROWSERIFY;
var __WATCH_HTML = __SRC + '/html/**/**';
// DIST PATH OPTIONS
var __DIST = './dist';
var __DIST_JS = __DIST + '/assets/js';
var __DIST_CSS = __DIST + '/assets/css';
var __DIST_VENDOR = __DIST + '/assets/vendor';
// CLEAN PATHS
// clean Browserify bundles
var __CLEAN_BROWSERIFY = [
// Bundles
__DIST_JS + '/**/**.browserify.js',
// Minified bundles
__DIST_JS + '/**/**.browserify.min.js',
// .map files
__DIST_JS + '/**/**.browserify.js.map',
__DIST_JS + '/**/**.browserify.min.js.map'
];
var __CLEAN_CSS = __DIST_CSS;
var __CLEAN_HTML = __DIST + '/**/**.html';
var __CLEAN_VENDOR = __DIST_VENDOR;
/////////////////
// CLEAN TASKS //
/////////////////
var del = require('del');
// CLEAN DIST JS BROWSERIFY BUNDLES
gulp.task('clean-browserify', function (cb) {
if (!__CLEAN) {
return cb()
}
del(__CLEAN_BROWSERIFY).then(function () {
cb()
});
});
// CLEAN DIST CSS
gulp.task('clean-css', function (cb) {
if (!__CLEAN) {
return cb()
}
del(__CLEAN_CSS).then(function () {
cb()
});
});
////////////////
// SASS TASKS //
////////////////
// Compile Sass
gulp.task('sass', function () {
return gulp.src(__SRC_SASS)
// (optional) sourcemaps
.pipe($.if(__SOURCEMAPS, $.sourcemaps.init()))
// Compile Sass
.pipe($.sass({
// Resolve Sass file imports from node_modules
importer: require('sass-importer-npm')
})
// Handle errors
.on('error', $.sass.logError))
// Post CSS
.pipe($.postcss([
// autoprefixer
require('autoprefixer')({ browsers: ['last 2 versions'] })
]))
// (optional) Minify CSS
.pipe($.if(__MINIFY, $.rename({ extname: '.min.css' })))
.pipe($.if(__MINIFY, $.cleanCss()))
// (optional) Write .map file
.pipe($.if(__SOURCEMAPS, $.sourcemaps.write('./')))
// Write CSS file
.pipe(gulp.dest(__DIST_CSS))
});
// Clean CSS & Compile Sass
gulp.task('sass:build', function (callback) {
runSequence('clean-css', 'sass', callback)
});
// Watch Sass
gulp.task('sass:watch', function () {
gulp.watch(__WATCH_SASS, ['sass'])
});
////////////////
// BROWSERIFY //
////////////////
// BROWSERIFY DEPENDENCIES
var browserify = require('browserify');
var watchify = require('watchify');
var source = require('vinyl-source-stream');
var buffer = require('vinyl-buffer');
var globby = require('globby');
var path = require('path');
var assign = require('lodash.assign');
/**
* bundleLogger
* Provides logs for browserify bundles
*/
var prettyHrtime = require('pretty-hrtime');
var _startTime;
var bundleLogger = {
start: function (filepath) {
_startTime = process.hrtime();
$.util.log('Bundling', $.util.colors.green(filepath) + '...');
},
end: function (filepath) {
var taskTime = process.hrtime(_startTime);
var prettyTime = prettyHrtime(taskTime);
$.util.log('Bundled', $.util.colors.green(filepath), 'in', $.util.colors.magenta(prettyTime));
}
}
// Compile Browserify bundles
gulp.task('browserify', function (callback) {
globby(__SRC_BROWSERIFY).then(function (bundles) {
var bundleQueue = bundles.length;
bundles = bundles.map(function (bundle) {
return {
src: bundle,
dest: __DIST_JS,
bundleName: path.basename(bundle)
}
});
var browserifyThis = function (bundleConfig) {
var opts = assign({}, watchify.args, {
// Specify the entry point of your app
entries: bundleConfig.src,
// Enable source maps!
debug: __DEBUG,
paths: [
// Resolve files from node_modules
path.resolve(__dirname, 'node_modules')
]
});
var bundler = browserify(opts);
var bundle = function () {
// Log when bundling starts
bundleLogger.start(path.join(bundleConfig.dest, bundleConfig.bundleName));
return bundler
.bundle()
// Report compile errors
.on('error', $.util.log)
// Use vinyl-source-stream to make the
// stream gulp compatible. Specifiy the
// desired output filename here.
.pipe(source(bundleConfig.bundleName))
// buffer file contents
.pipe(buffer())
// (optional) sourcemaps
// loads map from browserify file
.pipe($.if(__SOURCEMAPS, $.sourcemaps.init({ loadMaps: true })))
// Add transformation tasks to the pipeline here.
// (optional) Minify CSS
.pipe($.if(__MINIFY, $.rename({ extname: '.min.js' })))
.pipe($.if(__MINIFY, $.uglify()))
// (optional) Write .map file
.pipe($.if(__SOURCEMAPS, $.sourcemaps.write('./')))
// Write JS file
.pipe(gulp.dest(bundleConfig.dest))
.on('end', reportFinished)
};
if (global.__WATCHING) {
// Wrap with watchify and rebundle on changes
bundler = watchify(bundler);
// Rebundle on update
bundler.on('update', bundle);
}
var reportFinished = function () {
// Log when bundling completes
bundleLogger.end(path.join(bundleConfig.dest, bundleConfig.bundleName));
if (bundleQueue) {
bundleQueue --;
if (bundleQueue === 0) {
// If queue is empty, tell gulp the task is complete.
// https://github.com/gulpjs/gulp/blob/master/docs/API.md#accept-a-callback
callback();
}
}
};
return bundle();
};
// Start bundling source files with Browserify
bundles.forEach(browserifyThis);
});
});
// Clean & Compile bundles
gulp.task('browserify:build', function (callback) {
runSequence('clean-browserify', 'browserify', callback)
});
// Watch Browserify Bundles
gulp.task('browserify:watch', function () {
gulp.watch(__WATCH_BROWSERIFY, ['browserify'])
});
//////////
// HTML //
//////////
// CLEAN DIST HTML
gulp.task('html:clean', function (cb) {
if (!__CLEAN) { return cb() }
del(__CLEAN_HTML).then(function () { cb() });
});
var swigMarked = require('swig-marked');
swigMarked.configure({
highlight: function (code) {
return require('highlight.js').highlightAuto(code).value
}
});
// Compile Swig
gulp.task('html', function () {
return gulp.src(__SRC_HTML)
.pipe($.frontMatter({ property: 'data' }))
.pipe($.swig({
defaults: {
cache: false,
loader: require('fs-resolver')([
path.join(process.cwd(), __SRC, 'html')
])
},
setup: function(swig) {
swigMarked.useTag(swig, 'markdown');
}
}))
.pipe($.changed(__DIST, { hasChanged: $.changed.compareSha1Digest }))
.pipe(gulp.dest(__DIST));
});
// Compile Swig & Prettify HTML
gulp.task('html:prettify', ['html'], function () {
return gulp.src(__DIST + '/**/*.html')
.pipe($.changed(__DIST, { hasChanged: $.changed.compareSha1Digest }))
.pipe($.prettify({
indent: 4,
indent_inner_html: false,
preserve_newlines: true,
max_preserve_newlines: 1,
brace_style: "condense",
unformatted: [ "a", "span", "i", "pre", "code" ]
}))
.pipe(gulp.dest(__DIST));
});
// Clean HTML, Compile Swig & Prettify HTML
gulp.task('html:build', function (callback) {
runSequence('html:clean', 'html:prettify', callback)
});
// Watch HTML
gulp.task('html:watch', ['watch:set'], function () {
gulp.watch(__WATCH_HTML, ['html:prettify'])
});
////////////////////////
// COPY VENDOR ASSETS //
////////////////////////
gulp.task('vendor:clean', function (cb) {
if (!__CLEAN) { return cb() }
del(__CLEAN_VENDOR).then(function () { cb() });
});
gulp.task('copy:vendor', ['vendor:clean'], function () {
return gulp.src(__VENDOR_ASSETS, { cwd: 'node_modules/' })
.pipe(gulp.dest(__DIST_VENDOR));
});
/////////////
// GENERAL //
/////////////
// Called before running any watcher
// Sets a global __WATCHING flag
gulp.task('watch:set', function (cb) {
global.__WATCHING = true
cb()
});
// Watchers
gulp.task('watch', ['sass:watch', 'browserify:watch', 'html:watch']);
// Default
gulp.task('default', ['copy:vendor', 'sass:build', 'browserify:build', 'html:build']);