-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.js
80 lines (79 loc) · 2.15 KB
/
build.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
const cssnano = require('cssnano')
const UglifyJS = require('uglify-es') // for let support
const htmlMinifier = require('html-minifier').minify
const fs = require('fs')
const path = require('path')
const fsDir = path.resolve('./fs_dev')
const fsMinDir = path.resolve('./fs')
const files = fs.readdirSync(fsDir)
if (fs.existsSync(fsMinDir)) {
const files = fs.readdirSync(fsMinDir)
for (let file of files) {
try {
fs.unlinkSync(path.resolve('./fs/' + file))
} catch (e) {
console.error(e)
}
}
} else {
fs.mkdirSync(fsMinDir)
}
for (let file of files) {
const ext = (file === 'init.js' || /\.min\./i.test(file)) ? false : path.extname(file)
const fileIn = path.resolve('./fs_dev/' + file)
const fileOut = path.resolve('./fs/' + file)
switch (ext) {
case '.js': {
const res = UglifyJS.minify(fs.readFileSync(fileIn, 'utf8'))
if (res.error) {
console.error('File: ', file, fileOut)
console.error(res.error)
} else {
fs.writeFileSync(fileOut, res.code)
}
break
}
case '.html': {
fs.writeFileSync(fileOut, htmlMinifier(fs.readFileSync(fileIn, 'utf8'), {
collapseWhitespace: true,
removeComments: true,
removeOptionalTags: true,
removeRedundantAttributes: true,
removeAttributeQuotes: true,
minifyCSS: true,
minifyJS: true,
preventAttributesEscaping: true,
removeEmptyAttributes: true,
removeScriptTypeAttributes: true,
removeStyleLinkTypeAttributes: true,
removeTagWhitespace: true,
useShortDoctype: true
}))
break
}
case '.css': {
cssnano.process(fs.readFileSync(fileIn), {
from: file
}, {
preset: ['default', {
discardComments: {
removeAll: true
}
}]
}).then(result => {
fs.writeFileSync(fileOut, result)
})
break
}
// to do minify email
case '.png':
case '.gif':
case '.jpeg':
case '.jpg':
// default: copy file
// eslint-disable-next-line
default: {
fs.createReadStream(fileIn).pipe(fs.createWriteStream(fileOut))
}
}
}