-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.babel.js
189 lines (173 loc) · 4.47 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
import yargs from "yargs";
import cleanCss from "gulp-clean-css";
import gulpif from "gulp-if";
import postcss from "gulp-postcss";
import sourcemaps from "gulp-sourcemaps";
import autoprefixer from "autoprefixer";
import { src, dest, watch, series, parallel } from "gulp";
import imagemin from "gulp-imagemin";
import webpack from "webpack-stream";
import named from "vinyl-named";
import del from "del";
import browserSync from "browser-sync";
import zip from "gulp-zip";
import info from "./package.json";
import replace from "gulp-replace";
import wpPot from "gulp-wp-pot";
import rename from "gulp-rename";
const PRODUCTION = yargs.argv.prod;
var sass = require("gulp-sass")(require("sass"));
var paths = {
root: "./src",
html: {
src: "src/*.html",
},
styles: {
src: "src/scss/**/*.scss",
admin: "src/scss/**/*.scss",
dest: "src/css",
},
scripts: {
src: "src/js/**/*.js",
dest: "src/js",
},
};
export const styles = () => {
return src([paths.styles.src, paths.styles.admin])
.pipe(gulpif(!PRODUCTION, sourcemaps.init()))
.pipe(sass().on("error", sass.logError))
.pipe(gulpif(PRODUCTION, postcss([autoprefixer])))
.pipe(gulpif(PRODUCTION, cleanCss({ compatibility: "ie8" })))
.pipe(gulpif(!PRODUCTION, sourcemaps.write()))
.pipe(dest(paths.styles.dest))
.pipe(server.stream());
};
export const scripts = () => {
return src([
"src/js/bundle.js",
"src/js/admin.js",
"src/js/customize-preview.js",
])
.pipe(named())
.pipe(
webpack({
module: {
rules: [
{
test: /\.js$/,
use: {
loader: "babel-loader",
options: {
presets: ["@babel/preset-env"], //or ['babel-preset-env']
},
},
},
],
},
mode: PRODUCTION ? "production" : "development",
devtool: !PRODUCTION ? "inline-source-map" : false,
output: {
filename: "[name].js",
},
externals: {
jquery: "jQuery",
},
})
)
.pipe(dest("dist/js"));
};
export const images = () => {
return src("src/images/**/*.{jpg,jpeg,png,svg,gif}")
.pipe(gulpif(PRODUCTION, imagemin()))
.pipe(dest("dist/images"));
};
export const copy = () => {
return src([
"src/**/*",
"!src/{images,js,scss}",
"!src/{images,js,scss}/**/*",
]).pipe(dest("dist"));
};
export const watchForChanges = () => {
watch("src/scss/**/*.scss", styles, reload);
watch("src/images/**/*.{jpg,jpeg,png,svg,gif}", series(images, reload));
watch(
["src/**/*", "!src/{images,js,scss}", "!src/{images,js,scss}/**/*"],
series(copy, reload)
);
watch("src/js/**/*.js", series(scripts, reload));
watch("**/*.php", reload);
};
export const clean = () => {
return del(["dist", "bundled", "languages"]);
};
export const compress = () => {
return src([
"**/*",
"!node_modules{,/**}",
"!bundled{,/**}",
"!src{,/**}",
"!.babelrc",
"!.gitignore",
"!gulpfile.babel.js",
"!package.json",
"!package-lock.json",
])
.pipe(replace("_plugintitle", info.title))
.pipe(replace("_plugindescription", info.description))
.pipe(replace("_pluginauthor", info.author))
.pipe(replace("_pluginname_", info.name + "_"))
.pipe(replace("_pluginname-", info.slug + "-"))
.pipe(replace("_pluginname", info.slug))
.pipe(replace("_PluginName", info.namespace))
.pipe(
rename(function (path) {
console.log(path);
if (path.basename.includes("_pluginname")) {
path.basename = `${info.slug}`;
}
})
)
.pipe(
rename(function (path) {
path.dirname = `${info.slug}/` + path.dirname; // Change 'folder_name' to your desired folder name
})
)
.pipe(dest("temp"))
.pipe(zip(`${info.slug}.zip`))
.pipe(dest("bundled"));
};
export const pot = () => {
return src("**/*.php")
.pipe(
wpPot({
domain: "_pluginname",
package: info.name,
})
)
.pipe(dest(`languages/${info.name}.pot`));
};
const server = browserSync.create();
export const serve = (done) => {
server.init({
proxy: "http://provider.test/",
});
done();
};
export const reload = (done) => {
server.reload();
done();
};
export const dev = series(
clean,
parallel(styles, images, copy, scripts),
serve,
watchForChanges
);
export const build = series(
clean,
parallel(styles, images, copy, scripts),
pot,
compress
);
export default dev;