/
index.js
147 lines (132 loc) · 3.63 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
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
const through = require('through2');
const gutil = require('gulp-util');
const path = require('path');
const styleTagTrim = require('./utils/styleTagTrim')
const { commentReg, paranthesesReg } = require('./utils/reg')
function isStyle(file, target) {
let ext = path.extname(file.path);
if (ext === target) {
return true;
}
if (!/\.(htm|html|css|ejs|ftl)/.test(ext)) {
return false;
}
return true;
}
// clear style content
function clear(content, option) {
let ret = content.replace(/(^\s+)|\t+/g, '')
if (option.clearComment) {
ret = ret.replace(commentReg, '')
}
if (option.clearLine) {
ret = ret.replace(/\s+/g, '')
} else {
// clear \s\n in css rules
// ie. { ... }
ret = ret.replace(paranthesesReg, function ($, $1, $2) {
return $1.replace(/\n/g, '').replace(/\s{2,}/g, ' ') + $2.replace(/[\s\n]+/g, '') + '\n'
})
ret = ret.replace(/\n\s{1,}/g, '\n')
// remove last \n
if (ret.slice(-1) === '\n') {
ret = ret.slice(0, -1)
}
}
return ret;
}
function format(content, ext, option) {
if (ext === '.css' || ext == null) {
return clear(content, option)
} else {
// html content
// clear style tag
content = styleTagTrim(content)
let ret = [];
let styleIndexs = [];
let endTagLen = '</style>'.length;
let start = 0, end = 0,
len = content.length;
let prevEnd = start;
do {
start = content.indexOf('<style>', prevEnd)
end = content.indexOf('</style>', start)
if (start > -1 && end > -1) {
end += endTagLen;
ret.push.call(
ret,
content.slice(prevEnd, start),
clear(content.slice(start, end), option)
)
prevEnd = end;
}
} while (start > -1 && end > -1 && end <= len)
ret.push(content.slice(prevEnd));
if (option.merge) {
let isStyle = /^<style>/;
// filter \s between styles
ret = ret.filter((str, i, arr) => {
if (i > 0 && isStyle.test(arr[i - 1]) && isStyle.test(arr[i + 1])) {
return !/\s+/.test(str)
}
return true;
})
let styles = ret.filter(str => isStyle.test(str))
let others = ret.filter(str => !isStyle.test(str))
if (styles.length > 1) {
styles = '<style>' + styles.reduce((a, b) => {
return a + b.replace(/<.?style>/g, '')
}, '') + '</style>'
if (others.length > 1) {
others.splice(1, 0, styles)
if (others[0].slice(-1) !== '\n') {
others[1] = '\n' + others[1]
}
if (others[2] && others[2].slice(0) !== '\n') {
others[1] = others[1] + '\n'
}
} else {
others = styles;
}
ret = others
}
}
return ret.join('');
}
}
function gulpPrefix(option) {
option = Object.assign({
// clear comment
clearComment: true,
// merge all style tag if html
merge: false,
// remove \n\r
clearLine: true,
ext: null
}, option)
let stream = through.obj(function (file, encoding, callback) {
if (file.isNull()) {
return callback(null, file);
}
if (file.isStream()) {
return callback(createError(file, 'Streaming not supported'));
}
if (!file.isBuffer()) {
return callback(null, file);
}
let check = isStyle(file, option.ext);
if (check === false) {
return callback(null, file);
}
let result = file.contents.toString();
file.contents = Buffer.from(
format(result, path.extname(file.path), option)
)
callback(null, file);
});
stream.on('error', function (err) {
console.log(err);
});
return stream;
}
module.exports = gulpPrefix;