This repository has been archived by the owner on May 4, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgulpfile.js
executable file
·296 lines (249 loc) · 9.03 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
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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
"use strict";
// # Fragment Builder
//
// v2.0.2
// ------------------------------------------------*/
// REQUIRE
// ------------------------------------------------*/
var gulp = require('gulp');
//var gutil = require('gulp-util'); // @deprecated
var del = require('del'); // @unused
var lazypipe = require('lazypipe'); // @unused
var browserSync = require('browser-sync').create();
var reload = browserSync.reload;
var sourcemaps = require('gulp-sourcemaps');
// Get args from command line
var argv = require('minimist')(process.argv.slice(2));
var imageResize = require('gulp-image-resize');
//var pngcrush = require('imagemin-pngcrush');
// Auto chargement des plugins
// @deprecated
// @todo revenir au require et savoir ce que l'on charge quand on le charge
// @requires gulp-load-plugin
// @see https://www.npmjs.com/package/gulp-load-plugins
var gulpLoadPlugins = require('gulp-load-plugins');
var plugins = gulpLoadPlugins({
camelize: true,
lazy:true
});
var $ = plugins; //shortcut
// Error handling, notifications
// https://www.npmjs.com/package/gulp-notify
// https://github.com/mikaelbr/node-notifier
// https://github.com/gulpjs/fancy-log
// https://github.com/chalk/chalk
var onError = function(err) {
$.notify.onError({
title: "<%= error.plugin %>" ,
subtitle: "ERROR !!",
icon: "./gulp-tasks/images/warning.png",
message: "<%= error.message %>",
sound: "Basso",
wait: true
})(err);
$.notify.on('click', function (options) {
return false;
});
this.emit('end');
};
// Utils
var assignIn = require('lodash.assignin'),
_ = require('lodash');
// -----------------------------------------------------------
// # Projects Configuration
// -----------------------------------------------------------
// Globals
global._PROJECTS_PATH = "./_KITCHEN/";
global._PROJECT = argv.project ;
global._BASE_PATH = _PROJECTS_PATH + _PROJECT + '/' ;
global._SRC_DIR = "_src/";
global._SRC_PATH = _BASE_PATH + _SRC_DIR;
global._BUILD_DIR = "_BUILD/";
global._BUILD_PATH = _BASE_PATH + _BUILD_DIR;
global._JS_DIR = "assets/js/";
global._CSS_DIR = "assets/css/";
global._IMG_DIR = "assets/images/";
global._SASS_DIR = "assets/_scss/";
global._PAGES_DIR = "pages/";
global._DATAS_DIR = "datas/";
global._TEMPLATES_DIR = "templates/";
// # Project Loader
//
// Chargement du fichier de config passé en argument de la commande.
//
// `gulp task --project=project_name`
//
// @requires minimist
// @see https://www.npmjs.com/package/minimist
// @see https://github.com/gulpjs/gulp/blob/master/docs/recipes/using-external-config-file.md
// @see http://istrategylabs.com/2015/02/swept-up-in-the-stream-5-ways-isl-used-gulp-to-improve-our-process/
// Default configuration
var _config = require('./_config-default');
// Project Overides
var project = require(_BASE_PATH + '/app.js');
var config = _.merge({},_config, project);
// ------------------------------------------------*/
// # SETTINGS | SHORTCUTS
// ------------------------------------------------*/
// SRC
var SrcPath = config.SrcPath; // With a ending /
// Build
var BuildPath = config.BuildPath ;// With a ending /
var JsPath = config.JsPath ;
var ImagePath = config.ImagePath ;
gulp.task('config', function(){
return console.log('Config : ' + JSON.stringify(config, null, 4));
});
// -----------------------------------------------------------
// # TASKS
// -----------------------------------------------------------
/* # Task Loader
[Utiliser un fichier de config externe avec Gulp](https://github.com/gulpjs/gulp/blob/master/docs/recipes/using-external-config-file.md)
@function
@name getTask
@param {task}
@see http://macr.ae/article/splitting-gulpfile-multiple-files.html
@see https://www.npmjs.com/package/gulp-task-loader
*/
function getTask(task) {
return require('./gulp-tasks/' + task)(gulp, plugins, config, browserSync, onError);
}
// Styles
// Pre-pross/ Post : autoprefixing, …
gulp.task('styles', getTask('styles'));
// Linting
// https://github.com/anandthakker/doiuse
var postcss = require('gulp-postcss');
var doiuse = require('doiuse');
// Remove mapSource from stream
// ?? ne fait rien, le lien vers la source map n'est pa effacé du stream
var purgeSourcemaps = require('gulp-purge-sourcemaps');
gulp.task('style-lint',function(){
var report = [];
gulp.src(_SRC_PATH + 'assets/css/*.css', { cwd: process.cwd() })
.pipe(purgeSourcemaps())
// PostCSS
.pipe(postcss([
require("doiuse")({
browsers:['ie >= 11'],
ignore: ['rem','flexbox'], // an optional array of features to ignore
ignoreFiles: ['/main.css.map'], // an optional array of file globs to match against original source file path, to ignore
onFeatureUsage: function(usageInfo) {
report.push(usageInfo.message+'\n');
}
})
]))
.pipe(gulp.dest(config.BuildPath).on('end', function(){
console.log(report);
const fs = require('fs');
fs.writeFile(_SRC_PATH+ _PROJECT+'-style-lint.txt', report, function(err) {
if(err) {
return console.log(err);
}
console.log("The file was saved!");
});
}));
});
// doiuse
// Svg Tool
gulp.task('build-sprite', getTask('svgSprite'));
gulp.task('font-blast', function(){
var explodeFont = require('./gulp-tasks/fontblast')(config.fontblast);
return explodeFont();
});
// ASSETS MANAGMENT
gulp.task('bundle-assets', getTask('bundle-assets'));
// TEMPLATING
gulp.task('prototype', getTask('prototype'));
// IMAGES TOOLS
gulp.task('image-touch-icons', getTask('img_touch-icons')); // Generate touch icons
gulp.task('image-optim', getTask('img_image-optim')); // Optimize
gulp.task('image-resize', getTask('img_image-resize')); // Resize image to a max Size
gulp.task('image-responsives', getTask('img_responsives')); // Generate images variations for different brealpoints
gulp.task('image-galleries', getTask('img_galleries')); // Generate thumbs
// Utils
gulp.task('font2build', function() {
gulp.src(_SRC_PATH + 'assets/fonts/**/*.{woff,woff2}')
.pipe($.plumber({
errorHandler: onError
}))
.pipe($.cached('fonts'))
.pipe(gulp.dest(config.BuildPath))
.pipe(browserSync.stream())
.pipe($.notify({
title: "Font files Updated",
message: "Fon files are updated in Build Folder"
}));
});
gulp.task('images2build', function() {
gulp.src(config.SrcPath + config.ImagePath + '**/*.{jpg,jpeg,png,gif,svg}')
.pipe($.plumber({
errorHandler: onError
}))
.pipe($.cached('images'))
.pipe(gulp.dest(config.BuildPath + config.ImagePath))
.pipe(browserSync.stream())
.pipe($.notify({
title: "Images folder Updated",
message: "All images files are updated in Build Folder"
}));
});
gulp.task('assets2build', function() {
gulp.src(config.SrcPath + '**/*.{css,js}')
.pipe($.plumber({
errorHandler: onError
}))
.pipe($.cached('assets'))
.pipe(gulp.dest(config.BuildPath))
.pipe(browserSync.stream())
.pipe($.notify({
title: "Assets Updated",
message: "Assets files are updated in Build Folder"
}));
});
// browser-sync starting the server.
// https://www.browsersync.io/docs/options
/*
browserSync.init({
open: 'external',
host: 'myproject.local',
proxy: 'myproject.local', // or project.dev/app/
port: 3000
});
*/
gulp.task('browser-sync', function() {
browserSync.init({
server: {
baseDir: _BUILD_PATH
}
});
});
// ---------------------------------------------------------------------------
// # COMMANDS
// ---------------------------------------------------------------------------
/** # Build static site
*
* Génère un site static
*
*
*/
gulp.task('static-site', ['styles', 'prototype', 'bundle-assets','browser-sync'], function () {
gulp.watch(config.sassPath+"**/*.scss", ['styles']);
gulp.watch(config.SrcPath+"templates/*.njk", ['prototype']);
gulp.watch(config.SrcPath+"pages/**/*.md", ['prototype']);
gulp.watch(config.SrcPath+"datas/**/*.json", ['prototype']);
gulp.watch(config.SrcPath+config.ImagePath+"**/*.{jpg,jpeg,png,gif}", ['images2build']);
// gulp.watch(config.SrcPath+config.galleries.folder+"**/*.{jpg,jpeg,png,gif}", ['image-galleries']);
gulp.watch(config.BuildPath + "**/*.html").on('change', browserSync.reload);
gulp.watch(config.SrcPath+"assets/**/*.{css,js}", ['bundle-assets']);
});
// ## Sass-watch
gulp.task('sass-watch', ['styles'], function () {
gulp.watch(config.sassPath+"**/*.scss", ['styles']);
});
// ## Default Task
gulp.task('default', ['styles','bundle-assets'], function () {
gulp.watch(config.sassPath + '**/*.scss', ['styles']);
gulp.watch(config.SrcPath + config.ImagePath + "**/*.{jpg,jpeg,png,gif,svg}", ['images2build']);
gulp.watch(config.SrcPath + "assets/**/*.{css,js}", ['bundle-assets']);
});