-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
92 lines (80 loc) · 2.6 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
/**
* @file 内联静态资源的入口模板
* @author [email protected]
*/
var Inliner = require('./lib/processor/inliner');
// 注册预定义的内联处理器
Inliner.register(
require('./lib/processor/css'),
require('./lib/processor/font'),
require('./lib/processor/html'),
require('./lib/processor/img'),
require('./lib/processor/js'),
require('./lib/processor/svg')
);
exports.inline = Inliner.inline;
/**
* 增加自定义的内联任务方法
*
* @param {string} type 内联的处理器类型,`html` | `js` | `css` | `img` | `font` | `svg`
* @param {Object} task,增加的内联任务,可以是如下三种结构:
* 1) 直接传入执行的任务处理方法
* function (file) { return file.data; }
* 2) 传入一个对象,结构如下
* {
* enable: function () { return true; },
* task: function (file) { return file.data }
* }
* 3)传入一个数组,表示任务列表,数组元素结构为1)或 2)
*/
exports.addInlineTaskFor = function (type, task) {
Inliner.addInlineTask(type, task);
};
/**
* 移除指定类型的内联任务
*
* @param {string} type 要移除的任务的目标处理器类型
* @param {Function} task 要移除的内联任务
*/
exports.removeInlineTask = function (type, task) {
Inliner.removeInlineTask(type, task);
};
/**
* 注册内联处理器
*
* @param {string} type 处理器类型名
* @param {Object} processor 处理器,e.g.,
* {
* taskList: [
* function (file) {
* var me = this;
* return file.data.replace(
* /<tpl\s+src="([^"]+)">/g,
* function (match, path) {
* var result = me.getInlineResult(path);
* return result ? result.data : match;
* }
* );
* }
* ],
* compress: function (file, option) {
*
* }
* }
*/
exports.registerInlineProcessor = function (type, processor) {
if (!processor || typeof processor !== 'object') {
return;
}
function CustomProcessor() {
Inliner.apply(this, arguments);
this.file.data = this.file.data.toString();
this.initInlineTasks(processor.taskList);
}
require('util').inherits(CustomProcessor, Inliner);
CustomProcessor.prototype.type = type;
var proto = Object.assign({}, processor);
delete proto.taskList;
Object.assign(CustomProcessor.prototype, proto);
Inliner.register(CustomProcessor);
};