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
125 lines (116 loc) · 3.75 KB
/
Copy pathgulpfile.js
File metadata and controls
125 lines (116 loc) · 3.75 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
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
const { dest, parallel, series, src, watch } = require("gulp");
var assets = require("postcss-assets");
const atImport = require("postcss-import");
const autoprefixer = require("autoprefixer");
const connect = require("gulp-connect");
const del = require("del");
const htmlmin = require("gulp-htmlmin");
const inline = require("gulp-inline-source");
const nano = require("cssnano");
const nunjucksRender = require("gulp-nunjucks-render");
const postcss = require("gulp-postcss");
const sourcemaps = require("gulp-sourcemaps");
const sourcePath = "./src";
const outputPath = "./output";
function clean() {
// delete all files at the output path, except .gitignore
const paths = [
`${outputPath}/**/*`
];
return del(paths);
}
function css() {
// NODE_ENV production is set when using the "npm run build" command
// if NODE_ENV === production, no sourcemaps and minify the css
if (process.env.NODE_ENV != "production") {
return src(`${sourcePath}/css/_main.css`)
.pipe(sourcemaps.init())
.pipe(postcss([
atImport(),
assets(),
autoprefixer({overrideBrowserslist: ["defaults"]})
]))
.pipe(sourcemaps.write())
.pipe(dest(outputPath));
} else {
return src(`${sourcePath}/css/_main.css`)
.pipe(postcss([
atImport(),
assets(),
autoprefixer({overrideBrowserslist: ["defaults"]}),
nano()
]))
.pipe(dest(outputPath));
}
}
function js() {
// copy over the javascript files, as is
return src(`${sourcePath}/js/**/*.js`)
.pipe(dest(`${outputPath}/js/`));
}
function html() {
// set a var for whether or not we are in dev mode
// development var will be false when running the npm run build command
// during development we will NOT inline css or js files
// that way we can properly debug
// on builds everything will inlined to a single html file
var development = true;
var ignore = ["css", "js"];
if (process.env.NODE_ENV === "production") {
development = false;
ignore = [];
}
return src(`${sourcePath}/index.html`)
.pipe(nunjucksRender({
// pass custom data to nunjucks for use throughout template(s)
// can use custom data from package.json (ex. title)
data: {
development: development,
title: process.env.npm_package_websiteTitle
},
path: `${sourcePath}/html`
}))
.pipe(inline({
compress: true,
ignore: ignore
}))
.pipe(htmlmin({
collapseWhitespace: true,
minifyJS: false,
removeComments: true
}))
.pipe(dest(outputPath))
.pipe(connect.reload());
}
function complete() {
// delete all the files at the output path except the index.html file
// this is a separate function than the clean function above because
// on the intial clean we want to remove EVERYTHING
// on the build complete, we want to leave the index.html
const paths = [
`${outputPath}/**/*`,
`!${outputPath}/index.html`
];
return del(paths);
}
function server(cb) {
connect.server({
root: outputPath,
livereload: true,
port: 8888
});
cb();
}
function watcher(cb) {
const paths = [
`${sourcePath}/**/*.html`,
`${sourcePath}/**/*.css`,
`${sourcePath}/**/*.js`,
`${sourcePath}/**/*.svg`
];
watch(paths, series(css, js, html));
cb();
}
exports.build = series(clean, css, js, html, complete);
exports.clean = clean;
exports.server = series(clean, css, js, html, parallel(server, watcher));