forked from ericmotil/gulp-nunjucks-sass
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Gulpfile.js
150 lines (114 loc) · 4.62 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
'use strict';
// -----------------------------------------------------------------------------
// Dependencies
// -----------------------------------------------------------------------------
var gulp = require('gulp');
var sass = require('gulp-sass');
var autoprefixer = require('gulp-autoprefixer');
var sassdoc = require('sassdoc');
var browserSync = require('browser-sync').create();
var nunjucksRender = require('gulp-nunjucks-render');
var concat = require('gulp-concat');
var imagemin = require('gulp-imagemin');
var pngquant = require('imagemin-pngquant');
var siteOutput = './dist';
// -----------------------------------------------------------------------------
// Configuration
// -----------------------------------------------------------------------------
var input = './scss/*.scss';
var inputMain = './scss/main.scss';
var output = siteOutput + '/css';
var inputTemplates = './pages/*.html';
var sassOptions = { outputStyle: 'expanded' };
var autoprefixerOptions = { browsers: ['last 2 versions', '> 5%', 'Firefox ESR'] };
var sassdocOptions = { dest: siteOutput + '/sassdoc' };
// -----------------------------------------------------------------------------
// Sass compilation
// -----------------------------------------------------------------------------
gulp.task('sass', function() {
return gulp
.src(inputMain)
.pipe(sass(sassOptions).on('error', sass.logError))
.pipe(autoprefixer(autoprefixerOptions))
.pipe(gulp.dest(output))
.pipe(browserSync.stream());
});
// -----------------------------------------------------------------------------
// Javascript
// -----------------------------------------------------------------------------
gulp.task('scripts', function() {
return gulp.src([
'./bower_components/bootstrap-sass/assets/javascripts/bootstrap.js',
'js/main.js'
])
.pipe(concat({ path: 'main.js'}))
.pipe(browserSync.reload({stream:true}))
.pipe(gulp.dest(siteOutput + '/js'));
});
// -----------------------------------------------------------------------------
// Templating
// -----------------------------------------------------------------------------
gulp.task('nunjucks', function() {
nunjucksRender.nunjucks.configure(['./templates/']);
// Gets .html and .nunjucks files in pages
return gulp.src(inputTemplates)
// Renders template with nunjucks
.pipe(nunjucksRender())
// output files in dist folder
.pipe(gulp.dest(siteOutput))
});
// -----------------------------------------------------------------------------
// Imagemin
// -----------------------------------------------------------------------------
gulp.task('img', function() {
return gulp.src('./img/**/*')
.pipe(imagemin({
progressive: true,
svgoPlugins: [{removeViewBox: false}],
use: [pngquant()]
}))
.pipe(gulp.dest(siteOutput + '/img'));
});
// -----------------------------------------------------------------------------
// Fonts
// -----------------------------------------------------------------------------
// gulp.task('fonts', function() {
// return gulp.src(['./fonts/*'])
// .pipe(gulp.dest(siteOutput + '/fonts/'));
// });
// -----------------------------------------------------------------------------
// Sass documentation generation
// -----------------------------------------------------------------------------
gulp.task('sassdoc', function() {
return gulp
.src(input)
.pipe(sassdoc(sassdocOptions))
.resume();
});
// -----------------------------------------------------------------------------
// Watchers
// -----------------------------------------------------------------------------
gulp.task('watch', function() {
// Watch the sass input folder for change,
// and run `sass` task when something happens
gulp.watch(input, ['sass']).on('change', function(event) {
console.log('File ' + event.path + ' was ' + event.type + ', running tasks...');
});
gulp.watch('./js/*', ['scripts']).on('change', browserSync.reload);
// Watch nunjuck templates and reload browser if change
gulp.watch(inputTemplates, ['nunjucks']).on('change', browserSync.reload);
});
// -----------------------------------------------------------------------------
// Static server
// -----------------------------------------------------------------------------
gulp.task('browser-sync', function() {
browserSync.init({
server: {
baseDir: siteOutput
}
});
});
// -----------------------------------------------------------------------------
// Default task
// -----------------------------------------------------------------------------
gulp.task('default', ['sass', 'nunjucks', 'img', 'scripts', 'watch', 'browser-sync']);