-
Notifications
You must be signed in to change notification settings - Fork 1
/
gulpfile.js
405 lines (369 loc) · 14.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
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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
// https://github.com/thecodercoder/frontend-boilerplate
/*
|--------------------------------------------------------------------------
| DEPENDENCIES
|--------------------------------------------------------------------------
*/
// Initialize modules
// Importing specific gulp API functions lets us write them below as series() instead of gulp.series()
const { src, dest, watch, series, parallel } = require('gulp');
// Importing all the Gulp-related packages we want to use
const
// Gulp utilisation
gulp = require('gulp'),
// Combine gulp with sass utilisation
sass = require('gulp-sass'),
// Combine multiples plugins
postcss = require('gulp-postcss'),
// Parser for scss
// scss = require('postcss-scss'),
// Autoprefix css properties
autoprefixer = require('autoprefixer'),
// Clean and minify CSS files
cssnano = require('cssnano'),
// Clean and concat JS files
concat = require('gulp-concat'),
// Minify JS files
terser = require('gulp-terser'),
// Generate sourcemaps
sourcemaps = require('gulp-sourcemaps'),
// Twig
twig = require('gulp-twig'),
// Merge : Permet de faire plusieurs src en une task
merge = require('merge-stream'),
// Delete generated files
del = require('del'),
// SVG sprite
svgSprite = require('gulp-svg-sprite'),
// Gestion Error
plumber = require('gulp-plumber'),
// ZIP
zip = require('gulp-zip'),
// filesize
size = require('gulp-filesize');
/*
|--------------------------------------------------------------------------
| CONFIGURATION
|--------------------------------------------------------------------------
*/
const target = {
'src': './src/',
'buildSite': './site/',
'buildStyleguide': './styleguide/',
};
// File paths
const files = {
scssSite: [target.src + 'style/print.scss', target.src + 'style/style-ans.scss'], // cible uniquement les scss qu'il faut compiler dans le dossier __public/site/
scssStyleguide: [target.src + 'style/*.scss', '!' + target.src + 'style/print.scss', '!' + target.src + 'style/style-ans.scss'],
scssTarteAuCitron: target.src + 'style/styleguide/3-plugin/tarteaucitron-ds-ans.scss',
twigToWatch: target.src + 'twig/**/*.twig',
scssToWatch: target.src + 'style/**/*.scss',
jsAppToWatch: target.src + 'script/app/**/*.js',
jsPluginToWatch: target.src + 'script/plugin/**/*.js',
jsStyleguideToWatch: target.src + 'script/styleguide/**/*.js',
jsBlocsToWatch: target.src + 'script/blocs/**/*.js',
fontToWatch: target.src + 'font/**/*.*',
imgToWatch: target.src + 'img/**/*.*',
iconsToWatch: target.src + 'svg-icons/sprite/*.*'
}
// SVG
var svgSpriteconfig = {
shape: {
dimension: { // Set maximum dimensions
maxWidth: 32,
maxHeight: 32
},
dest: 'sprite' // Keep the intermediate files
},
mode: {
symbol: { // Activate the «symbol» mode
render: {
css: false, // CSS output option for icon sizing
scss: false // SCSS output option for icon sizing
},
dest: ".",
sprite: "icon-sprite.svg", // Sprite path and name
example: true // Build a sample page, please!
}
},
svg: {
doctypeDeclaration: false,
dimensionAttributes: false
}
};
/*
|--------------------------------------------------------------------------
| TASKS
|--------------------------------------------------------------------------
*/
// Clean task: delete generated files
function clean(){
return del([target.buildSite, target.buildStyleguide]);
}
// Twig task: compiles the .twig files into .html
function twigSite(){
return src(target.src + 'twig/site/page/*.*')
.pipe(twig({
errorLogToConsole: true
}))
.pipe(dest(target.buildSite));
}
function twigStyleguide(){
return src(target.src + 'twig/styleguide/page/*.*')
.pipe(twig({
errorLogToConsole: true
}))
.pipe(dest(target.buildStyleguide));
}
function phpStyleguide(){
return src(target.src + 'php/*.*')
.pipe(dest(target.buildStyleguide + 'php'));
}
// Sass Site task: compiles the style.scss file into style.css
function styleSite(){
return src(files.scssSite)
// .pipe(sourcemaps.init()) // initialize sourcemaps first
.pipe(sass().on('error', sass.logError)) // compile SCSS to CSS
.pipe(postcss([
autoprefixer(),
cssnano()
])) // PostCSS plugins
// .pipe(sourcemaps.write('.')) // write sourcemaps file in current directory
.pipe(dest(target.buildSite + 'style'))
.pipe(dest(target.buildStyleguide + 'style')); // put final CSS in dist folder
}
// Sass Styleguide task: compiles the style.scss file into style.css
function styleStyleguide(){
return src(files.scssStyleguide)
// .pipe(sourcemaps.init()) // initialize sourcemaps first
.pipe(sass().on('error', sass.logError)) // compile SCSS to CSS
.pipe(postcss([
autoprefixer(),
cssnano()
])) // PostCSS plugins
// .pipe(sourcemaps.write('.')) // write sourcemaps file in current directory
.pipe(dest(target.buildStyleguide + 'style')); // put final CSS in dist folder
}
// Sass TarteAuCitron task: compiles the tarteaucitron-ds-ans.scss file into tarteaucitron-ds-ans.css
function styleTarteAuCitron(){
return src(files.scssTarteAuCitron)
.pipe(sass().on('error', sass.logError)) // compile SCSS to CSS
.pipe(postcss([
autoprefixer(),
cssnano()
])) // PostCSS plugins
.pipe(dest(target.buildStyleguide + 'style')); // put final CSS in dist folder
}
// JS APP task: concatenates and uglifies JS files to app.js
function scriptApp(){
return src([
// Not jquery
target.src + 'script/app/_detection-js.js',
//
// OPEN jquery
target.src + 'script/app/_jquery-open.js',
//
// > Appeler tous les autres scripts ici
target.src + 'script/app/_links.js',
target.src + 'script/app/_table.js',
target.src + 'script/app/_nav-primary.js',
target.src + 'script/app/_widget-access.js',
target.src + 'script/app/_alert.js',
target.src + 'script/app/_tooltip.js',
//
// > Init des scripts du dessus
target.src + 'script/app/app.js',
//
// Close jquery
target.src + 'script/app/_jquery-close.js',
// Not jquery
])
.pipe(concat('app.js'))
.pipe(terser())
.pipe(dest(target.buildSite + 'script'))
.pipe(dest(target.buildStyleguide + 'script'));
}
// JS PLUGIN task: concatenates and uglifies JS files to vendor.js
function scriptPlugin(){
return src([
//
// OPEN
target.src + 'script/app/_jquery-open.js',
//
// > Appeler tous les autres scripts ici
// * Bootstrap
target.src + 'script/plugin/popper/popper-min.js', // Nécessaire pour faire fonctionner les dropdown
target.src + 'script/plugin/bootstrap/util.js',
// target.src + 'script/plugin/bootstrap/alert.js',
// target.src + 'script/plugin/bootstrap/button.js',
target.src + 'script/plugin/bootstrap/collapse.js',
target.src + 'script/plugin/bootstrap/dropdown.js',
target.src + 'script/plugin/bootstrap/modal.js',
target.src + 'script/plugin/bootstrap/tooltip.js', // Tooltip doit être appelé avant popover : https://stackoverflow.com/questions/18599382/twitter-bootstrap-popover-not-working
// target.src + 'script/plugin/bootstrap/popover.js',
// target.src + 'script/plugin/bootstrap/scrollspy.js',
// target.src + 'script/plugin/bootstrap/tab.js',
// target.src + 'script/plugin/bootstrap/affix-v336-modbs4.js',
// * Others
target.src + 'script/plugin/what-input/what-input.js',
target.src + 'script/plugin/a11y/van11y-accessible-accordion-aria.min.js',
target.src + 'script/plugin/a11y/van11y-accessible-tab-panel-aria.min.js',
//
// CLOSE
target.src + 'script/app/_jquery-close.js',
// Not jquery
// target.src + 'script/plugin/tiny-slider.js',
// target.src + 'script/plugin/svg-icons/svgxuse.js', // Pour l'utilisation du composant svg icons
])
.pipe(concat('vendor.js'))
.pipe(terser())
.pipe(dest(target.buildSite + 'script'))
.pipe(dest(target.buildStyleguide + 'script'));
}
// JS STYLEGUIDE task: concatenates and uglifies JS files to styleguide.js
function scriptStyleguide(){
return src([
//
// OPEN
target.src + 'script/app/_jquery-open.js',
//
// * Custom
target.src + 'script/styleguide/_nav.js',
//
// > Init des scripts du dessus
target.src + 'script/styleguide/styleguide.js',
//
// CLOSE
target.src + 'script/app/_jquery-close.js',
])
.pipe(concat('styleguide.min.js'))
.pipe(terser())
.pipe(dest(target.buildStyleguide + 'script'));
}
// JS BLOCS task
function scriptBlocs(){
return src([
target.src + 'script/plugin/jvectormap/jvectormap.min.js',
target.src + 'script/plugin/tiny-slider/tiny-slider.js',
target.src + 'script/blocs/*.js',
])
.pipe(terser())
.pipe(dest(target.buildStyleguide + 'script'));
}
// SVG Sprite
// Préparation du sprite
// Chaque nouvelle icône doit être nettoyée :
// - Nettoyer les SVG : https://jakearchibald.github.io/svgomg/
// - Harmoniser la taille comme défini dans svgSpriteconfig
// - Vérifier qu'aucun élément n'a de bordure. Si ce n'est pas le cas, transformer les bordures en choisissant Objet > Décomposer dans un logiciel de dessin vectoriel (comme Inkscape)
// - Passer tous les fonds de forme en noir afin de supprimer les infos de couleur dans les svg
// - Placer chaque icône "nettoyée" dans le répertoire svg-icons/sprite/
function createSvgSprite(){
return src(files.iconsToWatch)
.pipe(plumber())
.pipe(svgSprite(svgSpriteconfig))
.pipe(dest(target.buildSite + 'svg-icons'))
.pipe(dest(target.buildStyleguide + 'svg-icons'));
}
// Assets task: copy JS, fonts and imgs
function assets(){
const assetsJs = src([
target.src + 'script/plugin/jquery/jquery-3.5.1.min.js',
])
.pipe(dest(target.buildSite + 'script'))
.pipe(dest(target.buildStyleguide + 'script'));
const assetsJsStyleguide = src([
target.src + 'script/plugin/prism/prism.js',
])
.pipe(dest(target.buildStyleguide + 'script'));
const assetsFont = src(files.fontToWatch)
.pipe(dest(target.buildSite + 'font'))
.pipe(dest(target.buildStyleguide + 'font'));
const assetsImg = src([
target.src + 'img/pictogrammes-illustratifs/*.*',
target.src + 'img/favicon.ico',
target.src + 'img/logo-ANS-footer.svg',
target.src + 'img/logo-ANS.svg',
target.src + 'img/logo-ministere.svg',
], {base: 'src/img'}) // defines a base to keep folder structure: https://stackoverflow.com/questions/35845039/how-base-option-affects-gulp-src-gulp-dest/35848322#35848322
.pipe(dest(target.buildSite + 'img'));
const assetsImgStyleguide = src(files.imgToWatch)
.pipe(dest(target.buildStyleguide + 'img'));
return merge(assetsJs, assetsJsStyleguide, assetsFont, assetsImg, assetsImgStyleguide);
}
// Create zip
function zipStarterKit(){
return src([
target.buildSite + '**/*',
'!' + target.buildSite + '*.html',
target.buildSite + 'layout-base.html'
])
.pipe(zip('starterkit.zip'))
.pipe(dest(target.buildStyleguide + 'zip'));
}
function zipPictogrammesFonctionnels(){
return src(target.src + 'svg-icons/sprite/*.*')
.pipe(zip('pictogrammes-fonctionnels.zip'))
.pipe(dest(target.buildStyleguide + 'zip'));
}
function zipPictogrammesIllustratifs(){
return src(target.src + 'img/pictogrammes-illustratifs/*.*')
.pipe(zip('pictogrammes-illustratifs.zip'))
.pipe(dest(target.buildStyleguide + 'zip'));
}
function zipLogos(){
return src([
target.src + 'img/favicon.ico',
target.src + 'img/logo-ANS.jpg',
target.src + 'img/logo-ANS.png',
target.src + 'img/logo-ANS.svg',
target.src + 'img/logo-ANS-footer.jpg',
target.src + 'img/logo-ANS-footer.png',
target.src + 'img/logo-ANS-footer.svg',
target.src + 'img/logo-ministere.jpg',
target.src + 'img/logo-ministere.png',
target.src + 'img/logo-ministere.svg',
])
.pipe(zip('logotype.zip'))
.pipe(dest(target.buildStyleguide + 'zip'));
}
function zipSizes(){
return src(target.buildStyleguide + 'zip/*.*')
.pipe(size());
}
function zipVersions(){
return src(target.src + 'zip/*.zip')
.pipe(dest(target.buildStyleguide + 'zip'));
}
function zipTarteAuCitron(){
return src([
target.buildStyleguide + 'style/tarteaucitron-ds-ans.css',
])
.pipe(zip('tarteaucitron-ds-ans.css.zip'))
.pipe(dest(target.buildStyleguide + 'zip'));
}
// Watch task: watch SCSS and JS files for changes
function watchTask(){
watch(files.twigToWatch, series(twigSite, twigStyleguide));
watch(files.scssToWatch, series(styleSite, styleStyleguide, styleTarteAuCitron));
watch(files.jsAppToWatch, scriptApp);
watch(files.jsPluginToWatch, scriptPlugin);
watch(files.jsStyleguideToWatch, scriptStyleguide);
watch(files.jsBlocsToWatch, scriptBlocs);
watch(files.fontToWatch, assets);
watch(files.imgToWatch, assets);
watch(files.iconsToWatch, createSvgSprite);
}
// Export the default Gulp task so it can be run
// Runs the scss and js tasks simultaneously
// then watch task
exports.default = series(
clean,
parallel(twigSite, twigStyleguide, phpStyleguide, styleSite, styleStyleguide, styleTarteAuCitron, scriptApp, scriptPlugin, scriptStyleguide, scriptBlocs, assets, createSvgSprite),
zipStarterKit, zipPictogrammesFonctionnels, zipPictogrammesIllustratifs, zipLogos, zipVersions, zipTarteAuCitron, zipSizes
);
exports.watch = series(
clean,
parallel(twigSite, twigStyleguide, phpStyleguide, styleSite, styleStyleguide, styleTarteAuCitron, scriptApp, scriptPlugin, scriptStyleguide, scriptBlocs, assets, createSvgSprite),
zipStarterKit, zipPictogrammesFonctionnels, zipPictogrammesIllustratifs, zipLogos, zipVersions, zipTarteAuCitron, zipSizes, watchTask
);