-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
136 lines (109 loc) · 3.14 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
'use strict';
var gutil = require('gulp-util');
var through = require('through2');
var _ = require('./lib/util.js');
var PluginError = gutil.PluginError;
var pluginName = 'gulp-inline-js';
var stringRegStr = '(?:' +
'\"(?:[^\\\\\"\\r\\n\\f]|\\\\[\\s\\S])*\"' + //match the " delimiter string
'|' +
'\'(?:[^\\\\\'\\r\\n\\f]|\\\\[\\s\\S])*\'' + //match the ' delimiter string
')';
var jscommentRegStr = '(?:' +
'\\/\\/[^\\r\\n\\f]*' + // match the single line comment
'|' +
'\\/\\*[\\s\\S]+?\\*\\/' + //match the multi line comment
')';
var inlineRegStr = '\\b(__inline)\\s*\\(\\s*(' + stringRegStr + ')\\s*\\)';
function createError(file, err) {
if (typeof err === 'string') {
return new PluginError(pluginName, file.path + ': ' + err, {
fileName: file.path,
showStack: false
});
}
var msg = err.message || err.msg || 'unspecified error';
return new PluginError(pluginName, file.path + ': ' + msg, {
fileName: file.path,
lineNumber: err.line,
stack: err.stack,
showStack: false
});
}
module.exports = function (opt) {
var embeddedMap = {};
function embeddedCheck(fileMain, fileEmbedded) {
var main = fileMain.path;
var embedded = fileEmbedded.path;
if (main === embedded) {
error('unable to embed file[' + main + '] into itself.');
} else if (embeddedMap[embedded]) {
var next = embeddedMap[embedded],
msg = [embedded];
while (next && next !== embedded) {
msg.push(next);
next = embeddedMap[next];
}
console.log(msg);
msg.push(embedded);
error('circular dependency on [' + msg.join('] -> [') + '].');
}
embeddedMap[embedded] = main;
return true;
}
function embeddedUnlock(file) {
delete embeddedMap[file.path];
}
function error(msg) {
//for watching, unable to exit
embeddedMap = {};
throw new Error(msg);
}
function embed(file) {
var reg = new RegExp(stringRegStr + '|' +
jscommentRegStr + '|' + inlineRegStr, 'g');
var content = String(file.contents);
content = content.replace(reg, function (m, type, url) {
if (type === '__inline') {
var info = _.uri(url, _.dirname(file.path));
if (info && info.file) {
try {
var f = info.file;
if (embeddedCheck(file, f)) {
embed(f);
m = String(f.contents);
}
}
catch (e) {
embeddedMap = {};
e.message = e.message + ' in [' + file.path + ']';
throw e;
}
}
else {
var msg = 'unable to inline non-existent file [' + url + ']'
throw new Error(msg);
}
}
return m;
});
file.contents = new Buffer(content);
}
function expand(file, encoding, callback) {
if (file.isNull()) {
return callback(null, file);
}
if (file.isStream()) {
return callback(createError(file, 'Streaming not supported'));
}
try {
embed(file);
}
catch (e) {
return callback(createError(file, e.message));
}
embeddedUnlock(file);
callback(null, file);
}
return through.obj(expand);
};