File size: 5,950 Bytes
4b876a7 | 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 | let _ = require('lodash');
let loaderUtils = require('loader-utils');
const os = require('os');
let definitions;
let EOLChar; // Holds content EOL marker.
const globalRegex = /(?:((?:\/[*]|<!--).*?(?:[*]\/|-->))|(.*?))*/gm;
const elifRegex = /(?:\/[*]|<!--)(?:\s*)#elif\s+([!]?[\w-_]+(?:(?:&&[!]?[\w-_]+)*|(?:[|]{2}[!]?[\w-_]+)*))(?:\s*)(?:[*]\/|-->)/;
const elseRegex = /(?:\/[*]|<!--)(?:\s*)#else(?:\s*)(?:[*]\/|-->)/;
const endifRegex = /(?:\/[*]|<!--)(?:\s*)#endif(?:\s*)(?:[*]\/|-->)/;
const ifRegex = /(?:\/[*]|<!--)(?:\s*)#if\s+([!]?[\w-_]+(?:(?:&&[!]?[\w-_]+)*|(?:[|]{2}[!]?[\w-_]+)*))(?:\s*)(?:[*]\/|-->)/;
function getBranchCode(branchRules, code = '') {
let activeBranch = _.find(branchRules, rule => {
if (!rule.condition) { return true; }
if (rule.condition.type === 'and') {
let r = _.intersection(rule.condition.regard, definitions);
let dr = _.difference(rule.condition.disregard, definitions);
return r.length + dr.length === rule.condition.regard.length + rule.condition.disregard.length;
} else if (rule.condition.type === 'or') {
let r = _.intersection(rule.condition.regard, definitions);
let dr = _.difference(rule.condition.disregard, definitions);
return r.length + dr.length > 0;
} else if (rule.condition[0] === '!') {
return definitions.indexOf(rule.condition.substr(1)) === -1;
} else {
return definitions.indexOf(rule.condition) !== -1;
}
});
if (activeBranch) {
return getCode(activeBranch.content);
}
}
function getCode(rules, code = '') {
let rule = rules.shift();
if (!rule) {
return code;
}
if (rule.type === 'expression' && rule.content) {
code += EOLChar + rule.content;
} else if (rule.type === 'branch') {
code += getBranchCode(rule.content) || '';
}
return getCode(rules, code);
}
function getCondition(expression) {
if (expression.indexOf('&&') !== -1) {
let definitions = expression.split('&&');
return {
type: 'and',
regard: _.filter(definitions, def => def[0] !== '!'),
disregard: _.map(_.filter(definitions, def => def[0] === '!'), def => def.substr(1))
};
} else if (expression.indexOf('||') !== -1) {
let definitions = expression.split('||');
return {
type: 'or',
regard: _.filter(definitions, def => def[0] !== '!'),
disregard: _.map(_.filter(definitions, def => def[0] === '!'), def => def.substr(1))
};
} else {
return expression;
}
}
function getRules(matches, stack = [{ content: [] }]) {
let current;
while (current = matches.shift()) {
let target;
let match;
if (match = current.match(ifRegex)) {
target = stack[0];
let branch = {
type: 'branch',
content: []
};
stack.unshift(branch);
let ifBlock = {
type: 'if',
condition: getCondition(match[1]),
content: []
};
stack.unshift(ifBlock);
branch.content.push(ifBlock);
target.content.push(branch);
target.content.push(ifBlock);
} else if (match = current.match(elifRegex)) {
stack.shift(); // out of if
target = stack[0];
let ifBlock = {
type: 'if',
condition: getCondition(match[1]),
content: []
};
stack.unshift(ifBlock);
target.content.push(ifBlock);
} else if (match = current.match(elseRegex)) {
stack.shift(); // out of if
target = stack[0];
let ifBlock = {
type: 'if',
content: []
};
stack.unshift(ifBlock);
target.content.push(ifBlock);
} else if (match = current.match(endifRegex)) {
stack.shift(); // out of if
stack.shift(); // out of branch
} else {
target = stack[0];
target.content.push({
type: 'expression',
content: current
});
}
}
return stack;
}
function PreprocessorLoader(content) {
let query = loaderUtils.parseQuery(this.query) || {};
definitions = query.definitions || [];
// Dynamically determine file end of line (EOL) marker.
// Use os.EOL if no EOL marker found.
// Note: ECMA 5.1 Specifications permits end slicing
// on empty strings
let lc = content.slice(-1);
if (lc === '\n') {
let slc = content.slice(-2);
if (slc === '\r') {
EOLChar = '\r\n';
} else {
EOLChar = '\n';
}
} else {
// Unknown EOL marker, use current OS EOL instead.
EOLChar = os.EOL;
}
// Trim removes EOL marker. Place after finding it.
if (!content.trim()) {
return content;
}
let matches = content.match(globalRegex);
// ignore empty matches
matches = _.filter(matches, match => match && match.length);
let rules = getRules(matches);
if (!rules) {
return content;
}
rules = rules.shift().content;
let code = getCode(rules);
if (this.cacheable) {
this.cacheable(true);
}
// Ensure modified code reconstitutes final EOL marker,
// as trim function removes it. ESLint is one of many
// programs to complain if final line EOL marker is
// missing.
content = code + EOLChar;
let variables = query.variables || [];
_.each(variables, (value, key) => {
let regex = new RegExp(`[$]{2}[{]{2}${key}[}]{2}`, 'g');
content = content.replace(regex, value)
});
return content;
}
module.exports = PreprocessorLoader;
|