This repository was archived by the owner on Aug 20, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgulpfile.js
More file actions
234 lines (199 loc) · 6.17 KB
/
Copy pathgulpfile.js
File metadata and controls
234 lines (199 loc) · 6.17 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
/**
* @fileoverview
* Runs the gulp build tasks
*/
'use strict';
// Load plugins
var gulp = require('gulp'),
// Live reloading localhost
browserSync = require('browser-sync'),
// Creates OSX notifications
notify = require('gulp-notify'),
// Copy referenced assets to a folder
copyAssets = require('postcss-copy-assets'),
// Node modules
URL = require('url'),
pathUtils = require('path'),
prettyHrtime = require('pretty-hrtime'),
// Gulp modules
del = require('del'),
deploy = require('gulp-gh-pages'),
gulp = require( 'gulp' ),
gulpif = require( 'gulp-if' ),
gutil = require('gulp-util'),
jade = require('gulp-jade'),
less = require( 'gulp-less' ),
sourcemaps = require('gulp-sourcemaps'),
// Plumbers fix pipes
plumber = require('gulp-plumber'),
// Styles
postcss = require('gulp-postcss'),
cssnano = require('cssnano'),
pixrem = require('pixrem'),
rgbaFallback = require('postcss-color-rgba-fallback'),
opacity = require('postcss-opacity'),
autoprefixer = require('autoprefixer'),
lessGlob = require('less-plugin-glob'),
// Compress images
imagemin = require('gulp-imagemin');
/*
* Less tasks
*/
// Clean the build directory
gulp.task( 'clean-less', function() {
return del('public/css/**');
});
gulp.task( 'less', function() {
var postCssPlugins = [
autoprefixer({ browsers: ['> 1%','last 2 version','ie >= 8'], flexbox: 'no-2009' }),
rgbaFallback,
opacity,
pixrem,
copyAssets
];
// Only include cssnano on a production build as it takes ages
if ( productionBuild ) {
postCssPlugins.push(cssnano);
}
var targetPath = 'public/css';
return gulp.src(['libs/styles/*.less'])
.pipe( gulpif(!productionBuild, sourcemaps.init({largeFile: true})) ) // Generate sourcemaps
.pipe(less({ plugins: [lessGlob] }).on('error', function(err){
gutil.log(err);
this.emit('end');
}))
.on("error", notify.onError(function (error) {
return error.message;
}))
.pipe( gulpif(!productionBuild,sourcemaps.write()) )
.pipe( gulpif(!productionBuild,sourcemaps.init({loadMaps: true})) ) // Generate sourcemaps
// .pipe( sourcemaps.init({loadMaps: true} )
.pipe( postcss( postCssPlugins, { to: targetPath + '/assets' } ) ) // Postcss (the "to" is for copyAssets)
.pipe( gulpif(!productionBuild,sourcemaps.write('.')) ) // Write the sourcemaps
.pipe( gulp.dest(targetPath) ) // Write the less
.pipe(browserSync.reload({stream: true}))
.pipe( notify({ message: 'Less successfully compiled' }));
});
gulp.task( 'watch-less', function() {
gulp.watch( 'libs/styles/**/*.less', ['less'] );
});
/*
* Jade tasks
*/
gulp.task( 'clean-jade', function() {
return del([
// Delete the contents of public/
'public/*',
// but ignore these ones
'!public/js',
'!public/css',
'!public/images',
'!public/robots.txt'
]);
});
gulp.task( 'jade', function() {
return gulp.src(['libs/jade/**/*.jade', '!libs/jade/templates/**/*.jade'])
.pipe( jade( { pretty: true, basedir: "libs" }) )
.pipe( gulp.dest('public') )
.pipe(browserSync.reload({stream: true}));
});
gulp.task( 'watch-jade', function() {
// This just rebuilds the affects file - cheap
gulp.watch('libs/jade/**/*.jade', function(event) {
// This is mostly a hack to make it look like proper task in the console
var start = process.hrtime();
gutil.log('Starting', "'" + gutil.colors.cyan('jade') + '"...');
// Find the relative path between the jade directory and the file
// and apply that to the public directory to get the right folder
var relativePath = pathUtils.parse( pathUtils.relative(
pathUtils.resolve( __dirname, 'libs/jade' ),
event.path
)).dir;
relativePath = pathUtils.resolve( 'public', relativePath );
gulp.src(event.path)
.pipe(plumber())
.pipe(jade({ pretty: true, basedir: "libs" }).on('error', function(err){
gutil.log(err);
this.emit('end');
}))
.on("error", notify.onError(function (error) {
return error.message;
}))
.pipe(gulp.dest(relativePath))
.on( 'finish', function() {
var end = process.hrtime(start);
gutil.log(
'Finished',
"'" + gutil.colors.cyan('jade') + '"',
'after',
gutil.colors.magenta(prettyHrtime(end))
);
})
.pipe(browserSync.reload({stream: true}))
.pipe( notify({ message: 'Jade successfully compiled' }));
});
// This will rebuild all of jade - expensive
//gulp.watch('libs/template/*.jade', ['jade']);
});
/*
* Images tasks
*/
gulp.task( 'clean-images', function() {
return del('public/images');
});
gulp.task('images', function() {
gulp.src('libs/assets/images/**/*')
.pipe(plumber())
.pipe(imagemin())
.pipe(gulp.dest('public/images'))
// .pipe(notify({ message: 'Images complete' }));
});
gulp.task( 'watch-images', function() {
gulp.watch( 'libs/assets/images/**/*', ['images'] );
});
/*
* gh-pages
*/
gulp.task( 'gh-pages', ['build'], function() {
return gulp.src('public/**')
.pipe( deploy() );
});
/*
* Global tasks
*/
// Starts localhost
gulp.task('local', function() {
browserSync({
server: { baseDir: "public/" },
// Makes sure it opens in Chrome, regardless of default browser
browser: "google chrome",
notify: false
});
});
// Flag to tell us this is a production build
var productionBuild = false;
gulp.task( 'production', function() {
// This task just sets a flag to say we are in production mode
// This is a bit of a hack as this is task is technically carried out
// in parallel to other tasks and order is not guarenteed,
// however as long as it is specified
// first it should first.
productionBuild = true;
});
gulp.task( 'clean', function() {
// This task just sets a flag to say we are in production mode
// This is a bit of a hack as this is task is technically carried out
// in parallel to other tasks and order is not guarenteed,
// however as long as it is specified
// first it should first.
productionBuild = true;
});
// ------- Task groups --------
// Clean all the things
gulp.task( 'clean', [ 'clean-jade', 'clean-less', 'clean-images'] );
// Watch all the things
gulp.task( 'watch', ['default', 'watch-less', 'watch-jade', 'watch-images', 'local'] );
// Single build
gulp.task( 'default', ['less','jade', 'images'] );
// Production build - also minifies the JS
gulp.task( 'build', ['production', 'clean', 'default'] );