-
Notifications
You must be signed in to change notification settings - Fork 154
/
gulpfile.js
106 lines (97 loc) · 2.26 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
const { src, dest, task, series, parallel } = require("gulp");
const webpack = require("webpack-stream");
const less = require("gulp-less");
const autoprefix = require("gulp-autoprefixer");
const uglify = require("gulp-uglify");
const minifyCSS = require("gulp-csso");
const gzip = require("gulp-gzip");
const rename = require("gulp-rename");
const clean = require("gulp-clean");
const path = require("path");
const fs = require("fs");
const resolve = (name) => path.resolve(__dirname, name);
const cssPath = "./source/css/min";
const jsPath = "./source/js/min";
task("clean", () => {
return src([cssPath, jsPath], {
read: false,
allowEmpty: true,
}).pipe(
clean({
force: true,
})
);
});
task("css", () => {
const ignoreFiles = ["video"].map((file) => `./source/css/${file}.less`);
return src("./source/css/*.less", {
ignore: ignoreFiles,
})
.pipe(less())
.pipe(
autoprefix({
overrideBrowserslist: ["> 2%", "last 2 versions", "not ie 6-9"],
cascade: false,
})
)
.pipe(minifyCSS())
.pipe(
rename({
suffix: ".min",
})
)
.pipe(dest(cssPath));
});
task("js", () => {
const getEntryData = () => {
const ignoreFiles = ["pjax.js", "census.js"];
try {
let files = fs.readdirSync("./source/js", "utf-8");
files = files.filter((file) => {
return ignoreFiles.length
? /\.js$/.test(file) && !ignoreFiles.includes(file)
: /\.js$/.test(file);
});
const result = {};
files.forEach((file) => {
const fileName = file.replace(/.js$/, "");
result[fileName] = resolve(`./source/js/${file}`);
});
result.utils = resolve("./source/utils/utils.js");
return result;
} catch (error) {
throw new Error(err);
}
};
return webpack({
mode: "production",
entry: getEntryData(),
module: {
rules: [
{
test: /\.js$/,
loader: "babel-loader",
include: resolve("source"),
exclude: resolve("node_modules"),
options: {
presets: ["@babel/preset-env"],
plugins: ["@babel/plugin-transform-runtime"],
},
},
],
},
stats: "errors-only",
output: {
filename: "[name].min.js",
},
})
.pipe(uglify())
.pipe(dest(jsPath))
.pipe(
gzip({
threshold: "10kb",
})
)
.pipe(dest(jsPath));
});
task("default", series("clean", parallel("css", "js")));