-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.babel.js
97 lines (85 loc) · 2.19 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
import del from 'delete';
import fs from 'fs';
import gulp from 'gulp';
import gulpif from 'gulp-if';
import path from 'path';
import pkg from './package.json';
import program from 'commander';
import through from 'through2';
import webpack from 'webpack';
program
.version(pkg.version)
.option('-o, --output [path]', 'Output folder', '')
.parse(process.argv);
const config = mode => ({
mode,
entry: {
lazyload: path.resolve(__dirname, 'src/lazyload.js'),
},
output: {
path: path.resolve(__dirname, 'dist/trunk'),
filename: '[name].[hash:8].min.js',
chunkFilename: '[name].[hash:8].min.js',
},
module: {
rules: [
{
test: /\.js$/,
exclude: /node_modules/,
loader: 'babel-loader',
},
],
},
});
const addWPVersion = (options) => through.obj((file, enc, next) => {
const content = file.contents.toString('utf-8').replace(/Version:\s+\S+/, `Version: ${pkg.version}`);
file.contents = Buffer.from(content);
next(null, file);
});
const isFile = filename => file => file.relative === filename;
const clean = () => new Promise(resolve => del(['dist/trunk'], (err, deleted) => {
if (err) throw err;
resolve();
}));
const bundle = mode => () => new Promise(resolve => webpack(config(mode), (err, stats) => {
if (err) throw err;
resolve();
}));
const copy = () => gulp.src(['src/**/*.txt', 'src/**/*.php'])
.pipe(gulpif(isFile('wp-accelerate.php'), addWPVersion()))
.pipe(gulp.dest('dist/trunk'));
const copyToFolder = () => new Promise(resolve => {
if (!!program.output) {
del.sync(program.output, { force: true });
fs.mkdirSync(program.output, { recursive: true });
resolve(gulp.src('dist/trunk/**/*.*').pipe(gulp.dest(program.output)));
} else {
resolve();
};
})
const watchJs = () => gulp.watch('src/**\/*.js', gulp.series(
clean,
bundle('development'),
copy,
copyToFolder,
));
const watchPhp = () => gulp.watch('src/**\/*.php', gulp.series(
copy,
copyToFolder,
));
export const start = gulp.series(
clean,
bundle('development'),
copy,
copyToFolder,
gulp.parallel(
watchJs,
watchPhp,
),
);
export const build = gulp.series(
clean,
bundle('production'),
copy,
copyToFolder,
);