-
Notifications
You must be signed in to change notification settings - Fork 0
/
Gulpfile.js
182 lines (162 loc) · 4.33 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
// Require our dependencies
const babel = require( 'gulp-babel' );
const browserSync = require( 'browser-sync' );
const cheerio = require( 'gulp-cheerio' );
const concat = require( 'gulp-concat' );
const del = require( 'del' );
const eslint = require( 'gulp-eslint' );
const fs = require( 'fs' );
const gulp = require( 'gulp' );
const gutil = require( 'gulp-util' );
const notify = require( 'gulp-notify' );
const plumber = require( 'gulp-plumber' );
const reload = browserSync.reload;
const rename = require( 'gulp-rename' );
const sort = require( 'gulp-sort' );
const sourcemaps = require( 'gulp-sourcemaps' );
const uglify = require( 'gulp-uglify' );
const wpPot = require( 'gulp-wp-pot' );
// Set assets paths.
const paths = {
'php': [ './*.php', './**/*.php' ],
'concat_scripts': 'assets/scripts/concat/*.js',
'scripts': [ 'assets/scripts/*.js', '!assets/scripts/*.min.js' ],
};
/**
* Handle errors and alert the user.
*/
function handleErrors() {
const args = Array.prototype.slice.call( arguments );
notify.onError( {
'title': 'Task Failed [<%= error.message %>',
'message': 'See console.',
'sound': 'Sosumi' // See: https://github.com/mikaelbr/node-notifier#all-notification-options-with-their-defaults
} ).apply( this, args );
gutil.beep(); // Beep 'sosumi' again.
// Prevent the 'watch' task from stopping.
this.emit( 'end' );
}
/**
* Concatenate and transform JavaScript.
*
* https://www.npmjs.com/package/gulp-concat
* https://github.com/babel/gulp-babel
* https://www.npmjs.com/package/gulp-sourcemaps
*/
gulp.task( 'concat', () =>
gulp.src( paths.concat_scripts )
// Deal with errors.
.pipe( plumber(
{'errorHandler': handleErrors}
) )
// Start a sourcemap.
.pipe( sourcemaps.init() )
// Convert ES6+ to ES2015.
.pipe( babel( {
'presets': [
[ 'env', {
'targets': {
'browsers': [ 'last 2 versions' ]
}
} ]
]
} ) )
// Concatenate partials into a single script.
.pipe( concat( 'project.js' ) )
// Append the sourcemap to project.js.
.pipe( sourcemaps.write() )
// Save project.js
.pipe( gulp.dest( 'assets/scripts' ) )
.pipe( browserSync.stream() )
);
/**
* Minify compiled JavaScript.
*
* https://www.npmjs.com/package/gulp-uglify
*/
gulp.task( 'uglify', [ 'concat' ], () =>
gulp.src( paths.scripts )
.pipe( plumber( {'errorHandler': handleErrors} ) )
.pipe( rename( {'suffix': '.min'} ) )
.pipe( babel( {
'presets': [
[ 'env', {
'targets': {
'browsers': [ 'last 2 versions' ]
}
} ]
]
} ) )
.pipe( uglify( {
'mangle': false
} ) )
.pipe( gulp.dest( 'assets/scripts' ) )
);
/**
* Delete the theme's .pot before we create a new one.
*/
gulp.task( 'clean:pot', () =>
del( [ 'languages/bbp-dash.pot' ] )
);
/**
* Scan the theme and create a POT file.
*
* https://www.npmjs.com/package/gulp-wp-pot
*/
gulp.task( 'wp-pot', [ 'clean:pot' ], () =>
gulp.src( paths.php )
.pipe( plumber( {'errorHandler': handleErrors} ) )
.pipe( sort() )
.pipe( wpPot( {
'domain': 'bbp-dash',
'package': 'bbp-dash'
} ) )
.pipe( gulp.dest( 'languages/bbp-dash.pot' ) )
);
/**
* JavaScript linting.
*
* https://www.npmjs.com/package/gulp-eslint
*/
gulp.task( 'js:lint', () =>
gulp.src( [
'assets/scripts/concat/*.js',
'assets/scripts/*.js',
'!assets/scripts/project.js',
'!assets/scripts/*.min.js',
'!Gruntfile.js',
'!Gulpfile.js',
'!node_modules/**'
] )
.pipe( eslint() )
.pipe( eslint.format() )
.pipe( eslint.failAfterError() )
);
/**
* Process tasks and reload browsers on file changes.
*
* https://www.npmjs.com/package/browser-sync
*/
gulp.task( 'watch', function() {
// Kick off BrowserSync.
browserSync( {
'open': false, // Open project in a new tab?
'injectChanges': true, // Auto inject changes instead of full reload.
'proxy': 'bbp-dash.dev', // Use http://_s.dev:3000 to use BrowserSync.
'watchOptions': {
'debounceDelay': 1000 // Wait 1 second before injecting.
}
} );
// Run tasks when files change.
gulp.watch( paths.scripts, [ 'scripts' ] );
gulp.watch( paths.concat_scripts, [ 'scripts' ] );
gulp.watch( paths.php, [ 'markup' ] );
} );
/**
* Create individual tasks.
*/
gulp.task( 'markup', browserSync.reload );
gulp.task( 'i18n', [ 'wp-pot' ] );
gulp.task( 'scripts', [ 'uglify' ] );
gulp.task( 'lint', [ 'js:lint' ] );
gulp.task( 'default', [ 'i18n', 'scripts', ] );