-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.js
112 lines (88 loc) · 2.97 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
"use strict";
let path = require('path');
let gulp = require('gulp');
let gulpif = require('gulp-if');
let sass = require('gulp-sass');
let jade = require('gulp-jade');
let newer = require('gulp-newer');
let sourcemaps = require('gulp-sourcemaps');
let browserSync = require('browser-sync');
let _ = require('lodash');
let reload = browserSync.reload;
let argv = require('yargs').argv;
let config = require('./config.json');
let defaults = {
minify: false,
sourcemaps: true,
reload: true,
urlRewrite: false,
};
let env = _.defaults(config.environments[config.env], defaults);
let jadeUtils = {
slugify: (str) => {
return str.toLowerCase()
.replace(/[^\w\d]+/g, '-');
},
url: (path) => {
if (!env.urlRewrite)
path += '.html';
return env.locals.baseUrl + path;
}
};
function staticPaths() {
return config.static.map((dir) => config.paths.static + dir + '/**');
}
function copyAssets(dest) {
gulp.src(config.paths.etc + 'favicon.ico')
.pipe(newer(dest))
.pipe(gulp.dest(dest));
return gulp.src(staticPaths(), { base: config.paths.static })
.pipe(newer(dest))
.pipe(gulp.dest(dest));
}
function buildSass(out, minify, reloadBS, useSourcemap) {
let outputStyle = minify ? 'compressed' : 'nested';
return gulp.src(config.paths.styles + 'app.scss')
.pipe(gulpif(useSourcemap, sourcemaps.init()))
.pipe(sass({ outputStyle }).on('error', sass.logError))
.pipe(gulpif(useSourcemap, sourcemaps.write('.')))
.pipe(gulp.dest(out + 'css'))
.pipe(gulpif(reloadBS, reload({ stream: true, match: '**/*.css' })));
}
function buildTemplates(locals, path) {
return gulp.src(config.paths.templates + 'pages/*.jade')
.pipe(jade({
locals: _.merge({}, locals, jadeUtils),
pretty: '\t',
}).on('error', (err) => console.log(err)))
.pipe(gulp.dest(path));
}
gulp.task('templates', () => buildTemplates(env.locals, env.output));
gulp.task('sass', () => buildSass(env.output, env.minify, env.reload, env.sourcemaps));
gulp.task('assets', () => copyAssets(env.output));
gulp.task('templates-watch', ['templates'], reload);
gulp.task('assets-watch', ['assets'], reload);
gulp.task('serve', ['sass', 'templates'], () => {
browserSync({
server: {
baseDir: env.output
}
});
gulp.watch(staticPaths(), ['assets-watch']);
gulp.watch(config.paths.styles + '**/*.scss', ['sass']);
gulp.watch(config.paths.templates + '**/*.jade', ['templates-watch']);
});
gulp.task('build', () => {
if (!_.has(config.environments, argv.env))
throw new Error(`Environment '${argv.env}' not found. Please check your config.json`);
env = _.defaults(config.environments[argv.env], defaults);
copyAssets(env.output);
buildTemplates(env.locals, env.output);
buildSass(env.output, env.minify, env.reload, env.sourcemaps);
// Copy .htaccess file if necessary
if (env.urlRewrite) {
gulp.src(config.paths.etc + '.htaccess')
.pipe(newer(env.output))
.pipe(gulp.dest(env.output));
}
});