forked from UCF/UCF-Section-Plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.js
132 lines (109 loc) · 2.81 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
const fs = require('fs');
const browserSync = require('browser-sync').create();
const gulp = require('gulp');
const include = require('gulp-include');
const eslint = require('gulp-eslint');
const isFixed = require('gulp-eslint-if-fixed');
const babel = require('gulp-babel');
const rename = require('gulp-rename');
const uglify = require('gulp-uglify');
const readme = require('gulp-readme-to-markdown');
const merge = require('merge');
let config = {
src: {
jsPath: './src/js'
},
dist: {
jsPath: './static/js'
},
packagesPath: './node_modules',
sync: false,
syncTarget: 'http://localhost/wordpress/'
};
/* eslint-disable no-sync */
if (fs.existsSync('./gulp-config.json')) {
const overrides = JSON.parse(fs.readFileSync('./gulp-config.json'));
config = merge(config, overrides);
}
/* eslint-enable no-sync */
//
// Helper functions
//
// Base JS linting function (with eslint). Fixes problems in-place.
function lintJS(src, dest) {
dest = dest || config.src.jsPath;
return gulp.src(src)
.pipe(eslint({
fix: true
}))
.pipe(eslint.format())
.pipe(isFixed(dest));
}
// Base JS compile function
function buildJS(src, dest) {
dest = dest || config.dist.jsPath;
return gulp.src(src)
.pipe(include({
includePaths: [config.packagesPath, config.src.jsPath]
}))
.on('error', console.log) // eslint-disable-line no-console
.pipe(babel())
.pipe(uglify())
.pipe(rename('ucf-section-admin.min.js'))
.pipe(gulp.dest(dest));
}
// BrowserSync reload function
function serverReload(done) {
if (config.sync) {
browserSync.reload();
}
done();
}
// BrowserSync serve function
function serverServe(done) {
if (config.sync) {
browserSync.init({
proxy: {
target: config.syncTarget
}
});
}
done();
}
//
// JavaScript
//
// Run eslint on js files in src.jsPath
gulp.task('es-lint-plugin', () => {
return lintJS([`${config.src.jsPath}/*.js`], config.src.jsPath);
});
// Concat and uglify js files through babel
gulp.task('js-build-plugin', () => {
return buildJS(`${config.src.jsPath}/admin.js`, config.dist.jsPath);
});
// All js-related tasks
gulp.task('js', gulp.series('es-lint-plugin', 'js-build-plugin'));
//
// Documentation
//
// Generates a README.md from README.txt
gulp.task('readme', () => {
return gulp.src('readme.txt')
.pipe(readme({
details: false,
screenshot_ext: [] // eslint-disable-line camelcase
}))
.pipe(gulp.dest('.'));
});
//
// Rerun tasks when files change
//
gulp.task('watch', (done) => {
serverServe(done);
gulp.watch(`${config.src.jsPath}/**/*.js`, gulp.series('js', serverReload));
gulp.watch('./**/*.php', gulp.series(serverReload));
});
//
// Default task
//
gulp.task('default', gulp.series('js', 'readme'));