forked from urvanov-ru/crayon-syntax-highlighter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgulpfile.js
468 lines (397 loc) · 13.3 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
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
// phpcs:disable
/**
* Gulpfile.
*
* Gulp with WordPress.
*
* Implements:
* 1. Live reloads browser with BrowserSync.
* 2. CSS: Sass to CSS conversion, error catching, Autoprefixing, Sourcemaps,
* CSS minification, and Merge Media Queries.
* 3. JS: Concatenates & uglifies Vendor and Custom JS files.
* 4. Images: Minifies PNG, JPEG, GIF and SVG images.
* 5. Watches files for changes in CSS or JS.
* 6. Watches files for changes in PHP.
* 7. Corrects the line endings.
* 8. InjectCSS instead of browser page reload.
* 9. Generates .pot file for i18n and l10n.
*
* @author Kevin Provance (@kprovance)
* @version 2.0.0 - Rewrite for Gulp 4.0
*/
/**
* Configuration.
*
* Project Configuration for gulp tasks.
*
* In paths you can add <<glob or array of globs>>. Edit the variables as per your project requirements.
*/
// START Editing Project Variables.
// CHANGE THESE FOR EACH NEW VERSION...then compile this script. It'll update the data in the PHP files.
var pluginVersion = '2.9.0';
var pluginDate = 'April 11, 2021';
// Project related.
var projectURL = 'http://127.0.0.1'; // Project URL. Could be something like localhost:8888.
// Translation related.
var bugReport = 'https://github.com/urvanov-ru/crayon-syntax-highlighter/issues'; // Where can users report bugs.
var lastTranslator = 'Kevin Provance <[email protected]>'; // Last translator Email ID.
var team = 'urvanov.ru <[email protected]>'; // Team's Email ID.
// Browsers you care about for autoprefixing.
// Browserlist https://github.com/ai/browserslist.
var AUTOPREFIXER_BROWSERS = [ 'last 2 version', '> 1%', 'ie > 10', 'ie_mob > 10', 'ff >= 30', 'chrome >= 34', 'safari >= 7', 'opera >= 23', 'ios >= 7', 'android >= 4', 'bb >= 10' ];
// STOP Editing Project Variables.
/**
* Load Plugins.
*
* Load gulp plugins and assing them semantic names.
*/
var gulp = require( 'gulp' ); // Gulp of-course.
// CSS related plugins.
var sass = require( 'gulp-sass' ); // Gulp pluign for Sass compilation.
var minifycss = require( 'gulp-uglifycss' ); // Minifies CSS files.
var autoprefixer = require( 'gulp-autoprefixer' ); // Autoprefixing magic.
var mmq = require( 'gulp-merge-media-queries' ); // Combine matching media queries into one media query definition.
// JS related plugins.
var concat = require( 'gulp-concat' ); // Concatenates JS files.
var uglify = require( 'gulp-uglify' ); // Minifies JS files.
// Image realted plugins.
var imagemin = require( 'gulp-imagemin' ); // Minify PNG, JPEG, GIF and SVG images with imagemin.
// Utility related plugins.
var rename = require( 'gulp-rename' ); // Renames files E.g. style.css -> style.min.css.
var lineec = require( 'gulp-line-ending-corrector' ); // Consistent Line Endings for non UIX systems. Gulp Plugin for Line Ending Corrector (A utility that makes sure your files have consistent line endings).
var filter = require( 'gulp-filter' ); // Enables you to work on a subset of the original files by filtering them using globbing.
var sourcemaps = require( 'gulp-sourcemaps' ); // Maps code in a compressed file (E.g. style.css) back to it’s original position in a source file.
var browserSync = require( 'browser-sync' ).create(); // Reloads browser and injects CSS. Time-saving synchronised browser testing.
var wpPot = require( 'gulp-wp-pot' ); // For generating the .pot file.
var sort = require( 'gulp-sort' ); // Recommended to prevent unnecessary changes in pot-file.
var merge = require( 'merge-stream' );
var sassPackager = require( 'gulp-sass-packager' );
var replace = require( 'gulp-replace' );
var clean = require( 'gulp-clean' );
var fs = require( 'fs' );
sass.compiler = require( 'node-sass' );
/**
* Task: `browser-sync`.
*
* Live Reloads, CSS injections, Localhost tunneling.
*
* This task does the following:
* 1. Sets the project URL
* 2. Sets inject CSS
* 3. You may define a custom port
* 4. You may want to stop the browser from openning automatically
*/
gulp.task(
'browser-sync',
function() {
'use strict';
browserSync.init(
{
// For more options.
// @link http://www.browsersync.io/docs/options.
// Project URL.
proxy: projectURL,
// `true` Automatically open the browser with BrowserSync live server.
// `false` Stop the browser from automatically opening.
open: true,
// Inject CSS changes.
// Commnet it to reload browser for every CSS change.
injectChanges: true,
// Use a specific port (instead of the one auto-detected by Browsersync).
// port: 7000.
}
);
}
);
function processScss( source, dest, addMin ) {
'use strict';
var process = gulp.src( source, { allowEmpty: true } )
.pipe( sourcemaps.init() )
.pipe(
sass(
{
indentType: 'tab',
indentWidth: 1,
errLogToConsole: true,
// outputStyle: 'compact',
// outputStyle: 'compressed',
// outputStyle: 'nested'.
outputStyle: 'compact',
precision: 10,
}
)
)
// eslint-disable-next-line no-console
.on( 'error', console.error.bind( console ) )
.pipe( sourcemaps.write( { includeContent: false } ) )
.pipe( sourcemaps.init( { loadMaps: true } ) )
.pipe( autoprefixer( AUTOPREFIXER_BROWSERS ) )
.pipe( sourcemaps.write( './' ) )
.pipe( lineec() ) // Consistent Line Endings for non UNIX systems.
.pipe( gulp.dest( dest ) )
.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.css if that is enqueued.
if ( addMin ) {
process = process.pipe( rename( { suffix: '.min' } ) ).pipe(
minifycss(
{
maxLineLen: 0,
}
)
)
.pipe( lineec() ) // Consistent Line Endings for non UNIX systems.
.pipe( gulp.dest( dest ) )
.pipe( filter( '**/*.css' ) ) // Filtering stream to only css files.
.pipe( browserSync.stream() ); // Reloads style.min.css if that is enqueued.
}
return process;
}
/**
* Task: `styles`.
*
* 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
*
* @param {Array} styleArray Style array.
*/
function styles( styleArray ) {
'use strict';
// Core styles.
var core = styleArray.map(
function( file ) {
return processScss( file.path, file.dest, false );
}
);
return merge( core );
}
function combineJS( source, dest, finalFile, minOnly ) {
'use strict';
var stream = gulp.src( source );
stream = stream
.pipe( concat( finalFile + '.js' ) )
.pipe( lineec() ); // Consistent Line Endings for non UNIX systems.
if ( false === minOnly ) {
stream = stream
.pipe( gulp.dest( dest ) );
}
// eslint-disable-next-line no-unused-vars
stream = stream
.pipe(
rename(
{
basename: finalFile,
suffix: '.min',
}
)
)
.pipe( uglify() )
.pipe( lineec() )
.pipe( gulp.dest( dest ) );
}
/**
* Task: `images`.
*
* Minifies PNG, JPEG, GIF and SVG images.
*
* This task does the following:
* 1. Gets the source of images raw folder
* 2. Minifies PNG, JPEG, GIF and SVG images
* 3. Generates and saves the optimized images
*
* This task will run only once, if you want to run it
* again, do it with the command `gulp images`.
*
* @param {string} imagesSrc
* @param {string}imagesDestination
*/
function images( imagesSrc, imagesDestination ) {
'use strict';
gulp.src( imagesSrc )
.pipe(
imagemin(
{
progressive: true,
optimizationLevel: 3, // 0-7 low-high
interlaced: true,
svgoPlugins: [ { removeViewBox: false } ],
}
)
)
.pipe( gulp.dest( imagesDestination ) );
}
/**
* WP POT Translation File Generator.
*
* This task does the following:
* 1. Gets the source of all the PHP files
* 2. Sort files in stream by path or any custom sort comparator
* 3. Applies wpPot with the variable set at the top of this file
* 4. Generate a .pot file of i18n that can be used for l10n to build .mo file
*
* @param {string} domain
* @param {string} srcFile
* @param {string} destFile
* @param {string} packageName
* @param {string} translatePath
*/
function translate( domain, srcFile, destFile, packageName, translatePath ) {
'use strict';
return gulp.src( srcFile )
.pipe( sort() )
.pipe(
wpPot(
{
domain: domain,
destFile: destFile,
package: packageName,
bugReport: bugReport,
lastTranslator: lastTranslator,
team: team,
}
)
)
.pipe( gulp.dest( translatePath + '/' + destFile ) );
}
/**
* Core function
*
* @param {Function} done
*/
function crayonMinJS( done ) {
'use strict';
combineJS(
[
'./js/src/util.js',
'./js/src/jquery.popup.js',
'./js/src/urvanov_syntax_highlighter.js',
],
'./js/min/',
'urvanov_syntax_highlighter',
true
);
combineJS(
[
'./js/src/util.js',
'./js/src/jquery.popup.js',
'./js/src/urvanov_syntax_highlighter.js',
'./util/tag-editor/urvanov_syntax_highlighter_qt.js',
'./util/tag-editor/urvanov_syntax_highlighter_tag_editor.js',
'./util/tag-editor/colorbox/jquery.colorbox-min.js',
],
'./js/min/',
'urvanov_syntax_highlighter.te',
true
);
done();
}
function crayonStyles() {
'use strict';
var cssFiles;
var arr = [
{ path: './css/src/admin_style.scss', dest: './css/src/' },
{ path: './css/src/global_style.scss', dest: './css/src/' },
{ path: './css/src/urvanov_syntax_highlighter_style.scss', dest: './css/src/' },
];
styles( arr );
cssFiles = gulp.src(
[ './css/src/admin_style.scss', './css/src/global_style.scss', './css/src/urvanov_syntax_highlighter_style.scss' ],
{ allowEmpty: true }
)
.pipe( sassPackager( {} ) )
.pipe( concat( 'urvanov_syntax_highlighter.min.scss' ) )
.pipe(
sass(
{
errLogToConsole: true,
outputStyle: 'compressed',
precision: 10,
}
)
)
// eslint-disable-next-line no-console
.on( 'error', console.error.bind( console ) )
.pipe( sourcemaps.write( { includeContent: false } ) )
.pipe( sourcemaps.init( { loadMaps: true } ) )
.pipe( autoprefixer( AUTOPREFIXER_BROWSERS ) )
.pipe( sourcemaps.write( './' ) )
.pipe( lineec() ) // Consistent Line Endings for non UNIX systems.
.pipe( gulp.dest( './css/min/' ) );
return merge( cssFiles );
}
function crayonImages( done ) {
'use strict';
images( './css/images/**/*.{png,jpg,gif,svg}', './screenshots/' );
images( './screenshots/*.{png,jpg,gif,svg}', './screenshots/' );
images( './util/theme-editor/images/*.{png,jpg,gif,svg}', './util/theme-editor/images/' );
done();
}
function crayonTranslate( done ) {
'use strict';
translate( 'urvanov-syntax-highlighter', './**/*.php', 'urvanov-syntax-highlighter.pot', 'urvanov-syntax-highlighter', './trans' );
done();
}
function crayonReplace( done ) {
'use strict';
// getCurrentFilenames();
// Copying the file to a the same name.
fs.copyFileSync(
'class-urvanov-syntax-highlighter-global.php',
'class-urvanov-syntax-highlighter-global-tmp.php',
fs.constants.COPYFILE_FICLONE
);
fs.unlinkSync( './class-urvanov-syntax-highlighter-global.php' );
fs.copyFileSync(
'class-urvanov-syntax-highlighter-plugin.php',
'class-urvanov-syntax-highlighter-plugin-tmp.php',
fs.constants.COPYFILE_FICLONE
);
fs.unlinkSync( './class-urvanov-syntax-highlighter-plugin.php' );
gulp.src( [ './class-urvanov-syntax-highlighter-global-tmp.php' ] )
// See http://mdn.io/string.replace#Specifying_a_string_as_a_parameter
.pipe( replace( /(?<=urvanov_syntax_highlighter_version = ').*?(?=';)/g, pluginVersion ) ) // jshint ignore:line
.pipe( replace( /(?<=urvanov_syntax_highlighter_date = ').*?(?=';)/g, pluginDate ) ) // jshint ignore:line
.pipe( rename( 'class-urvanov-syntax-highlighter-global.php' ) )
.pipe( gulp.dest( './' ) );
gulp.src( [ './class-urvanov-syntax-highlighter-global-tmp.php' ] )
.pipe( gulp.dest( './' ) )
.pipe( clean( { force: true } ) );
gulp.src( [ './class-urvanov-syntax-highlighter-plugin-tmp.php' ] )
.pipe( replace( /(?<=Version: ).*?(?=\r?\n)/g, pluginVersion ) ) // jshint ignore:line
.pipe( replace( /(?<='Version' => ').*?(?=',)/g, pluginVersion ) ) // jshint ignore:line
.pipe( replace( /(?<='Date' => ').*?(?=',)/g, pluginDate ) ) // jshint ignore:line
.pipe( rename( 'class-urvanov-syntax-highlighter-plugin.php' ) )
.pipe( gulp.dest( './' ) );
gulp.src( [ './class-urvanov-syntax-highlighter-plugin-tmp.php' ] )
.pipe( gulp.dest( './' ) )
.pipe( clean( { force: true } ) );
done();
}
/**
* Tasks
*/
gulp.task( 'js', crayonMinJS );
gulp.task( 'css', crayonStyles );
gulp.task( 'images', crayonImages );
gulp.task( 'translate', crayonTranslate );
gulp.task( 'replace', crayonReplace );
gulp.task( 'crayon', gulp.series( crayonMinJS, crayonStyles, crayonImages, crayonTranslate ) );
/**
* Watch Tasks.
*
* Watches for file changes and runs specific tasks.
*/
gulp.task(
'default',
gulp.series(
'crayon'
)
);