-
Notifications
You must be signed in to change notification settings - Fork 58
/
Copy pathgulpfile.js
155 lines (128 loc) · 3.24 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
const util = require("util");
const path = require("path");
const SRC = path.join(__dirname, "./src");
const DEST = path.join(__dirname, "./dist");
const JS_DIR = "js/*.js";
const SCSS_DIR = "scss/*.scss";
const HTML_DIR = "*.html";
const ASSETS_DIR = "assets/**/*";
const LOCALES_DIR = "locales/*.json";
const OPTS = {
cwd: SRC
}
const pipeline = util.promisify(require("stream").pipeline);
const gulp = require("gulp");
const gulpif = require("gulp-if")
const sourcemaps = require("gulp-sourcemaps");
const through = require("through2");
const browserSync = require('browser-sync').create();
if (process.argv.includes("--production")) {
process.env.NODE_ENV = "production";
}
const mode = process.env.NODE_ENV || "development";
function prodOnly(task) {
return gulpif(
() => mode === "production",
task
)
}
function js() {
const webpack = require("webpack-stream");
const babel = require("gulp-babel");
const uglify = require("gulp-uglify");
return pipeline(
gulp.src(JS_DIR, OPTS),
webpack({
mode,
devtool: "source-map",
output: {
filename: "script.js"
}
}),
prodOnly(sourcemaps.init({loadMaps: true})),
prodOnly(through.obj(function (file, enc, cb) {
// Filter out the sourcemaps since gulp-sourcemaps handles them
if (!file.path.endsWith(".map")) this.push(file);
cb();
})),
prodOnly(babel({
presets: ["@babel/env"]
})),
prodOnly(uglify()),
prodOnly(sourcemaps.write(".")),
gulp.dest(DEST)
);
}
function css() {
const sass = require("gulp-sass");
const csso = require("gulp-csso");
const concat = require("gulp-concat");
const postcss = require("gulp-postcss");
const autoprefixer = require("autoprefixer");
sass.compiler = require("node-sass");
return pipeline(
gulp.src(SCSS_DIR, OPTS),
sourcemaps.init(),
sass(),
concat("style.css"),
prodOnly(postcss([autoprefixer()])),
prodOnly(csso()),
sourcemaps.write("."),
gulp.dest(DEST),
browserSync.stream({match: "**/*.css"})
)
}
function html() {
const htmlmin = require("gulp-htmlmin");
return pipeline(
gulp.src(HTML_DIR, OPTS),
htmlmin({collapseWhitespace: true}),
gulp.dest(DEST)
)
}
function assets() {
return pipeline(
gulp.src(ASSETS_DIR, OPTS),
gulp.dest(DEST)
);
}
function locales() {
return pipeline(
gulp.src(LOCALES_DIR, OPTS),
gulp.dest(path.join(DEST, "locales"))
);
}
function clean() {
const del = require("del");
return del(DEST);
}
exports.js = js;
exports.css = css;
exports.html = html;
exports.assets = assets;
exports.locales = locales;
const build = gulp.series(
clean,
gulp.parallel(js, css, html, assets, locales)
);
exports.build = build;
function serve() {
browserSync.init({
server: DEST,
cors: true
});
function reload() {
// We don't return it or else Gulp gets confuzzled
browserSync.reload();
return Promise.resolve(); // Instead we return a resolved Promise
}
const WATCH_OPTS = {
cwd: SRC
};
gulp.watch(JS_DIR, WATCH_OPTS, gulp.series(js, reload));
gulp.watch(SCSS_DIR, WATCH_OPTS, css); // We stream css directly into browserSync
gulp.watch(HTML_DIR, WATCH_OPTS, gulp.series(html, reload));
gulp.watch(ASSETS_DIR, WATCH_OPTS, gulp.series(assets, reload));
gulp.watch(LOCALES_DIR, WATCH_OPTS, gulp.series(locales, reload));
}
exports.serve = gulp.series(build, serve);