-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
258 lines (238 loc) · 6.78 KB
/
Copy pathindex.js
File metadata and controls
258 lines (238 loc) · 6.78 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
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
/**
* Created by Rodey on 2015/11/9.
*/
'use strict';
const fs = require('fs');
const path = require('path');
const through2 = require('through2');
const PluginError = require('plugin-error');
const uglifyjs = require('uglify-js');
const uglifycss = require('uglifycss');
const Tool = require('./lib/tools');
const PLUGIN_NAME = 'gulp-tag-include';
//正则匹配
var tagName = 'include',
includerRegx = new RegExp(
'<' + tagName + '\\s+([\\s\\S]*?)>([\\s\\S]*?)<\\/' + tagName + '>',
'gi'
),
includer2Regx = new RegExp(
'\\s*@' +
tagName +
'\\s*\\(\\s*[\\\'|"]([\\s\\S]*?)[\\\'|"],\\s*\\{*([\\s\\S]*?)\\}*\\s*\\)',
'gi'
),
srcRegx = new RegExp('\\s*src\\s*=\\s*[\\\'|"]([\\s\\S]*?)"', 'gi'),
attrReg = new RegExp('\\s+(\\S+)\\s*=\\s*[\\\'|"]([\\s\\S]*?)[\\\'|"]', 'gi'),
attr2Reg = new RegExp(
'\\s*([\\s\\S]*?)\\s*:\\s*[\\\'|"]([\\s\\S]*?)[\\\'|"],?',
'gi'
);
/**
* html标签形式引入
* @param filePath 当前页面地址
* @param $1 匹配到的页面内容
* @param options 可选参数
* @returns {*}
*/
var replaceTag = function (filePath, $1, options) {
var ms = srcRegx.exec($1),
src = ms[1] || '',
isCompress = options['compress'],
extractCss = options['extractCss'],
extractJs = options['extractJs'],
globals = options['globals'];
srcRegx.lastIndex = 0;
src = path.normalize(path.dirname(filePath) + path.sep + src);
if (!fs.existsSync(src)) {
return $1;
}
var htmlContent = Tool.getFileContent(src);
//判断文件类型--add
var ext = path.extname(src);
if (ext === '.css') {
if (isCompress) {
htmlContent = uglifycss.processString(htmlContent);
}
// 是否将引入的css文件合并输出到指定的单独文件中
if (extractCss) {
if (!path.isAbsolute(extractCss)) {
extractCss = path.resolve(process.cwd(), extractCss);
}
if (!fs.existsSync(extractCss)) {
fs.writeFileSync(extractCss, '', 'utf8');
}
fs.appendFileSync(extractCss, htmlContent, 'utf8');
htmlContent = '';
} else {
htmlContent =
'<style type="text/css" charset="utf-8">' + htmlContent + '</style>';
}
} else if (ext === '.js') {
if (isCompress) {
htmlContent = uglifyjs.minify(htmlContent, {}).code;
}
// 是否将引入的js文件合并输出到指定的单独文件中
if (extractJs) {
if (!path.isAbsolute(extractJs)) {
extractJs = path.resolve(process.cwd(), extractJs);
}
if (!fs.existsSync(extractJs)) {
fs.writeFileSync(extractJs, '', 'utf8');
}
fs.appendFileSync(extractJs, htmlContent, 'utf8');
htmlContent = '';
} else {
htmlContent =
'<script type="text/javascript" charset="utf-8" defer>' +
htmlContent +
'</script>';
}
}
//=========标签内容属性替换
/**
* exp: <includ src="assets/layout/header.html" title="html页面已引入" css="index.css"></includ>
*/
if (options.tagAttr === true)
htmlContent = Tool.extractTagAttr(globals, htmlContent, $1, attrReg);
//=========内容属性替换
/**
* <includ src="assets/layout/header.html">
* @title = html页面已引入
* @css = index.css
* </includ>
*/
if (options.tagContent === true)
htmlContent = Tool.extractTagContent(
globals,
htmlContent,
includerRegx,
$1
);
//去除空变量
htmlContent = Tool.cleanEmptyVars(htmlContent);
//递归遍历
if (
htmlContent.search(includerRegx) !== -1 ||
htmlContent.search(includer2Regx) !== -1
) {
htmlContent = replaceCallback(src, htmlContent, options);
}
return Tool.rtrim(htmlContent);
};
/**
* 模板方法形式引入
* @param filePath 当前页面地址
* @param content 引入的模板内容
* @param src 引入的模板地址,方便递归
* @param args 引入的参数列表
* @param options 可选参数
* @returns {*}
*/
var replaceMethodTag = function (filePath, src, args, options) {
var src = path.normalize(path.dirname(filePath) + path.sep + src);
if (!fs.existsSync(src)) {
return src;
}
var htmlContent = Tool.getFileContent(src),
args = args;
//属性参数替换
htmlContent = Tool.extractTagAttr(
options['globals'],
htmlContent,
args,
attr2Reg
);
//递归遍历替换
if (
htmlContent.search(includer2Regx) !== -1 ||
htmlContent.search(includerRegx) !== -1
) {
htmlContent = replaceCallback(src, htmlContent, options);
}
return htmlContent;
};
/**
* 处理文件内容
* @param filePath
* @param content
* @param options 可选参数
* @returns {*}
*/
var replaceCallback = function (filePath, content, options) {
var content = content
/**
* html标签形式 <include src="template src" [!args]></include>
*/
.replace(includerRegx, function ($1) {
return replaceTag(filePath, $1, options);
})
/**
* 以模板方法的形式 @include('template src', { [!args] })
*/
.replace(includer2Regx, function ($1, src, args) {
return replaceMethodTag(filePath, src, args, options);
});
return content;
};
/**
* 重置匹配正则
* @param options
*/
var resetIncludeRegx = function (options) {
var tagName = options.tagName;
includerRegx = new RegExp(
'<' + tagName + '\\s+([\\s\\S]*?)>([\\s\\S]*?)<\\/' + tagName + '>',
'gi'
);
includer2Regx = new RegExp(
'\\s*@' +
tagName +
'\\s*\\(\\s*[\\\'|"]([\\s\\S]*?)[\\\'|"],\\s*\\{*([\\s\\S]*?)\\}*\\s*\\)',
'gi'
);
};
//获取文件内容
var getContent = function (file, options) {
//默认参数
//tagAttr:标签上的属性
//tagContent: 标签内的属性
//Learn more as README.md
var opts = options || {};
opts['tagAttr'] = true;
opts['tagContent'] = true;
opts['tagName'] = opts.tagName || 'include';
opts['compress'] = opts['compress'] === undefined ? true : opts['compress'];
opts['globals'] = opts['globals'] || {};
resetIncludeRegx(opts);
//对内容进行处理
var content = file.contents.toString('utf-8'),
filePath = file.path;
if (typeof content === 'undefined') {
content = Tool.getFileContent(filePath);
}
content = replaceCallback(filePath, content, opts);
return content;
};
var includer = function (options) {
return through2.obj(function (file, enc, next) {
if (file.isStream()) {
this.emit(
'error',
new PluginError(PLUGIN_NAME, 'Stream content is not supported')
);
return next(null, file);
}
if (file.isBuffer()) {
try {
var content = getContent(file, options);
file.contents = Buffer.from(content);
} catch (err) {
this.emit('error', new PluginError(PLUGIN_NAME, ''));
}
}
this.push(file);
return next();
});
};
module.exports = includer;