-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.js
137 lines (120 loc) · 3.16 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
const gulp = require('gulp');
const del = require('del');
const rename = require('gulp-rename');
const browsersync = require('browser-sync');
const zip = require('gulp-vinyl-zip').zip;
const sass = require('gulp-sass');
const wpPot = require('gulp-wp-pot');
const package = require('./package.json');
sass.compiler = require('node-sass');
const pluginName = 'nh3-mag-blocks';
const zipPath = [
'*.php',
'build/**/*',
'classes/**/*',
'languages/nh3-mag-blocks.pot',
'templates/**/*'
];
const potFiles = ['classes/**/*.php', 'templates/**/*.php', '*.php'];
/**
* 'zip' task function.
*
* Create a zip file that contains the plugin files in a directory named after the plugin.
*/
function makeZip() {
return gulp
.src(zipPath, { base: '.' })
.pipe(rename(file => (file.dirname = `${pluginName}/${file.dirname}`)))
.pipe(zip(`${pluginName}.zip`))
.pipe(gulp.dest('.'));
}
/**
* Currently only copy the css from the source forlder to the build folder.
*/
function buildCss() {
return gulp
.src('./src/css/editor.scss')
.pipe(sass().on('error', sass.logError))
.pipe(gulp.dest('./build/css/'));
}
/**
* Currently only copy the css from the source forlder to the build folder.
*/
function buildCssGeneral() {
return gulp
.src('./src/css/style.scss')
.pipe(sass().on('error', sass.logError))
.pipe(gulp.dest('./build/css/'));
}
/**
* Build the POT file by parsing the php source files
*/
function buildPot() {
return gulp
.src(potFiles)
.pipe(
wpPot({
domain: package.wp.textDomain,
package: package.wp.pluginName
})
)
.pipe(gulp.dest(`./languages/${package.wp.textDomain}.pot`));
}
/**
* Watch for changes on the css source folder to trigger the copyCss function.
*/
function watchCss() {
return gulp.watch('src/css/**/*.scss', buildCss);
}
function watchCssGeneral() {
return gulp.watch('src/css/**/*.scss', buildCssGeneral);
}
/**
* Watches for changes on the build folder to trigger a browser reload.
*/
function watchBuild() {
return gulp.watch('build/**/*', reloadBrowser);
}
/**
* Watches for changes on the pot source files to trigger the pot build
*/
function watchPot() {
return gulp.watch(potFiles, buildPot);
}
/**
* Deletes all build related files
*/
function cleanBuild() {
return del(['build/**', 'languages/**', `${pluginName}.zip`]);
}
/**
* Promise wrapper around the BrowserSync reload() method.
*/
function reloadBrowser() {
return Promise.resolve(browsersync.reload());
}
/**
* 'sync' task function.
*
* Launch a BrowserSync instance, proying the actual localhost backend.
*/
function syncBrowser() {
return browsersync.init({
proxy: 'http://localhost/nh3-wp'
});
}
// Groups all watch functions
const watch = gulp.parallel(watchCss, watchCssGeneral, watchBuild, watchPot);
// Groups all build functions
const build = gulp.parallel(buildCss, buildCssGeneral, buildPot);
// Starts a browser sync instance and watches for changes.
const defaultTask = gulp.parallel(syncBrowser, build, watch);
// Exports tasks to CLI
module.exports = {
default: defaultTask,
build: build,
sync: syncBrowser,
watch: watch,
zip: makeZip,
'clean:build': cleanBuild
};