-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmacro.js
179 lines (143 loc) · 5.76 KB
/
macro.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
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
var loaderUtils = require("loader-utils");
var assign = require("object-assign");
var warning = require("./utils/warning");
var error = require("./utils/error");
var parse = require("./utils/parse");
/**
*
* @param {string} s
* @return {string}
*/
function regEscape(s) {
return s.replace(/[-\/\\^$*+?.()|[\]{}]/g, "\\$&");
}
module.exports = function (callback, source, inputSourceMap) {
this.cacheable();
// Handle filenames (#106)
var webpackRemainingChain = loaderUtils.getRemainingRequest(this).split("!");
var filename = webpackRemainingChain[webpackRemainingChain.length - 1];
// Handle options
var globalOptions = this.options.macro || {};
var loaderOptions = loaderUtils.parseQuery(this.query);
var userOptions = assign({}, globalOptions, loaderOptions);
var defaultOptions = {
definition: [],
_IFDEF: "#IF_DEF",
_ELSE: "#ELSE",
_ENDIF: "#END_IF",
encoding: "utf8"
};
var options = assign({}, defaultOptions, userOptions);
/**
* The definition should be array.
*/
if (!Array.isArray(options.definition)) {
warning("The definition option for macro mode requires an array!", filename);
callback(null, source, inputSourceMap);
return;
}
var parseResult = parse(source);
if (!parseResult) {
warning("File cannot be parsed!", filename);
callback(null, source, inputSourceMap);
return;
}
var comments = parseResult.comments.filter(function (commentNode) {
return commentNode.type == "CommentLine";
});
var commentArray = [];
comments.forEach(function (comment) {
var ifdefReg = new RegExp("^\\s*<\\s*" + regEscape(options._IFDEF) + "\\s+([^>]*)\\s*>\\s*$", "g");
var elseReg = new RegExp("^\\s*<\\s*" + regEscape(options._ELSE) + "\\s*>\\s*$", "g");
var endifReg = new RegExp("^\\s*<\\s*" + regEscape(options._ENDIF) + "\\s*>\\s*$", "g");
if (comment.value.match(ifdefReg)) commentArray.push({comment: comment, type: "if"});
if (comment.value.match(elseReg)) commentArray.push({comment: comment, type: "else"});
if (comment.value.match(endifReg)) commentArray.push({comment: comment, type: "end"});
});
commentArray.sort(function (a, b) {
return a.comment.start - b.comment.start;
});
var buffer = new Buffer(source, options.encoding);
var result = (function () {
var length = commentArray.length;
if (!length) return true;
var current = 0, currentNode, currentType;
while (current < length) {
currentNode = commentArray[current].comment;
currentType = commentArray[current].type;
if (currentType !== "if") {
warning("The " + currentType + " marco is in the wrong position!", filename, currentNode.loc);
current++;
continue;
}
break;
}
var stack = [];
while (current < length) {
currentNode = commentArray[current].comment;
currentType = commentArray[current].type;
if (currentType == "if") {
stack.push(commentArray[current]);
current++;
continue;
} else if (currentType == "end") {
if (!stack.length) {
warning("The end marco is in the wrong position!", filename, currentNode.loc);
current++;
continue;
} else {
var ifComment = stack.pop(), elseComment;
if (ifComment.type == "else") {
elseComment = ifComment;
ifComment = stack.pop();
}
var defName = (
(new RegExp("^\\s*<\\s*" + regEscape(options._IFDEF) + "\\s+([^>]*)\\s*>\\s*$", "g")).exec(
ifComment.comment.value
)
)[1];
if (options.definition.indexOf(defName) < 0) {
if (elseComment) {
buffer.fill(" ", ifComment.comment.end, elseComment.comment.start - 1, options.encoding);
} else {
buffer.fill(" ", ifComment.comment.end, currentNode.start - 1, options.encoding);
}
} else {
if (elseComment) {
buffer.fill(" ", elseComment.comment.end, currentNode.start - 1, options.encoding);
}
}
current++;
continue;
}
} else if (currentType == "else") {
if (!stack.length) {
warning("The else marco is in the wrong position!", filename, currentNode.loc);
current++;
continue;
} else {
var lastComment = stack[stack.length - 1];
if (lastComment.type != "if") {
error("The else marco is in the wrong position!", filename, currentNode.loc);
return false;
} else {
stack.push(commentArray[current]);
current++;
continue;
}
}
}
}
if (stack.length) {
stack.forEach(function (_ifComment) {
warning("The if marco is in the wrong position!", filename, _ifComment.comment.loc);
});
}
return true;
}).apply(this);
callback(
null,
result === false ? source : buffer.toString(options.encoding),
inputSourceMap
);
};