-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.js
157 lines (129 loc) · 4.68 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
151
152
153
154
155
156
157
'use strict';
/* GULP PLUGINS
========================================================================== */
var gulp = require('gulp'),
sass = require('gulp-sass'),
prefix = require('gulp-autoprefixer'),
rename = require('gulp-rename'),
livereload = require('gulp-livereload'),
postcss = require('gulp-postcss'),
cleanCSS = require('gulp-clean-css'),
changed = require('gulp-changed'),
sourcemaps = require('gulp-sourcemaps'),
shell = require('gulp-shell'),
clean = require('gulp-clean'),
webp = require('gulp-webp'),
imagemin = require('gulp-imagemin'),
gulpCopy = require('gulp-copy'),
requirejsOptimize = require('gulp-requirejs-optimize'),
rev = require('gulp-rev'),
revDel = require('rev-del'),
override = require('gulp-rev-css-url'),
runSequence = require('run-sequence');
/* CONFS
========================================================================== */
livereload({ start: true });
/* DEV TASKS
========================================================================== */
//Go to style guide folder and Start Fractal Server
gulp.task('fractalStart', shell.task([
//'fractal start --sync'
]));
//Compile the Sass css, prefix the styles and create the sourceMap for the dev CSS
gulp.task('css', function() {
return gulp.src('dev/sass/main-sass.scss')
.pipe(sourcemaps.init())
.pipe(sass({ errLogToConsole:true }))
.pipe(prefix('last 15 version'))
.pipe(rename('main.css'))
.pipe(sourcemaps.write())
.pipe(gulp.dest('dev/css'));
});
/* PROD TASKS
========================================================================== */
//Minify the dev CSS for production
gulp.task('minifyCSS', function () {
return gulp.src('dev/sass/main-sass.scss')
.pipe(sass({ errLogToConsole:true }))
.pipe(prefix('last 15 version'))
.pipe(cleanCSS({compatibility: 'ie8'}))
.pipe(rename('main.css'))
.pipe(gulp.dest('assets/css'));
});
//Use RequireJS to concatenate the js assets for production
gulp.task('scripts', function () {
return gulp.src('dev/js/main.js')
.pipe(requirejsOptimize(function(file) {
return {
name: 'main',
mainConfigFile: 'dev/js/main.js',
optimize: 'uglify2',
useStrict: true,
baseUrl: 'dev/js',
//include: ['lib/require.js'] /* If the site uses requireJS external links manager */
include: ['lib/almond.js'] /* If the site don't use any external links manager, we use almond.js for building */
};
}))
.pipe(gulp.dest('assets/js'));
});
//Creates WEBP image file
gulp.task('webp', function () {
return gulp.src(['dev/img/**/*', '!dev/img/**/*.svg'])
.pipe(webp({
quality: 90
}))
.pipe(gulp.dest('dev/img-min'));
});
//Minify the images
gulp.task('imageMin', function () {
return gulp.src('dev/img/**/*')
.pipe(changed('dev/img-min'))
.pipe(imagemin({
optimizationLevel : 7
// use: [pngcrush()]
}))
.pipe(gulp.dest('dev/img-min'));
});
//Clean the assets folder which will receive the rev'ed assets
gulp.task('clean', function () {
return gulp.src('assets', {read: false})
.pipe(clean());
});
//Copy the compressed images to the assets folder to be rev'ed
gulp.task('copy', function () {
return gulp.src(['dev/img-min/**/*'], {base: 'assets'})
.pipe(gulpCopy('assets/img', { prefix: 2}))
});
//CREATES REV VERSION
gulp.task('rev', function () {
// by default, gulp would pick `assets/css` as the base,
// so we need to set it explicitly:
return gulp.src(['assets/css/main.css', 'assets/js/main.js', 'assets/img/**/*.*', '!assets/img/**/*.webp'], {base: 'assets'})
.pipe(rev())
.pipe(override())
.pipe(gulp.dest('assets')) // write rev'd assets to build dir
.pipe(rev.manifest())
.pipe(revDel({ dest: 'assets' }))
.pipe(gulp.dest('assets')); // write manifest to build dir
});
/* DEV TASKS
========================================================================== */
//Creates a livereload Server and build static assets on the fly.
gulp.task('default', function() {
//Star the liveReload Server
livereload({ start: true });
//Start the FractalServer
//gulp.start('fractalStart');
//When a SASS file change, build the CSS
gulp.watch('dev/sass/**/*.scss', ['css']);
//When any CSS, HMTL, or JS files change, reload the browser
gulp.watch(['dev/css/main.css', '**/*.html', '**/*.php', 'dev/js/**/*.js']).on('change', function(file) {
livereload.changed(file.path);
});
});
//Build the complete site inside the "build" folder
gulp.task('build', function(callback) {
runSequence('clean', 'minifyCSS', 'scripts', 'webp', 'imageMin', 'copy', 'rev', function () {
process.exit(0);
});
});