This repository has been archived by the owner on Oct 10, 2024. It is now read-only.
forked from udacity/mws-restaurant-stage-1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.js
234 lines (200 loc) · 7.46 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
/** ===========================================================================
* Gulp configuration file.
* ========================================================================= */
// Requis
"use strict";
// Variables de chemins
var src = "./src"; // dossier de travail
var dist = "./dist"; // dossier à livrer
/** ---------------------------------------------------------------------------
* Load plugins.
* ------------------------------------------------------------------------- */
var gulp = require("gulp");
var autoprefixer = require("gulp-autoprefixer");
var sass = require("gulp-sass");
var concat = require("gulp-concat");
var babel = require("gulp-babel");
var imagemin = require("gulp-imagemin");
var sequence = require("run-sequence");
var csso = require("gulp-csso");
var clean = require("gulp-clean");
var cache = require("gulp-cache");
var rename = require("gulp-rename");
var plumber = require("gulp-plumber");
var htmlbeautify = require("gulp-html-beautify");
var jimpresize = require("gulp-jimp-resize");
var eslint = require("gulp-eslint");
var notify = require("gulp-notify");
var uglify = require('gulp-uglify-es').default; /* For ES6 ! */
var browserSync = require("browser-sync").create();
var htmlclean = require('gulp-htmlclean');
var htmlmin = require('gulp-htmlmin');
var replace = require('gulp-replace');
var inject = require('gulp-inject');
// Include plugins automatiquement
var plugins = require("gulp-load-plugins")({pattern: ["gulp-*", "gulp.*"], replaceString: /\bgulp[\-.]/}); // tous les plugins de package.json
/* ************* JS FILE ************* */
// minify JS
gulp.task("JSminify", function() {
return gulp.src(src + "/js/*.js")
.pipe(babel())
.pipe(uglify())
.pipe(plugins.rename({suffix: ".min"}))
.pipe(gulp.dest(dist + "/js"))
.pipe(plugins.notify({title: "Gulp",message: "JSminify Done"}));
});
// Check JS code for error with ES lint
gulp.task("JSlint", function() {
return gulp.src([src + "/js/*.js","!node_modules/**"])
.pipe(eslint({configFile: 'eslintrc.json'}))
.pipe(eslint.format())
.pipe(eslint.result(result => {
// Called for each ESLint result.
console.log(`ESLint result: ${result.filePath}`);
console.log(`# Messages: ${result.messages.length}`);
console.log(`# Warnings: ${result.warningCount}`);
console.log(`# Errors: ${result.errorCount}`);
}));
});
// Idem as previous but with a fail if error
gulp.task("JSlintFail", function() {
return gulp.src([src + "/js/*.js","!node_modules/**"])
.pipe(eslint({configFile: 'eslintrc.json'}))
.pipe(eslint.format())
.pipe(eslint.failAfterError());
});
// Copy SW to dist
gulp.task('JScopy', function (cb) {
return gulp.src([src + "/sw.js", src + "/app.js"])
.pipe(plugins.rename({suffix: ".min"}))
.pipe(replace('.css', '.min.css'))
.pipe(replace('.js', '.min.js'))
.pipe(replace('.min.json', '.json'))
.pipe(uglify())
.pipe(gulp.dest(dist + "/"))
.pipe(plugins.notify({title: "Gulp",message: "JScopy Done"}));
});
/* ************* CSS FILE ************* */
// Sass
gulp.task("CSSsass", function() {
return gulp.src(src+"/sass/*.scss")
.pipe(plugins.sass())
.pipe(plugins.autoprefixer({browsers:["last 2 versions"]}))
.pipe(gulp.dest(src + "/css"))
.pipe(plugins.notify({title: "Gulp",message: "CSSsass Done"}));
});
// Concatenate CSS
gulp.task("CSSconcat", function() {
return gulp.src([src + "/css/*.css","!"+ src+"/css/*.min.css","!"+ src+"/css/small.css"]) // ATTENTION Exeption sur les min.css
.pipe(plugins.concat("styles.css"))
.pipe(gulp.dest(dist + "/css"))
.pipe(plugins.notify({title: "Gulp",message: "CSSconcat Done"}));
});
// Minify CSS
gulp.task("CSSminify", function() {
return gulp.src([dist + "/css/*.css", "!"+ dist + "/css/*.min.css", src+"/css/small.css"])
.pipe(plugins.csso())
.pipe(plugins.rename({suffix: ".min"}))
.pipe(gulp.dest(dist + "/css"))
.pipe(plugins.notify({title: "Gulp",message: "CSSminify Done"}));
});
/* ************* HTML FILE ************* */
// Minify HTMLpages
gulp.task("HTMLpages", function() {
return gulp.src(dist + "/*.html")
.pipe(plugins.htmlclean())
.pipe(plugins.htmlmin({collapseWhitespace: true}))
.pipe(gulp.dest(dist + "/"));
});
// Inject .min.* in HTML
gulp.task('HTMLinject', function () {
return gulp.src(src + "/*.html")
.pipe(inject(gulp.src(dist + '/css/small.min.css'), {
relative: true,
starttag: '<!-- inject:head:css -->',
transform: function(filepath, file) {
return '<link rel="preload" href="./css/styles.min.css" as="style"><style>'+file.contents.toString()+'</style>';
}
}))
.pipe(inject(gulp.src(dist + '/js/main.min.js', {read: false}), {relative: true, starttag: '<!-- inject:specificMain:js -->'}))
.pipe(inject(gulp.src(dist + '/js/restaurant_info.min.js', {read: false}), {relative: true, starttag: '<!-- inject:specificResto:js -->'}))
.pipe(inject(gulp.src([dist + '/js/*.min.js', dist + '/*.min.js', '!' + dist + '/sw.min.js', '!' + dist + '/js/main.min.js', '!' + dist + '/js/restaurant_info.min.js', dist + '/css/*.min.css', '!' + dist + '/css/small.min.css'], {read: false}), {relative: true}))
.pipe(replace('../dist/', './'))
.pipe(gulp.dest(dist + "/"));
});
/* ************* IMAGES ************* */
// Optimize .jpg picture
gulp.task("IMGoptimize", function() {
return gulp.src([src + "/img/*.jpg", src + "/img/*.webp"])
.pipe(imagemin([
imagemin.jpegtran({progressive: true}),
]))
.pipe(gulp.dest(dist + "/img/"));
});
// Optimize logo
gulp.task("LOGOoptimize", function() {
return gulp.src([src + "/logo/*.png",src + "/logo/*.svg"])
.pipe(imagemin([
imagemin.optipng({optimizationLevel: 5}),
imagemin.svgo({plugins:[{removeViewBox: true},{cleanupIDs: false}]})
]))
.pipe(gulp.dest(dist + "/logo/"));
});
/* *************** MOVE & COPY ************* */
// copy FONTS
gulp.task("FONTScopy", function() {
return gulp.src(src + "/fonts/*")
.pipe(gulp.dest(dist + "/fonts/"))
});
// copy JSON
gulp.task("JSONcopy", function() {
return gulp.src(src + "/manifest.json")
.pipe(gulp.dest(dist + "/"))
});
// copy JSON
gulp.task("HTACCESScopy", function() {
return gulp.src(src + "/.htaccess")
.pipe(gulp.dest(dist + "/"))
});
// copy XML
gulp.task("XMLcopy", function() {
return gulp.src(src + "/sitemap.xml")
.pipe(gulp.dest(dist + "/"))
});
// copy TXT
gulp.task("TXTcopy", function() {
return gulp.src(src + "/robots.txt")
.pipe(gulp.dest(dist + "/"))
});
/* ************* TASKS MANAGER ************* */
// "Run_style" Task
gulp.task("Run_style", function(cb) {
sequence("CSSsass", "CSSconcat", "CSSminify", cb);
});
// "Run_script" Task
gulp.task("Run_script", function(cb) {
sequence("JScopy", "JSminify", cb);
});
// "Run_images" Task
gulp.task("Run_images", function(cb) {
sequence(["IMGoptimize", "LOGOoptimize"], cb);
});
// "Run_pages" Task
gulp.task("Run_pages", function(cb) {
sequence("HTMLinject",["FONTScopy","JSONcopy", "HTACCESScopy","XMLcopy","TXTcopy"],"HTMLpages", cb);
});
// Server live editing
gulp.task("serve", function() {
browserSync.init({
server: "./dist"
});
gulp.watch("scss/*.scss", ["Run_style"]);
gulp.watch("css/*.css", ["Run_style"]);
gulp.watch("js/*.js", ["Run_script"]);
gulp.watch("*.html", ["Run_pages"]);
gulp.watch("*.html").on("change", browserSync.reload);
});
// Default Task
gulp.task("default", ["JSlintFail"] ,function(cb) {
sequence(["Run_style", "Run_script", "Run_images","Run_pages"], cb);
});