-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Gulpfile.js
284 lines (267 loc) · 6.72 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
// Require our dependencies
const autoprefixer = require("autoprefixer");
const babel = require("gulp-babel");
const browserSync = require("browser-sync").create();
const concat = require("gulp-concat");
const cssnano = require("cssnano");
const eslint = require("gulp-eslint");
const fs = require("fs");
const gulp = require("gulp");
const iife = require('gulp-iife');
const packagejson = JSON.parse(fs.readFileSync("./package.json"));
const mqpacker = require("css-mqpacker");
const plumber = require("gulp-plumber");
const postcss = require("gulp-postcss");
const rename = require("gulp-rename");
const sass = require('gulp-sass')(require('sass'));
const sassGlob = require("gulp-sass-glob");
const sort = require("gulp-sort");
const gulpStylelint = require("@ronilaukkarinen/gulp-stylelint");
const sourcemaps = require("gulp-sourcemaps");
const uglify = require("gulp-uglify");
const wpPot = require("gulp-wp-pot");
// Some config data for our tasks
const config = {
styles: {
admin: "assets/sass/" + packagejson.name + "-admin.scss",
front_end: "assets/sass/" + packagejson.name + "-front-end.scss",
srcDir: "assets/sass/*.scss",
dest: "assets/css",
lint_dest: "assets/sass/"
},
scripts: {
admin: "./assets/js/src/admin/**/*.js",
admin_lint: "./assets/js/src/admin/",
front_end: "./assets/js/src/front-end/**/*.js",
front_end_lint: "./assets/js/src/front-end/",
uglify: ["assets/js/*.js", "!assets/js/*.min.js"],
dest: "./assets/js"
},
languages: {
src: [
"./**/*.php",
"!.git/*",
"!.svn/*",
"!bin/**/*",
"!node_modules/*",
"!release/**/*",
"!vendor/**/*"
],
dest: "./languages/" + packagejson.name + ".pot"
},
browserSync: {
active: false,
localURL: "mylocalsite.local"
}
};
function adminstyles() {
return gulp
.src(config.styles.admin, { allowEmpty: true })
.pipe(sourcemaps.init()) // Sourcemaps need to init before compilation
.pipe(sassGlob()) // Allow for globbed @import statements in SCSS
.pipe(sass()) // Compile
.on("error", sass.logError) // Error reporting
.pipe(
postcss([
mqpacker({
sort: true
}),
autoprefixer(),
cssnano({
safe: true // Use safe optimizations.
}) // Minify
])
)
.pipe(
rename({
// Rename to .min.css
suffix: ".min"
})
)
.pipe(sourcemaps.write()) // Write the sourcemap files
.pipe(gulp.dest(config.styles.dest)) // Drop the resulting CSS file in the specified dir
.pipe(browserSync.stream());
}
function adminsasslint() {
return gulp.src(config.styles.admin)
.pipe(gulpStylelint({
fix: true,
reporters: [
{formatter: 'string', console: true}
],
}))
.pipe(gulp.dest(config.styles.lint_dest));
}
function frontendsasslint() {
return gulp.src(config.styles.front_end)
.pipe(gulpStylelint({
fix: true,
reporters: [
{formatter: 'string', console: true}
]
}))
.pipe(gulp.dest(config.styles.lint_dest));
}
function frontendstyles() {
return gulp
.src(config.styles.front_end, { allowEmpty: true })
.pipe(sourcemaps.init()) // Sourcemaps need to init before compilation
.pipe(sassGlob()) // Allow for globbed @import statements in SCSS
.pipe(sass()) // Compile
.on("error", sass.logError) // Error reporting
.pipe(
postcss([
mqpacker({
sort: true
}),
autoprefixer(),
cssnano({
safe: true // Use safe optimizations.
}) // Minify
])
)
.pipe(
rename({
// Rename to .min.css
suffix: ".min"
})
)
.pipe(sourcemaps.write()) // Write the sourcemap files
.pipe(gulp.dest(config.styles.dest)) // Drop the resulting CSS file in the specified dir
.pipe(browserSync.stream());
}
function adminscripts() {
return gulp
.src(config.scripts.admin)
.pipe(sourcemaps.init())
.pipe(
babel({
presets: ["@babel/preset-env"]
})
)
.pipe(concat(packagejson.name + "-admin.js")) // Concatenate
.pipe(sourcemaps.write())
.pipe(eslint( {
parserOptions: {
requireConfigFile: false
}}
))
.pipe(iife({
useStrict: false,
params: ['$'],
args: ['jQuery']
}))
.pipe(gulp.dest(config.scripts.dest))
.pipe(browserSync.stream());
}
function frontendscripts() {
return gulp
.src(config.scripts.front_end)
.pipe(sourcemaps.init())
.pipe(
babel({
presets: ["@babel/preset-env"]
})
)
.pipe(concat(packagejson.name + "-front-end.js")) // Concatenate
.pipe(sourcemaps.write())
.pipe(eslint( {
parserOptions: {
requireConfigFile: false
}}
))
.pipe(gulp.dest(config.scripts.dest))
.pipe(browserSync.stream());
}
function adminscriptlint() {
return gulp
.src(config.scripts.admin)
.pipe(eslint( {
fix:true,
parserOptions: {
requireConfigFile: false
}
}
))
.pipe(eslint.format())
.pipe(gulp.dest(config.scripts.admin_lint))
// Brick on failure to be super strict
//.pipe(eslint.failOnError());
};
function frontendscriptlint() {
return gulp
.src(config.scripts.front_end)
.pipe(eslint( {
fix:true,
parserOptions: {
requireConfigFile: false
}
}
))
.pipe(eslint.format())
.pipe(gulp.dest(config.scripts.front_end_lint))
// Brick on failure to be super strict
//.pipe(eslint.failOnError());
};
function uglifyscripts() {
return (
gulp
.src(config.scripts.uglify)
.pipe(uglify()) // Minify + compress
.pipe(
rename({
suffix: ".min"
})
)
.pipe(sourcemaps.write())
.pipe(gulp.dest(config.scripts.dest))
.pipe(browserSync.stream())
);
}
// Generates translation file.
function translate() {
return gulp
.src(config.languages.src)
.pipe(
wpPot({
domain: packagejson.name,
package: packagejson.name
})
)
.pipe(gulp.dest(config.languages.dest));
}
// Injects changes into browser
function browserSyncTask() {
if (config.browserSync.active) {
browserSync.init({
proxy: config.browserSync.localURL
});
}
}
// Reloads browsers that are using browsersync
function browserSyncReload(done) {
browserSync.reload();
done();
}
// Watch directories, and run specific tasks on file changes
function watch() {
gulp.watch(config.styles.srcDir, styles);
gulp.watch(config.scripts.admin, adminscripts);
// Reload browsersync when PHP files change, if active
if (config.browserSync.active) {
gulp.watch("./**/*.php", browserSyncReload);
}
}
// define complex tasks
const lint = gulp.series(adminsasslint, frontendsasslint, adminscriptlint, frontendscriptlint);
const styles = gulp.series(adminstyles, frontendstyles);
const scripts = gulp.series(adminscripts, frontendscripts, uglifyscripts);
const build = gulp.series(lint, gulp.parallel(styles, scripts, translate));
// export tasks
exports.styles = styles;
exports.scripts = scripts;
exports.lint = lint;
exports.translate = translate;
exports.watch = watch;
exports.build = build;
exports.default = build;