-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgulpfile.js
142 lines (126 loc) · 3.2 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
const gulp = require('gulp');
const autoprefixer = require('gulp-autoprefixer');
const browserify = require('browserify');
const browserSync = require('browser-sync').create();
const buff = require('vinyl-buffer');
const clean = require('gulp-clean');
const data = require('gulp-data');
const fs = require('fs');
const merge = require('gulp-merge-json');
const nunjucks = require('gulp-nunjucks-render');
const sass = require('gulp-sass');
const source = require('vinyl-source-stream');
const sourcemaps = require('gulp-sourcemaps');
const watchify = require('watchify');
/**
* ES6 module support, Babel compilation.
* @param {Boolean} watch call the function with or without the watch flag.
*/
function compile(watch) {
var bundler = watchify(browserify('./source/scripts/app.js', {
debug: true
})
.transform("babelify", {
presets: ["es2015"]
}));
/**
* Bundling operation.
*/
function rebundle() {
bundler.bundle()
.on('error', function(err) {
console.error(err);
this.emit('end');
})
.pipe(source('app.js'))
.pipe(buff())
.pipe(sourcemaps.init({loadMaps: true}))
.pipe(sourcemaps.write())
.pipe(gulp.dest('./_build/scripts/'));
}
if (watch) {
bundler.on('update', function() {
console.log('-> bundling...');
rebundle();
});
}
rebundle();
}
/**
* Verbose method to call compile.
* @return {function} Function call to compile.
*/
function watch() {
return compile(true);
}
/**
* Merges all data into a single json file for template use.
*/
gulp.task('data', function() {
return gulp.src('source/data/**/*.json')
.pipe(merge('.generated_data'))
.pipe(gulp.dest('source/data/'));
});
gulp.task('build', function() {
return compile();
});
gulp.task('js-watch', function() {
return watch();
});
// Static server
gulp.task('serve', function() {
browserSync.init({
server: {
baseDir: "./_build"
}
});
});
// Sass to CSS compilation
gulp.task('sass', function() {
return gulp.src("source/styles/**/*.scss")
.pipe(sourcemaps.init())
.pipe(sass().on('error', sass.logError))
.pipe(autoprefixer())
.pipe(sourcemaps.write())
.pipe(gulp.dest("_build/styles"))
.pipe(browserSync.stream());
});
// Templates compilation
gulp.task('templates', ['data'], function() {
return gulp.src('source/pages/*.tpl')
.pipe(data((f) => {
const file = fs.readFileSync('source/data/.generated_data');
return JSON.parse(file);
}))
.pipe(nunjucks({
path: ['source/templates']
}))
.pipe(gulp.dest('_build'));
});
gulp.task('copy', ['clean'], function() {
return gulp.src(['source/assets/**/*'], {
base: 'source'
}).pipe(gulp.dest('_build'));
});
gulp.task('clean', function() {
return gulp.src('_build/assets', {read: false})
.pipe(clean());
});
gulp.task('watch', function() {
gulp.watch('source/styles/**/*.scss', ['sass']);
gulp.watch([
'source/pages/**/*.tpl',
'source/templates/**/*.tpl'], ['templates']);
gulp.watch([
'_build/**/*.{html,js}'])
.on('change', browserSync.reload);
gulp.watch('source/assets/**', ['copy']);
});
gulp.task('default', [
'sass',
'templates',
'copy',
'serve',
'watch',
'js-watch'
]);