-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.js
146 lines (129 loc) · 4 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
var gulp = require('gulp'),
rev = require('gulp-rev'),
watch = require('gulp-watch'),
usemin = require('gulp-usemin'),
imagemin = require('gulp-imagemin'),
cssnano = require('gulp-cssnano'),
uglify = require('gulp-uglify'),
sourcemaps = require('gulp-sourcemaps'),
postcss = require('gulp-postcss'),
cssnext = require('postcss-cssnext'),
nested = require('postcss-nested'),
cssimport = require('postcss-import'),
pxtorem = require('postcss-pxtorem'),
mixins = require('postcss-mixins'),
inlinesvg = require('postcss-inline-svg'),
datapacker = require('postcss-data-packer'),
nunjucksrender = require('gulp-nunjucks-render'),
path = require('path'),
del = require('del'),
browserSync = require('browser-sync').create();
// watch
gulp.task('watch', function() {
browserSync.init({
notify: false,
server: {
baseDir: 'app'
}
});
watch('app/**/*.html', function() {
browserSync.reload();
});
watch('app/templates/**/*.nunj', function() {
gulp.start('nunjucks');
browserSync.reload();
});
watch('app/assets/css/**/*.css', function() {
gulp.start('cssInject');
});
watch('app/assets/js/**/*.js', function() {
gulp.start('scriptRefresh');
});
});
// compile nunjucks files
gulp.task('nunjucks', function() {
return gulp.src('app/pages/**/*.+(html|nunj)')
.pipe(nunjucksrender({
path: ['app/templates']
}))
.pipe(gulp.dest('app'))
});
// compile css with postcss plugins
gulp.task('css', function() {
var processors = [
cssimport(),
mixins(),
nested(),
cssnext({ browsers: 'last 2 version, > 1%, ie >= 11, iOS >= 7' }),
pxtorem(),
inlinesvg({ path: 'app/assets/img/' }),
datapacker({
dest: {
path: function(opts) {
return ('app/build/css/' + path.basename(opts.from, '.css') + '-data.css');
},
map: { inline: true }
}
})
];
return gulp.src('app/assets/css/styles.css')
.pipe(sourcemaps.init())
.pipe(postcss(processors))
.pipe(sourcemaps.write())
.on('error', function(errorInfo) {
console.log(errorInfo.toString());
this.emit('end');
})
.pipe(gulp.dest('app/build/css'));
});
// reload browser after saving css without refreshing a dom
gulp.task('cssInject', ['css'], function() {
return gulp.src('app/build/css/styles.css')
.pipe(browserSync.stream());
});
// scripts
gulp.task('scripts', function() {
gulp.src('app/assets/js/**/*.js')
});
gulp.task('scriptRefresh', ['scripts'], function() {
browserSync.reload();
});
// delete dist folder
gulp.task('deleteDist', function() {
return del('dist');
});
// preview distributable version
gulp.task('previewDist', function() {
browserSync.init({
server: {
baseDir: 'dist'
}
});
});
// optimize images and copy to distributable folder
gulp.task('optimizeImages', ['deleteDist'], function() {
return gulp.src(['app/assets/img/**/*.+(jpg|svg|ico)'])
.pipe(imagemin({
progressive: true,
interlaced: true,
multipass: true,
optimizationLevel: 3
}))
.pipe(gulp.dest('dist/assets/img'));
});
// copy fonts to dist folder
gulp.task('fonts', ['deleteDist'], function() {
return gulp.src('app/assets/fonts/**/*')
.pipe(gulp.dest('dist/assets/fonts'));
});
// generate revisioned and minified files
gulp.task('usemin', ['deleteDist', 'css', 'scripts'], function() {
return gulp.src('app/**/*.html')
.pipe(usemin({
css: [function() {return rev()}, function() {return cssnano()}],
js: [function() {return rev()}, function() {return uglify()}]
}))
.pipe(gulp.dest('dist'));
});
// build production environment
gulp.task('build', ['deleteDist', 'optimizeImages', 'fonts', 'usemin']);