-
Notifications
You must be signed in to change notification settings - Fork 1
/
gulpfile.babel.js
executable file
·276 lines (231 loc) · 7.46 KB
/
gulpfile.babel.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
'use strict';
import plugins from 'gulp-load-plugins';
import browser from 'browser-sync';
import yargs from 'yargs';
import gulp from 'gulp';
import rimraf from 'rimraf';
import yaml from 'js-yaml';
import fs from 'fs';
import webpackStream from 'webpack-stream';
import webpack2 from 'webpack';
import named from 'vinyl-named';
import path from 'path';
const packageFile = require('./package.json');
// Load all Gulp plugins into one variable
const $ = plugins();
// Check for --production flag
const PRODUCTION = !!(yargs.argv.production);
// Check for --noserver flag
const SERVER = !(yargs.argv.noserver);
// Load settings from settings.yml
const {COMPATIBILITY, PATHS} = loadConfig();
function loadConfig() {
let ymlFile = fs.readFileSync('config.yml', 'utf8');
return yaml.load(ymlFile);
}
// Enter URL of your local server here
// Example: 'http://localhost:8888'
var URL = 'http://src.wordpress-develop.dev';
// Build the "dist" folder by running all of the below tasks
gulp.task('build',
PRODUCTION ?
gulp.series(clean, version, sass, sassMin, rtl, javascript, javascriptMin, buildSelect2, copy, copyVendorLicenses, packageFiles) :
gulp.series(clean, sass, sassMin, rtl, javascript, javascriptMin, buildSelect2, copy, copyVendorLicenses));
// Build the site and watch for file changes
gulp.task('default', gulp.series('build', watch) );
// Delete the "dist" folder
// This happens every time a build starts
function clean(done) {
rimraf(PATHS.dist, done);
}
// Copy files out of the assets folder
// This task skips over the "img", "js", and "scss" folders, which are parsed separately
function copy() {
return gulp.src(PATHS.assets)
.pipe(gulp.dest(PATHS.dist + '/assets'));
}
// Compile Sass into CSS
// In production, the CSS is compressed
function sass() {
return gulp.src( PATHS.entries.scss, { allowEmpty: true } )
.pipe($.sourcemaps.init())
.pipe($.sass({
includePaths: PATHS.sass
})
.on('error', $.sass.logError))
.pipe($.autoprefixer({
browsers: COMPATIBILITY
}))
.pipe($.if(!PRODUCTION, $.sourcemaps.write( '.' )))
.pipe(gulp.dest(PATHS.dist + '/css'))
.pipe(browser.reload({stream: true}));
}
function sassMin() {
return gulp.src( PATHS.entries.scss, { allowEmpty: true } )
.pipe($.sourcemaps.init())
.pipe($.sass({
includePaths: PATHS.sass,
outputStyle: 'compressed'
}).on('error', $.sass.logError))
.pipe($.rename(function (file) {
if ( file.extname.indexOf( '.min' ) < 0 ) {
file.extname = '.min' + file.extname;
}
}))
.pipe($.if(!PRODUCTION, $.sourcemaps.write( '.' )))
.pipe(gulp.dest(PATHS.dist + '/css'))
.pipe(browser.reload({stream: true}));
}
function rtl() {
return gulp.src( [
PATHS.dist + '/css/**/*.css',
'!' + PATHS.dist + '/css/**/*-rtl.css',
'!' + PATHS.dist + '/css/**/rbm-fh-select2*.css',
], { allowEmpty: true } )
.pipe( named() )
.pipe( $.rename( function( file ) {
if ( file.basename.indexOf( '.min' ) >= 0 ) {
file.basename = file.basename.replace( '.min', '.min-rtl' );
}
else {
file.basename = file.basename + '-rtl';
}
return file;
} ) )
.pipe(
$.rtlcss()
)
.pipe(
$.replace( /\/\*# sourceMappingURL.*$/, '' )
)
.pipe(
gulp.dest( PATHS.dist + '/css' )
);
}
let webpackConfig = {
module: {
rules: [
{
test: /.js$/,
use: [
{
loader: 'babel-loader'
}
]
}
]
},
externals: {
'jquery': 'jQuery',
'jquery-ui': 'jQuery.ui',
},
}
if ( ! PRODUCTION ) {
webpackConfig.devtool = 'inline-source-map';
}
// Combine JavaScript into one file
// In production, the file is minified
function javascript() {
return gulp.src(PATHS.entries.js)
.pipe(named())
.pipe(webpackStream(webpackConfig, webpack2))
.pipe($.sourcemaps.init( { loadMaps: true }))
.pipe($.if(!PRODUCTION, $.sourcemaps.write('.' )))
.pipe(gulp.dest(PATHS.dist + '/js'));
}
function javascriptMin() {
return gulp.src(PATHS.entries.js)
.pipe(named())
.pipe(webpackStream(webpackConfig, webpack2))
.pipe($.sourcemaps.init( { loadMaps: true }))
.pipe($.uglify()
.on('error', e => {
console.log(e);
})
)
.pipe($.rename(function (file) {
if ( file.extname.indexOf( '.min' ) < 0 ) {
file.extname = '.min' + file.extname;
}
}))
.pipe($.if(!PRODUCTION, $.sourcemaps.write('.' )))
.pipe(gulp.dest(PATHS.dist + '/js'));
}
function buildSelect2( done ) {
// Remove the Factory to replace with our own
let select2file = fs.readFileSync( __dirname + '/node_modules/select2/dist/js/select2.full.js', 'utf8' );
select2file = select2file.replace( /\(function \(factory\) {[\s\S]*} \(function \(jQuery\) {/m, '(function (jQuery) {' );
// Grab the Select2 Code
let select2code = select2file.match( /\(function \(jQuery\) {[\s\S]*}\)\);/m )[0];
// Append to our own Factory
let rbmselect2code = fs.readFileSync( __dirname + '/assets/src/js/admin/rbm-fh-select2.js', 'utf8' );
fs.writeFileSync( __dirname + '/assets/dist/js/rbm-fh-select2.js', rbmselect2code + ' ' + select2code, 'utf8' );
return gulp.src( 'assets/dist/js/rbm-fh-select2.js', { allowEmpty: true } )
.pipe(named())
.pipe($.uglify()
.on('error', e => {
console.log(e);
})
)
.pipe($.rename(function (file) {
console.log( file );
if ( file.extname.indexOf( '.min' ) < 0 ) {
file.extname = '.min' + file.extname;
}
}))
.pipe(gulp.dest(PATHS.dist + '/js'));
}
function copyVendorLicenses() {
return gulp.src(PATHS.licenses, { allowEmpty: true, base: '*' })
.pipe($.rename(function(file) {
file.dirname = file.dirname.replace( '../node_modules/', '' );
return file;
}))
.pipe(gulp.dest(PATHS.dist + '/licenses/'));
}
// Start BrowserSync to preview the site in
function server(done) {
browser.init({
proxy: URL,
ui: {
port: 8080
},
});
done();
}
// Reload the browser with BrowserSync
function reload(done) {
browser.reload();
done();
}
gulp.task( 'version', version );
function version() {
return gulp.src([
'assets/src/**/*',
'vendor/**/*',
'core/**/*',
'rbm-field-helpers.php'
], {base: './'})
// Doc block versions
.pipe($.replace(/\{\{VERSION}}/g, packageFile.version))
// Version constant
.pipe($.replace(/(define\( 'RBM_FIELD_HELPERS_VER', ')\d+\.\d+\.\d+/, "$1" + packageFile.version))
.pipe(gulp.dest('./'));
}
function packageFiles() {
return gulp.src([
'assets/dist/**/*',
'!assets/dist/**/*.map',
'vendor/**/*',
'core/**/*',
'rbm-field-helpers.php'
], {base: './'})
.pipe($.zip(`rbm-field-helpers-${packageFile.version}.zip`))
.pipe(gulp.dest('./'));
}
// Watch for changes to Sass, and JavaScript
function watch() {
gulp.watch(PATHS.assets, copy);
gulp.watch('assets/src/scss/admin/**/*.scss').on('all', gulp.series( sass, sassMin, rtl ) );
gulp.watch('assets/src/js/admin/**/*.js').on('all', javascript, javascriptMin );
}