-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.babel.js
356 lines (335 loc) · 12.4 KB
/
gulpfile.babel.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
/**
* Gulpfile for ppv-ngg.
*
* Implements:
* + Live reloads browser with BrowserSync.
* + CSS: Sass to CSS conversion, error catching, Autoprefixing, Sourcemaps,
* CSS minification, and Merge Media Queries.
* + Separate task for conditionally loaded CSS.
* + JS: Concatenates & uglifies JS files.
* + Separate task for conditionally loaded JS.
* + Copy PHP files from source to plugin directory.
* + Copy miscellaneous files from source to plugin directory
* For languages, images, changelog, etc.
* + Copy non-processed vendor JS files from node_modules to plugin directory.
* + Watches files for changes in files.
* + Corrects the line endings.
* + InjectCSS instead of browser page reload.
* + Convert readme.txt to readme.md.
*/
/**
* Load WPGulp Configuration.
*/
const config = require('./wpgulp.config.js');
/**
* Load Plugins.
*
* Load gulp plugins and passing them semantic names.
*/
const gulp = require('gulp'); // Gulp of-course.
// CSS related plugins.
const nodeSass = require('node-sass'); // Use node-sass for Sass compilation.
const gulpSass = require('gulp-sass'); // Gulp plugin to use Sass.
const sass = gulpSass( nodeSass );
const minifycss = require('gulp-uglifycss'); // Minifies CSS files.
const autoprefixer = require('gulp-autoprefixer'); // Autoprefixing magic.
const mmq = require('gulp-merge-media-queries'); // Combine matching media queries into one.
// JS related plugins.
const concat = require('gulp-concat'); // Concatenates JS files.
const uglify = require('gulp-uglify'); // Minifies JS files.
const babel = require('gulp-babel'); // Compiles ESNext to browser compatible JS.
// Readme conversion plugin
const convertreadme = require('gulp-readme-to-markdown'); // Convert readme.txt to readme.md.
// Utility related plugins.
const rename = require('gulp-rename'); // Renames files E.g. style.css -> style.min.css.
const lineec = require('gulp-line-ending-corrector'); // Consistent Line Endings for non UNIX systems. Gulp Plugin for Line Ending Corrector (A utility that makes sure your files have consistent line endings).
const filter = require('gulp-filter'); // Enables you to work on a subset of the original files by filtering them using a glob.
const sourcemaps = require('gulp-sourcemaps'); // Maps code in a compressed file (E.g. style.css) back to it’s original position in a source file (E.g. structure.scss, which was later combined with other css files to generate style.css).
const notify = require('gulp-notify'); // Sends message notification to you.
const browserSync = require('browser-sync').create(); // Reloads browser and injects CSS. Time-saving synchronized browser testing.
const remember = require('gulp-remember'); // Adds all the files it has ever seen back into the stream.
const plumber = require('gulp-plumber'); // Prevent pipe breaking caused by errors from gulp plugins.
const beep = require('beepbeep');
const zip = require('gulp-zip'); // Zip plugin or theme file.
const changed = require('gulp-changed'); // Only pass through changed files in the stream
const fancyLog = require('fancy-log'); // Log prefixing with a timestamp.
/**
* Custom Error Handler.
*
* @param Mixed err
*/
const errorHandler = r => {
notify.onError('\n\n❌ ===> ERROR: <%= error.message %>\n')(r);
beep();
// this.emit('end');
};
/**
* Task: 'browser-sync'.
*
* Live Reloads, CSS injections, Localhost tunneling.
* @link http://www.browsersync.io/docs/options/
*
* @param {Mixed} done Done.
*/
const browsersync = done => {
browserSync.init({
proxy: config.projectURL,
open: config.browserAutoOpen,
injectChanges: config.injectChanges,
watchEvents: ['change', 'add', 'unlink', 'addDir', 'unlinkDir']
});
done();
};
// Helper function to allow browser reload with Gulp 4.
const reload = done => {
browserSync.reload();
done();
};
/**
* Task: 'mainstyles'.
*
* Compiles Sass, Autoprefixes it and Minifies CSS.
*
* This task does the following:
* 1. Gets the source scss file
* 2. Compiles Sass to CSS
* 3. Writes Sourcemaps for it
* 4. Autoprefixes it and generates style.css
* 5. Renames the CSS file with suffix .min.css
* 6. Minifies the CSS file and generates style.min.css
* 7. Injects CSS or reloads the browser via browserSync
*/
gulp.task('mainstyles', () => {
return gulp
.src(config.styleMainSRC, {allowEmpty: true})
.pipe(plumber(errorHandler))
.pipe(sourcemaps.init())
.pipe(
sass({
errLogToConsole: config.errLogToConsole,
outputStyle: config.outputStyle,
precision: config.precision
})
)
.on('error', sass.logError)
.pipe(sourcemaps.write({includeContent: false}))
.pipe(sourcemaps.init({loadMaps: true}))
.pipe(autoprefixer(config.BROWSERS_LIST))
.pipe(sourcemaps.write('./'))
.pipe(lineec()) // Consistent Line Endings for non UNIX systems.
.pipe(gulp.dest(config.styleMainDEST))
.pipe(filter('**/*.css')) // Filtering stream to only css files.
.pipe(mmq({log: true})) // Merge Media Queries only for .min.css version.
.pipe(browserSync.stream()) // Reloads style if that is enqueued.
.pipe(rename({suffix: '.min'}))
.pipe(minifycss({maxLineLen: 10}))
.pipe(lineec()) // Consistent Line Endings for non UNIX systems.
.pipe(gulp.dest(config.styleMainDEST))
.pipe(filter('**/*.css')) // Filtering stream to only css files.
.pipe(browserSync.stream()) // Reloads style min if that is enqueued.
});
/**
* Task: 'multistyles'.
*
* Compiles Multiple Sass, Autoprefixes it and Minifies CSS.
*
* This task does the following:
* 1. Gets alls the source scss files
* 2. Compiles Sass to CSS
* 3. Writes Sourcemaps for it
* 4. Autoprefixes it and generates style.css
* 5. Renames the CSS file with suffix .min.css
* 6. Minifies the CSS file and generates style.min.css
* 7. Injects CSS or reloads the browser via browserSync
*/
gulp.task('multistyles', () => {
return gulp
.src(config.styleMultiSRC, {allowEmpty: true})
.pipe(plumber(errorHandler))
.pipe(sourcemaps.init())
.pipe(
sass({
errLogToConsole: config.errLogToConsole,
outputStyle: config.outputStyle,
precision: config.precision
})
)
.on('error', sass.logError)
.pipe(sourcemaps.write({includeContent: false}))
.pipe(sourcemaps.init({loadMaps: true}))
.pipe(autoprefixer(config.BROWSERS_LIST))
.pipe(sourcemaps.write('./'))
.pipe(lineec()) // Consistent Line Endings for non UNIX systems.
.pipe(gulp.dest(config.styleMultiDEST))
.pipe(filter('**/*.css')) // Filtering stream to only css files.
.pipe(mmq({log: true})) // Merge Media Queries only for .min.css version.
.pipe(browserSync.stream()) // Reloads style if that is enqueued.
.pipe(rename({suffix: '.min'}))
.pipe(minifycss({maxLineLen: 10}))
.pipe(lineec()) // Consistent Line Endings for non UNIX systems.
.pipe(gulp.dest(config.styleMultiDEST))
.pipe(filter('**/*.css')) // Filtering stream to only css files.
.pipe(browserSync.stream()) // Reloads style min if that is enqueued.
});
/**
* Task: 'mainJS'.
*
* Concatenate and uglify main JS scripts.
*
* This task does the following:
* 1. Gets the source folder for JS main files
* 2. Concatenates all the files and generates main.js
* 3. Renames the JS file with suffix .min.js
* 4. Uglifes/Minifies the JS file and generates main.min.js
*/
gulp.task('mainJS', () => {
return gulp
.src(config.jsMainSRC, {since: gulp.lastRun('mainJS')}) // Only run on changed files.
.pipe(plumber(errorHandler))
.pipe(
babel({
presets: [
[
'@babel/preset-env', // Preset to compile your modern JS to ES5.
{
targets: {browsers: config.BROWSERS_LIST} // Target browser list to support.
}
]
]
})
)
.pipe(remember(config.jsMainSRC)) // Bring all files back to stream.
.pipe(concat(config.jsMainFile + '.js'))
.pipe(lineec()) // Consistent Line Endings for non UNIX systems.
.pipe(gulp.dest(config.jsMainDEST))
.pipe(
rename({
basename: config.jsMainFile,
suffix: '.min'
})
)
.pipe(uglify())
.pipe(lineec()) // Consistent Line Endings for non UNIX systems.
.pipe(gulp.dest(config.jsMainDEST))
});
/**
* Task: 'multiJS'.
*
* Uglify mutliple JS scripts.
*
* This task does the following:
* 1. Gets the source folder for JS files
* 2. Copies source JS files to plugin folder
* 3. Renames the JS files with suffix .min.js
* 4. Uglifes/Minifies the JS files and places them in plugin folder
*/
gulp.task('multiJS', () => {
return gulp
.src(config.jsMultiSRC, {since: gulp.lastRun('multiJS')}) // Only run on changed files.
.pipe(plumber(errorHandler))
.pipe(
babel({
presets: [
[
'@babel/preset-env', // Preset to compile your modern JS to ES5.
{
targets: {browsers: config.BROWSERS_LIST} // Target browser list to support.
}
]
]
})
)
.pipe(remember(config.jsMultiSRC)) // Bring all files back to stream.
.pipe(lineec()) // Consistent Line Endings for non UNIX systems.
.pipe(gulp.dest(config.jsMainDEST))
.pipe(
rename({
suffix: '.min'
})
)
.pipe(uglify())
.pipe(lineec()) // Consistent Line Endings for non UNIX systems.
.pipe(gulp.dest(config.jsMainDEST))
});
/**
* Task: 'copyvendorJS'.
*
* Copy non-processed vendor JS files from node_modules to plugin directory.
* simplelightbox, justifiedGallery
*/
gulp.task('copyvendorJS', () => {
return gulp
.src(config.vendorJsSRC)
// .pipe(changed(config.vendorJsDEST))
.pipe(gulp.dest(config.vendorJsDEST))
});
/**
* Task: 'copyPHP'.
*
* Copy PHP files from source to plugin directory.
*
*/
gulp.task('copyPHP', () => {
return gulp
.src(config.phpSRC)
.pipe(changed(config.phpDEST))
.pipe(gulp.dest(config.phpDEST))
});
/**
* Task: 'copymisc'.
*
* Copy miscellaneous files from source to plugin directory.
* languages, images, changelog, etc.
*/
gulp.task('copymisc', () => {
return gulp
.src(config.miscGlob)
.pipe(changed(config.miscDEST))
.pipe(gulp.dest(config.miscDEST))
});
/**
* Task: 'readme'.
*
* Convert WordPress readme.txt to github readme.md
*/
gulp.task('readme', (done) => {
gulp.src([ config.readmeSRC ])
.pipe(convertreadme({
details: false
}))
.pipe(gulp.dest(config.readmeDEST));
done();
});
/**
* Task: 'package'.
*
* Packages all the processed plugin files in a ZIP file.
* Base option of gulp.src is one level above plugin folder
* so the packaged zip has the folder name inside the zip.
*
* Zip file source: ../site/wp-content/plugins/pluginName.
* Zip destination: ./dist.
* zipName: pluginName.zip.
*/
gulp.task('package', () => {
fancyLog("Packaging plugin files in a ZIP file");
return gulp
.src(config.zipSRC, {base: config.WPpluginFolder})
.pipe(zip(config.zipName))
.pipe(gulp.dest(config.zipDEST));
});
/**
* Watch Tasks.
*
* Watches for file changes and runs specific tasks.
* Does not watch readme or copyvendorJS.
*/
gulp.task(
'default',
gulp.series( 'readme', gulp.parallel('mainstyles', 'multistyles', 'mainJS', 'multiJS', 'copyPHP', 'copymisc', 'copyvendorJS', browsersync, () => {
gulp.watch(config.watchPhp, gulp.series('copyPHP', reload)); // Reload on PHP file changes.
gulp.watch(config.watchMisc, gulp.series('copymisc', reload)); // Reload on miscellaneous file changes.
gulp.watch(config.watchStyles, gulp.parallel('mainstyles', 'multistyles')); // Reload on SCSS file changes.
gulp.watch(config.watchJs, gulp.series('mainJS', 'multiJS', reload)); // Reload on JS file changes.
}))
);