forked from jarstone/dselect
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.js
193 lines (180 loc) · 5.27 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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
const { src, dest, watch, series, parallel } = require('gulp')
const fs = require('fs-extra')
const util = require('util')
const path = require('path')
const exec = util.promisify(require('child_process').exec)
const rename = require('gulp-rename')
const esbuild = require('esbuild')
const through = require('through2')
const source = {
css : 'source/css',
js : 'source/js',
}
const compiled = {
css : 'dist/css',
js : 'dist/js',
}
function clean() {
return new Promise(function (resolve) {
for (const [key, value] of Object.entries(compiled)) {
fs.emptyDirSync(value)
}
resolve()
})
}
let filtered_css = []
function css() {
return new Promise(async function (resolve) {
if (filtered_css.length) {
for (const file of filtered_css) {
const dest = file.replace(
path.join(__dirname, source.css),
path.join(__dirname, compiled.css)
).slice(0, -4) + 'css'
await exec(`sass --source-map --embed-sources ${file} ${dest} --quiet`).catch(err => console.log(err.stderr))
}
} else {
await exec(`sass --source-map --embed-sources ${source.css}:${compiled.css} --quiet`).catch(err => console.log(err.stderr))
}
resolve()
})
}
function css_watch() {
watch(source.css).on('all', async function (event, target) {
const obj = path.parse(target)
let targets = []
switch (event) {
case 'add':
case 'change':
if (obj.name.startsWith('_')) {
await new Promise(async function (resolve) {
src(`${source.css}/**/!(_*).scss`)
.pipe(through.obj(function (file, enc, callback) {
const content = file.contents.toString().split(/\r?\n/).filter(i => i.startsWith('@import')).join('')
if (content.includes(`${obj.name.substring(1)}'`) || content.includes(`${obj.name.substring(1)}"`)) {
targets.push(file.path)
}
return callback()
}))
.on('finish', resolve)
})
} else if (obj.ext === '.scss') {
targets.push(path.join(__dirname, obj.dir, obj.base))
}
break;
case 'unlink':
const removedTarget = path.join(__dirname, target).replace(
path.join(__dirname, source.css),
path.join(__dirname, compiled.css),
).slice(0, -4) + 'css'
fs.removeSync(removedTarget)
fs.removeSync(removedTarget + '.map')
fs.removeSync(removedTarget.slice(0, -3) + 'min.css')
break;
}
filtered_css = targets
})
return watch(source.css, css)
}
function css_prefix() {
return new Promise(async function (resolve) {
await exec(`npx postcss ${compiled.css}/*.css !${compiled.css}/*.min.css --use autoprefixer --map --replace`).catch(err => console.log(err.stderr))
resolve()
})
}
function css_minify() {
return src(`${compiled.css}/!(*.min).css`)
.pipe(through.obj(function (file, enc, callback) {
let content = file.contents.toString()
content = esbuild.transformSync(content, {
loader: 'css',
minify: true,
}).code
file.contents = Buffer.from(content)
this.push(file)
return callback()
}))
.pipe(rename({ suffix: '.min' }))
.pipe(dest(compiled.css))
}
let filtered_js = []
function js() {
return src(`${source.js}/**/*.js`)
.pipe(through.obj(function (file, enc, callback) {
if (pass(file.path, filtered_js)) {
let content = file.contents.toString()
content = esbuild.transformSync(content).code
file.contents = Buffer.from(content)
this.push(file)
}
return callback()
}))
.pipe(dest(compiled.js))
}
function js_watch() {
watch(source.js).on('all', async function (event, target) {
const obj = path.parse(target)
let targets = []
switch (event) {
case 'add':
case 'change':
if (obj.ext === '.js') {
targets.push(path.join(__dirname, obj.dir, obj.base))
}
break;
case 'unlink':
const removedTarget = path.join(__dirname, target).replace(
path.join(__dirname, source.js),
path.join(__dirname, compiled.js),
)
fs.removeSync(removedTarget)
fs.removeSync(removedTarget.slice(0, -2) + 'min.js')
break;
}
filtered_js = targets
})
return watch(source.js, js)
}
function js_minify() {
return src(`${compiled.js}/!(*.min).js`)
.pipe(through.obj(function (file, enc, callback) {
let content = file.contents.toString()
content = esbuild.transformSync(content, {
minify: true,
}).code
file.contents = Buffer.from(content)
this.push(file)
return callback()
}))
.pipe(rename({ suffix: '.min' }))
.pipe(dest(compiled.js))
}
function pass(item, items) {
if (items.length === 0) {
return true
} else {
return items.includes(item)
}
}
exports.css = css
exports.css_watch = css_watch
exports.css_prefix = css_prefix
exports.css_minify = css_minify
exports.js = js
exports.js_watch = js_watch
exports.js_minify = js_minify
exports.dev = parallel(css_watch, js_watch)
exports.build = series(
clean,
parallel(
series(
css,
css_prefix,
css_minify,
),
series(
js,
js_minify
),
)
)