This repository has been archived by the owner on Nov 27, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
122 lines (105 loc) · 3.12 KB
/
index.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
const { src, dest, lastRun } = require('gulp');
const $ = require('gulp-load-plugins')();
// Plugins
const autoprefixer = require('autoprefixer');
const browserify = require('browserify');
const buffer = require("vinyl-buffer");
const cssnano = require('cssnano');
const del = require('del');
const fs = require('fs');
const log = require('fancy-log');
const source = require("vinyl-source-stream");
const uglify = require('gulp-uglify-es').default;
// Use Dart to compile sass instead of node-sass
var gulpSass = require('gulp-sass')(require('sass'));
const args = require('minimist')(process.argv.slice(2), {
string: "env",
default: { "env": "development" }
})
const isDevelopment = function () {
return (args.env === 'development');
};
const isProduction = function () {
return !isDevelopment();
};
// checks if a sass file should be build into its own file or is just a partial
// Returns 'true' for standalone files
// Returns 'false' for includes
const sassFilter = function (file) {
return !file.path.includes("/_")
};
function pug(source, destination = '../templates', amp = '') {
let withAMP = false;
let css = '';
if (amp != '') {
css = fs.readFileSync('../public/css/amp.css').toString();
withAMP = true;
}
return src(source, { since: lastRun(pug) })
.pipe($.pug())
.pipe($.if(withAMP, $.replace('<!--amp-style-->', `${css}`)))
.pipe($.rename({
extname: ".twig"
}))
.pipe(dest(destination));
}
function sass(source, idir = '', destination = '../public/css', extraPlugins = []) {
plugins = extraPlugins.concat([
autoprefixer(),
isProduction() ? cssnano() : false,
].filter(Boolean));
return src(source)
.pipe($.sassInheritance({ dir: idir }))
.pipe($.filter(sassFilter))
.pipe($.sourcemaps.init())
.pipe(gulpSass())
.pipe($.postcss(plugins))
.pipe($.sourcemaps.write('./maps'))
.pipe(dest(destination));
}
function script(scriptpath, destination = '../public/js') {
return browserify({
entries: scriptpath,
debug: isDevelopment()
})
.bundle()
.on('error', err => {
log.error("Browserify Error: " + err.message);
})
.pipe(source(scriptpath.split("/").pop()))
.pipe(buffer())
.pipe($.sourcemaps.init())
.pipe($.if(isProduction(), uglify()))
.pipe($.sourcemaps.write('./maps'))
.pipe(dest(destination));
}
function copy(from, to) {
let rename = false
let pathParts = []
let newName, destPath = ""
// Check if user provided a filename
if(!to.endsWith("/")){
pathParts = to.split("/")
newName = pathParts.pop()
destPath = pathParts.join()
rename = true
} else {
destPath = to
}
return src(from)
.pipe($.if(rename, $.rename(newName)))
.pipe(dest(destPath))
}
function clean(dirs) {
log('Clean destination directories.')
return del(dirs, {
force: true
});
}
module.exports = {
copy: copy,
pug: pug,
sass: sass,
script: script,
clean: clean,
}