forked from quoid/userscripts
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgulpfile.js
More file actions
85 lines (76 loc) · 2.64 KB
/
Copy pathgulpfile.js
File metadata and controls
85 lines (76 loc) · 2.64 KB
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
const {dest, series, src} = require("gulp");
const del = require("del");
const inline = require("gulp-inline-source");
const postcss = require("gulp-postcss");
const autoprefixer = require("autoprefixer");
const htmlmin = require("gulp-html-minifier-terser");
const dom = require("gulp-dom");
const rename = require("gulp-rename");
const directory = process.env.NODE_ENV === "popup" ? "popup" : "page";
const copyLocation = "./temp";
const destLocation = "./extension/Userscripts Extension/Resources";
// clone public directory to avoid prefixing development assets
function copy() {
return src("./public/" + directory + "/**/*")
.pipe(dest(copyLocation));
}
// autoprefix select stylesheets and overwrite in place
function autoprefix() {
return src([copyLocation + "/build/bundle.css", copyLocation + "/css/global.css"])
.pipe(postcss([
autoprefixer({overrideBrowserslist: ["safari >= 13"]})
]))
.pipe(dest(file => file.base));
}
// inline assets
function inlineAssets() {
return src(`${copyLocation}/index.html`)
.pipe(inline({
attribute: false,
compress: false,
ignore: []
}))
.pipe(htmlmin({
collapseWhitespace: true,
minifyCSS: true,
minifyJS: false,
removeComments: true
}))
.pipe(dest(file => file.base));
}
// remove the inlined javascript and save to singular js file
function bundleJS() {
return src(`${copyLocation}/index.html`)
.pipe(dom(function() {
const scripts = this.querySelectorAll("script");
let result = "";
scripts.forEach(function(script) {
result += script.innerHTML;
});
return result;
}, false))
.pipe(rename(directory + ".js"))
.pipe(dest(destLocation));
}
// remove the scripts tags from source file and move/rename html file
function removeTags() {
return src(`${copyLocation}/index.html`)
.pipe(dom(function() {
const scripts = this.querySelectorAll("script");
scripts.forEach(function(script) {
const parent = script.parentNode;
parent.removeChild(script);
});
const f = this.createElement("script");
f.setAttribute("src", directory + ".js");
this.body.appendChild(f);
return this;
}, false))
.pipe(rename(directory + ".html"))
.pipe(dest(destLocation));
}
// remove the temp folder
function clean() {
return del(copyLocation);
}
exports.build = series(copy, autoprefix, inlineAssets, bundleJS, removeTags, clean);