-
Notifications
You must be signed in to change notification settings - Fork 14
/
gulpfile.js
291 lines (247 loc) · 9.92 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
/**
* Gulpfile heavily adapted from {@link https://github.com/ahmadawais/WPGulp WPGulp}
* Implements:
* 1. CSS: Sass to CSS conversion, error catching, autoprefixing, sourcemaps,
* CSS minification, and merge media queries.
* 2. JS: Concatenates & uglifies JS files.
* 3. Watches files for changes in CSS or JS.
* 4. Corrects the line endings {@link https://www.npmjs.com/package/gulp-line-ending-corrector}.
*
* @author Daniel M. Hendricks (@danielhendricks)
* @since 0.3.0
*/
var pkg = require('./package.json');
/**
* Configuration
*
* In paths you can add <<glob or array of globs>>. Edit the variables as per your project requirements.
*/
var project = pkg.name; // Project slug
var cssOutputStyle = 'expanded'; // Values: compact, compressed, nested, expanded
var cssOutputComments = false; // Output SASS source/line numbers in compiled CSS files
var styleSourcePath = './src/scss/'; // Path to source SASS files
var jsSourcePath = './src/js/'; // Path to source JavaScript files
var styleDestination = './assets/css/'; // Path to place the compiled CSS file
var jsDestination = './assets/js/'; // Path to place the compiled CSS file
var styleMapPath = './'; // Path to place the map files
var distZipFile = project + '.zip'; // The destination file for the ZIP task
/* Define the main CSS files to watch */
var styleTasks = [
{
name: 'plugin',
suffix: false, // Can be a string value, false for none, else uses 'name' value
source: 'plugin.scss' // The filename located in the styleSourcePath directory
},
{
name: 'admin',
source: 'admin.scss'
}
];
/* Define the JavaScript files to watch */
var jsTasks = [
{
name: 'frontend',
suffix: false,
//include: [ jsSourcePath + 'common/**/*.js' ],
//source: jsSourcePath + 'frontend', // Optional subdirectory to search for *.js. Defaults to 'name' value
//dest: jsDestination // Optionally, specfy alternate destination
},
{
name: 'admin',
//include: [ jsSourcePath + 'common/**/*.js' ],
}
];
/* Define strings to replace using 'gulp rename', defined in the config section of package.json */
var renameStrings = [
[ 'dmhendricks\/wordpress-base-plugin', pkg.config.username + '/' + pkg.name ], // Git/Composer identifier
[ 'VendorName\\PluginName', pkg.config.php_namespace ], // PHP namespace for your plugin
[ 'VendorName\\\\PluginName', pkg.config.php_namespace.replace( /\\/g, '\\\\' ) ], // Rename Composer namespace
[ 'wordpress-base-plugin', pkg.name ], // Plugin slug
[ 'wordpress_base_plugin', pkg.name.replace( /-/g, '_' ) ], // Plugin underscored slug
[ 'WPBP', pkg.config.prefix.toUpperCase() ], // Unique JavaScript object for your plugin
[ 'wpbp', pkg.config.prefix ], // Replace remaining plugin prefixes
[ 'WordPress Base Plugin', pkg.config.plugin_name ], // Replace plugin long name
[ 'My Plugin', pkg.config.plugin_short_name ] // Replace plugin short name
];
/**
* Browsers for which you want to enable autoprefixing.
*
* @see https://github.com/ai/browserslist
*/
const AUTOPREFIXER_BROWSERS = [
'last 2 version',
'> 1%',
'ie >= 9',
'ie_mob >= 10',
'ff >= 30',
'chrome >= 34',
'safari >= 7',
'opera >= 23',
'ios >= 7',
'android >= 4',
'bb >= 10'
];
/**
* Load gulp plugins and pass them semantic names.
*/
var gulp = require('gulp');
var pkg = require('./package.json');
// 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.
// JavaScript-related plugins.
var concat = require('gulp-concat'); // Concatenates JS files
var uglify = require('gulp-uglify'); // Minifies JS files
// Utility related plugins.
var rename = require('gulp-rename'); // Renames files (ex: style.css -> style.min.css)
var replace = require('gulp-batch-replace'); // Replace strings inside files
var 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)
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 (E.g. structure.scss, which was later combined with other css files to generate style.css)
var notify = require('gulp-notify'); // Displays notification message
var batchRename = require('gulp-simple-rename'); // Rename files with wildcard
var vinylPaths = require('vinyl-paths'); // Return each path in a stream
var del = require('del'); // Delete files that are renamed
var plumber = require( 'gulp-plumber' ); // Prevent pipe breaking caused by errors from gulp plugins.
/**
* Custom Error Handler.
*
* @param Mixed err
*/
const errorHandler = r => {
notify.onError( '\n\n ===> ERROR: <%= error.message %>\n' )( r );
beep();
};
/* Arrays to hold created task info */
var tasks_css = [];
var tasks_js = [];
/**
* Style tasks
*
* Compile SASS, autoprefixes and minifies.
*/
styleTasks.forEach( function( task ) {
var basename_suffix = ( typeof task.suffix === 'string' ? '-' + task.suffix : '' );
if( !( typeof task.suffix === 'boolean' && task.suffix === false ) ) basename_suffix = '-' + task.name;
tasks_css.push( task.name + 'CSS' );
gulp.task( task.name + 'CSS', (done) => {
gulp.src( styleSourcePath + task.source )
.pipe( plumber( errorHandler ) )
//.pipe( sourcemaps.init() )
.pipe( sass( {
sourceComments: cssOutputComments ? 'map' : null,
errLogToConsole: true,
outputStyle: cssOutputStyle,
precision: 10
} ) )
.on( 'error', console.error.bind( console ) )
//.pipe( sourcemaps.write( { includeContent: false } ) )
//.pipe( sourcemaps.init( { loadMaps: true } ) )
.pipe( autoprefixer( AUTOPREFIXER_BROWSERS ) )
//.pipe( sourcemaps.write ( styleMapPath ) )
.pipe( rename( {
basename: project + basename_suffix
}))
.pipe( lineec() ) // Consistent Line Endings for non UNIX systems.
.pipe( gulp.dest( styleDestination ) )
.pipe( filter( '**/*.css' ) ) // Filtering stream to only css files
.pipe( mmq( { log: true } ) ) // Merge Media Queries only for .min.css version.
.pipe( rename( {
basename: project + basename_suffix,
suffix: '.min'
}))
.pipe( minifycss() )
.pipe( lineec() ) // Consistent line endings for non-UNIX systems.
.pipe( gulp.dest( styleDestination ) )
.pipe( filter( '**/*.css' ) ) // Filtering stream to only css files
.pipe( notify( { message: 'TASK: "' + task.name + 'CSS" completed.', onLast: true } ) );
done();
});
});
/**
* Style tasks
*
* Concatenate, uglify and rename JavaScripts.
*/
jsTasks.forEach( function( task ) {
// Set base filename suffix (example '-admin'), if specified
var basename_suffix = ( typeof task.suffix === 'string' ? '-' + task.suffix : '' );
if( !( typeof task.suffix === 'boolean' && task.suffix === false ) ) basename_suffix = '-' + task.name;
// Set base filename suffix (example '-admin'), if specified
var jsSources = [ ( task.source ? task.source : jsSourcePath + task.name ) + '/*.js' ];
// Set JavaScript source paths
if( task.include ) {
task.include.forEach( function( item ) {
jsSources.push( item );
});
}
tasks_js.push( { id: task.name + 'JS', name: task.name, watch: jsSources } );
gulp.task( task.name + 'JS', (done) => {
gulp.src( jsSources )
.pipe( plumber( errorHandler ) )
.pipe( concat( task.source + '.js' ) )
.pipe( rename( {
basename: project + basename_suffix,
}))
.pipe( lineec() ) // Consistent Line Endings for non UNIX systems.
.pipe( gulp.dest( jsDestination ) )
.pipe( rename( {
basename: project + basename_suffix,
suffix: '.min'
}))
.pipe( uglify() )
.pipe( lineec() ) // Consistent Line Endings for non UNIX systems.
.pipe( gulp.dest( jsDestination ) )
.pipe( notify( { message: 'TASK: "' + task.name + 'JS" completed.', onLast: true } ) );
done();
});
});
/**
* Watches for file changes and runs specified tasks.
*/
//gulp.task( 'default', object_property_to_array( tasks_js, 'id', tasks_css ), function () {
// gulp.watch( styleSourcePath + '**/*.scss', tasks_css );
// tasks_js.forEach( function( task ) {
// gulp.watch( task.watch, [ task.id ] );
// });
//});
gulp.task(
'default',
gulp.series( gulp.parallel( object_property_to_array( tasks_js, 'id', tasks_css ) ), () => {
gulp.watch( styleSourcePath + '**/*.scss', gulp.parallel( tasks_css ) );
tasks_js.forEach( function( task ) {
gulp.watch( task.watch, gulp.series( task.id ) );
});
})
);
/**
* Task to rename files and variables
*/
gulp.task( 'rename', () => {
return gulp.src( [ './**/*.php', './*.json', './**/*.js', './**/*.scss', './*.txt', './*.md', '!./node_modules/**', '!./vendor/**', '!./.git/**', '!./languages/**', '!./*lock*', '!./gulpfile.js' ] )
.pipe( plumber( errorHandler ) )
.pipe( replace( renameStrings ) )
.pipe( vinylPaths( del ) )
.pipe( batchRename( function (path) {
return path.replace( /wordpress-base-plugin/, pkg.name );
} ) )
.pipe( gulp.dest( './' ) )
.pipe( notify( { message: 'TASK: "rename" completed.', onLast: true } ) );
});
/**
* Helper functions
*/
function object_property_to_array( obj, id, arr ) {
var result = [];
obj.forEach( function( item ) {
for( key in item ) {
if(item.hasOwnProperty(key)) {
if( key == id ) result.push( item[id] );
}
}
});
if( arr ) result = arr.concat( result );
return result;
}