-
Notifications
You must be signed in to change notification settings - Fork 11
/
gulpfile.js
177 lines (156 loc) · 6.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
// Props to http://mikevalstar.com/post/fast-gulp-browserify-babelify-watchify-react-build/ for most of this file.
'use strict';
var gulp = require( 'gulp' ); // Base gulp package.
var babelify = require( 'babelify' ); // Used to convert ES6 & JSX to ES5.
var browserify = require( 'browserify' ); // Providers "require" support, CommonJS.
var notify = require( 'gulp-notify' ); // Provides notification to both the console and Growel.
var rename = require( 'gulp-rename' ); // Rename sources.
var sourcemaps = require( 'gulp-sourcemaps' ); // Provide external sourcemap files.
var livereload = require( 'gulp-livereload' ); // Livereload support for the browser.
var gutil = require( 'gulp-util' ); // Provides gulp utilities, including logging and beep.
var chalk = require( 'chalk' ); // Allows for coloring for logging.
var source = require( 'vinyl-source-stream' ); // Vinyl stream support.
var buffer = require( 'vinyl-buffer' ); // Vinyl stream support.
var watchify = require( 'watchify' ); // Watchify for source changes.
var merge = require( 'utils-merge' ); // Object merge tool.
var duration = require( 'gulp-duration' ); // Time aspects of your gulp process.
var uglify = require( 'gulp-uglify' ); // Minify the JS.
var phpcs = require( 'gulp-phpcs' ); // Verify the PHP Coding Standards
var phplint = require( 'phplint' ).lint; // Lint PHP.
var shell = require( 'gulp-shell' ); // Run shell commands.
var runSequence = require( 'run-sequence' ); // Run tasks in series.
var wpPot = require( 'gulp-wp-pot' ); // Run our localization setup.
var sort = require( 'gulp-sort' ); // Run the sorting function used in the localization.
var eslint = require( 'gulp-eslint' );
// Configuration for Gulp.
var config = {
js: {
src: 'assets/src/js/batch.jsx',
watch: 'assets/src/js/**/*',
outputDir: 'assets/dist/',
outputFile: 'batch.min.js'
}
};
/**
* Task to handle the JavaScript building and sourcemap generating.
*/
gulp.task( 'build', function() {
var args = { debug: true };
var bundler = browserify( config.js.src, args )
.transform( babelify, {
presets: ['es2015', 'react']
} );
bundle( bundler );
} );
/**
* Task to handle linting Javascript
*/
gulp.task( 'eslint', function() {
return gulp.src([ 'assets/src/**/*.js', 'assets/src/**/*.jsx' ])
.pipe( eslint() )
.pipe( eslint.format() )
.pipe( eslint.failAfterError() )
});
/**
* Task to handle the file creation for localization.
*/
gulp.task( 'localize', function() {
return gulp.src([ '*.php', 'includes/**/*.php', 'templates/**/*.php' ])
.pipe( sort() )
.pipe( wpPot( {
domain: 'locomotive',
destFile:'locomotive.pot',
package: 'locomotive',
bugReport: 'https://github.com/reaktivstudios/locomotive/issues',
lastTranslator: 'Andrew Norcross <[email protected]>',
team: 'Reaktiv Studios <[email protected]>'
} ))
.pipe( gulp.dest( 'languages' ) );
});
/**
* Task to handle the JavaScript building and sourcemap generating.
*/
gulp.task( 'watch', function() {
var args = merge( watchify.args, { debug: true } ); // Merge in default watchify args with browserify arguments.
var bundler = browserify( config.js.src, args ) // Browserify.
.plugin( watchify, {
ignoreWatch: ['**/node_modules/**', '**/bower_components/**']
} ) // Watchify to watch source file changes.
.transform( babelify, {
presets: ['es2015', 'react']
} );
livereload.listen(); // Start livereload server.
bundle( bundler ); // Run the bundle the first time (required for Watchify to kick in).
bundler.on( 'update', function() {
bundle( bundler ); // Re-run bundle on source updates.
});
} );
// Completes the final file outputs.
function bundle( bundler ) {
var bundleTimer = duration( 'Javascript bundle time' );
return bundler
.bundle()
.on( 'error', function( err ) {
console.error( err ); this.emit( 'end' );
})
.pipe( source( 'batch.jsx' ) ) // Set source name.
.pipe( buffer() ) // Convert to gulp pipeline.
.pipe( rename( config.js.outputFile ) ) // Rename the output file.
.pipe( sourcemaps.init( { loadMaps: true } ) ) // Extract the inline sourcemaps.
.pipe( uglify() ) // Minify the JS.
.pipe( sourcemaps.write( './map' ) ) // Set folder for sourcemaps to output to.
.pipe( gulp.dest( config.js.outputDir ) ) // Set the output folder.
.pipe( notify( {
message: 'Generated file: <%= file.relative %>'
} ) ) // Output the file being created.
.pipe( bundleTimer ) // Output time timing of the file creation.
.pipe( livereload() ); // Reload the view in the browser.
}
// Verify PHP Coding Standards.
gulp.task( 'phpcs', function() {
return gulp.src( [
'**/*.php',
'!node_modules/**/*.*',
'!tests/**/*.*',
'!vendor/**/*.*'
] )
.pipe( phpcs( {
bin: 'vendor/bin/phpcs',
standard: 'ruleset.xml'
} ) )
.pipe( phpcs.reporter( 'log' ) )
.pipe( phpcs.reporter( 'fail' ) );
} );
// Lint PHP.
gulp.task( 'phplint', function( cb ) {
phplint( [
'**/*.php',
'!node_modules/**/*.*',
'!tests/**/*.*',
'!vendor/**/*.*'
], { limit: 10 }, function( err, stdout, stderr ) {
if ( err ) {
cb( err );
process.exit( 1 );
}
cb();
} );
} );
// Run single site PHPUnit tests.
gulp.task( 'phpunit-single', shell.task( [ 'vendor/bin/phpunit -c phpunit.xml.dist' ] ) );
// Run multisite PHPUnit tests.
gulp.task( 'phpunit-multisite', shell.task( [ 'vendor/bin/phpunit -c multisite.xml.dist' ] ) );
// Run single site PHPUnit tests with code coverage.
gulp.task( 'phpunit-codecoverage', shell.task( [ 'vendor/bin/phpunit -c codecoverage.xml.dist' ] ) );
// Gulp task for build
gulp.task( 'default', [ 'eslint', 'build' ] );
// Lint files.
gulp.task( 'lint', [ 'phplint', 'phpcs', 'eslint' ] );
// Run PHP tests.
gulp.task( 'phpunit', function() {
runSequence( 'phpunit-single', 'phpunit-multisite' );
} );
// Run full build and test.
gulp.task( 'test', function() {
runSequence( 'build', 'lint', 'phpunit' );
} );