preprocess.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439
  1. /*
  2. * preprocess
  3. * https://github.com/onehealth/preprocess
  4. *
  5. * Copyright (c) 2012 OneHealth Solutions, Inc.
  6. * Written by Jarrod Overson - http://jarrodoverson.com/
  7. * Licensed under the Apache 2.0 license.
  8. */
  9. 'use strict';
  10. exports.preprocess = preprocess;
  11. exports.preprocessFile = preprocessFile;
  12. exports.preprocessFileSync = preprocessFileSync;
  13. var path = require('path'),
  14. fs = require('fs'),
  15. os = require('os'),
  16. delim = require('./regexrules'),
  17. XRegExp = require('xregexp');
  18. function preprocessFile(srcFile, destFile, context, callback, options) {
  19. options = getOptionsForFile(srcFile, options);
  20. context.src = srcFile;
  21. fs.readFile(srcFile, function (err, data) {
  22. if (err) return callback(err, data);
  23. var parsed = preprocess(data, context, options);
  24. fs.writeFile(destFile, parsed, callback);
  25. });
  26. }
  27. function preprocessFileSync(srcFile, destFile, context, options) {
  28. options = getOptionsForFile(srcFile, options);
  29. context.src = srcFile;
  30. var data = fs.readFileSync(srcFile);
  31. var parsed = preprocess(data, context, options);
  32. return fs.writeFileSync(destFile, parsed);
  33. }
  34. function getOptionsForFile(srcFile, options) {
  35. options = options || {};
  36. options.srcDir = options.srcDir || path.dirname(srcFile);
  37. options.type = options.type || getExtension(srcFile);
  38. return options;
  39. }
  40. function getExtension(filename) {
  41. var ext = path.extname(filename||'').split('.');
  42. return ext[ext.length - 1];
  43. }
  44. function preprocess(src, context, typeOrOptions) {
  45. src = src.toString();
  46. context = context || process.env;
  47. // default values
  48. var options = {
  49. fileNotFoundSilentFail: false,
  50. srcDir: process.cwd(),
  51. srcEol: getEolType(src),
  52. type: delim['html']
  53. };
  54. // needed for backward compatibility with 2.x.x series
  55. if (typeof typeOrOptions === 'string') {
  56. typeOrOptions = {
  57. type: typeOrOptions
  58. };
  59. }
  60. // needed for backward compatibility with 2.x.x series
  61. if (typeof context.srcDir === "string") {
  62. typeOrOptions = typeOrOptions || {};
  63. typeOrOptions.srcDir = context.srcDir;
  64. }
  65. if (typeOrOptions && typeof typeOrOptions === 'object') {
  66. options.srcDir = typeOrOptions.srcDir || options.srcDir;
  67. options.fileNotFoundSilentFail = typeOrOptions.fileNotFoundSilentFail || options.fileNotFoundSilentFail;
  68. options.srcEol = typeOrOptions.srcEol || options.srcEol;
  69. options.type = delim[typeOrOptions.type] || options.type;
  70. }
  71. context = copy(context);
  72. return preprocessor(src, context, options);
  73. }
  74. function preprocessor(src, context, opts, noRestoreEol) {
  75. src = normalizeEol(src);
  76. var rv = src;
  77. // 不支持该语法,无需执行,大字符串时,该正则性能极低(比如包含较大base64时)(https://github.com/dcloudio/uni-app/issues/4661)
  78. // rv = replace(rv, opts.type.include, processIncludeDirective.bind(null, false, context, opts));
  79. // if (opts.type.extend) {
  80. // rv = replaceRecursive(rv, opts.type.extend, function(startMatches, endMatches, include, recurse) {
  81. // var file = (startMatches[1] || '').trim();
  82. // var extendedContext = copy(context);
  83. // var extendedOpts = copy(opts);
  84. // extendedContext.src = path.join(opts.srcDir, file);
  85. // extendedOpts.srcDir = path.dirname(extendedContext.src);
  86. // var fileContents = getFileContents(extendedContext.src, opts.fileNotFoundSilentFail, context.src);
  87. // if (fileContents.error) {
  88. // return fileContents.contents;
  89. // }
  90. // var extendedSource = preprocessor(fileContents.contents, extendedContext, extendedOpts, true).trim();
  91. // if (extendedSource) {
  92. // include = include.replace(/^\n?|\n?$/g, '');
  93. // return replace(extendedSource, opts.type.extendable, recurse(include));
  94. // } else {
  95. // return '';
  96. // }
  97. // });
  98. // }
  99. // if (opts.type.foreach) {
  100. // rv = replaceRecursive(rv, opts.type.foreach, function(startMatches, endMatches, include, recurse) {
  101. // var variable = (startMatches[1] || '').trim();
  102. // var forParams = variable.split(' ');
  103. // if (forParams.length === 3) {
  104. // var contextVar = forParams[2];
  105. // var arrString = getDeepPropFromObj(context, contextVar);
  106. // var eachArr;
  107. // if (arrString.match(/\{(.*)\}/)) {
  108. // eachArr = JSON.parse(arrString);
  109. // } else if (arrString.match(/\[(.*)\]/)) {
  110. // eachArr = arrString.slice(1, -1);
  111. // eachArr = eachArr.split(',');
  112. // eachArr = eachArr.map(function(arrEntry){
  113. // return arrEntry.replace(/\s*(['"])(.*)\1\s*/, '$2');
  114. // });
  115. // } else {
  116. // eachArr = arrString.split(',');
  117. // }
  118. // var replaceToken = new RegExp(XRegExp.escape(forParams[0]), 'g');
  119. // var recursedInclude = recurse(include);
  120. // return Object.keys(eachArr).reduce(function(stringBuilder, arrKey){
  121. // var arrEntry = eachArr[arrKey];
  122. // return stringBuilder + recursedInclude.replace(replaceToken, arrEntry);
  123. // }, '');
  124. // } else {
  125. // return '';
  126. // }
  127. // });
  128. // }
  129. // if (opts.type.exclude) {
  130. // rv = replaceRecursive(rv, opts.type.exclude, function(startMatches, endMatches, include, recurse){
  131. // var test = (startMatches[1] || '').trim();
  132. // return testPasses(test,context) ? '' : recurse(include);
  133. // });
  134. // }
  135. if (opts.type.if) {
  136. rv = replaceRecursive(rv, opts.type.if, function (startMatches, endMatches, include, recurse) {
  137. // I need to recurse first, so I don't catch "inner" else-directives
  138. var recursed = recurse(include);
  139. // look for the first else-directive
  140. var matches = opts.type.else && recursed.match(new RegExp(opts.type.else));
  141. var match = (matches || [""])[0];
  142. var index = match ? recursed.indexOf(match) : recursed.length;
  143. var ifBlock = recursed.substring(0, index);
  144. var elseBlock = recursed.substring(index + match.length); // empty string if no else-directive
  145. var variant = startMatches[1];
  146. var test = (startMatches[2] || '').trim();
  147. // fixed by xxxxxx
  148. var startLine = padContent(startMatches.input)
  149. var endLine = padContent(endMatches.input)
  150. switch(variant) {
  151. case 'if':
  152. case 'ifdef': // fixed by xxxxxx
  153. return testPasses(test, context) ? (startLine + ifBlock + endLine) : (startLine + padContent(ifBlock) + padContent(match || '') + elseBlock + endLine);
  154. case 'ifndef':
  155. return !testPasses(test, context) ? (startLine + ifBlock + endLine) : (startLine + padContent(ifBlock) + padContent(match || '') + elseBlock + endLine);
  156. // case 'ifdef':
  157. // return typeof getDeepPropFromObj(context, test) !== 'undefined' ? ifBlock : elseBlock;
  158. // case 'ifndef':
  159. // return typeof getDeepPropFromObj(context, test) === 'undefined' ? ifBlock : elseBlock;
  160. default:
  161. throw new Error('Unknown if variant ' + variant + '.');
  162. }
  163. });
  164. }
  165. // rv = replace(rv, opts.type.echo, function (match, variable) {
  166. // variable = (variable || '').trim();
  167. // // if we are surrounded by quotes, echo as a string
  168. // var stringMatch = variable.match(/^(['"])(.*)\1$/);
  169. // if (stringMatch) return stringMatch[2];
  170. // var arrString = getDeepPropFromObj(context, variable);
  171. // return typeof arrString !== 'undefined' ? arrString : '';
  172. // });
  173. // rv = replace(rv, opts.type.exec, function (match, name, value) {
  174. // name = (name || '').trim();
  175. // value = value || '';
  176. // var params = value.split(',');
  177. // var stringRegex = /^['"](.*)['"]$/;
  178. // params = params.map(function(param){
  179. // param = param.trim();
  180. // if (stringRegex.test(param)) { // handle string parameter
  181. // return param.replace(stringRegex, '$1');
  182. // } else { // handle variable parameter
  183. // return getDeepPropFromObj(context, param);
  184. // }
  185. // });
  186. // var fn = getDeepPropFromObj(context, name);
  187. // if (!fn || typeof fn !== 'function') return '';
  188. // return fn.apply(context, params);
  189. // });
  190. // rv = replace(rv, opts.type['include-static'], processIncludeDirective.bind(null, true, context, opts));
  191. if (!noRestoreEol) {
  192. rv = restoreEol(rv, opts.srcEol);
  193. }
  194. return rv;
  195. }
  196. function getEolType(source) {
  197. var eol;
  198. var foundEolTypeCnt = 0;
  199. if (source.indexOf('\r\n') >= 0) {
  200. eol = '\r\n';
  201. foundEolTypeCnt++;
  202. }
  203. if (/\r[^\n]/.test(source)) {
  204. eol = '\r';
  205. foundEolTypeCnt++;
  206. }
  207. if (/[^\r]\n/.test(source)) {
  208. eol = '\n';
  209. foundEolTypeCnt++;
  210. }
  211. if (eol == null || foundEolTypeCnt > 1) {
  212. eol = os.EOL;
  213. }
  214. return eol;
  215. }
  216. function normalizeEol(source, indent) {
  217. // only process any kind of EOL if indentation has to be added, otherwise replace only non \n EOLs
  218. if (indent) {
  219. source = source.replace(/(?:\r?\n)|\r/g, '\n' + indent);
  220. } else {
  221. source = source.replace(/(?:\r\n)|\r/g, '\n');
  222. }
  223. return source;
  224. }
  225. function restoreEol(normalizedSource, originalEol) {
  226. if (originalEol !== '\n') {
  227. normalizedSource = normalizedSource.replace(/\n/g, originalEol);
  228. }
  229. return normalizedSource;
  230. }
  231. function replace(rv, rule, processor) {
  232. var isRegex = typeof rule === 'string' || rule instanceof RegExp;
  233. var isArray = Array.isArray(rule);
  234. if (isRegex) {
  235. rule = [new RegExp(rule,'gmi')];
  236. } else if (isArray) {
  237. rule = rule.map(function(subRule){
  238. return new RegExp(subRule,'gmi');
  239. });
  240. } else {
  241. throw new Error('Rule must be a String, a RegExp, or an Array.');
  242. }
  243. return rule.reduce(function(rv, rule){
  244. return rv.replace(rule, processor);
  245. }, rv);
  246. }
  247. function replaceRecursive(rv, rule, processor) {
  248. if(!rule.start || !rule.end) {
  249. throw new Error('Recursive rule must have start and end.');
  250. }
  251. var startRegex = new RegExp(rule.start, 'mi');
  252. var endRegex = new RegExp(rule.end, 'mi');
  253. function matchReplacePass(content) {
  254. var matches = XRegExp.matchRecursive(content, rule.start, rule.end, 'gmi', {
  255. valueNames: ['between', 'left', 'match', 'right']
  256. });
  257. var matchGroup = {
  258. left: null,
  259. match: null,
  260. right: null
  261. };
  262. return matches.reduce(function (builder, match) {
  263. switch(match.name) {
  264. case 'between':
  265. builder += match.value;
  266. break;
  267. case 'left':
  268. matchGroup.left = startRegex.exec(match.value);
  269. break;
  270. case 'match':
  271. matchGroup.match = match.value;
  272. break;
  273. case 'right':
  274. matchGroup.right = endRegex.exec(match.value);
  275. builder += processor(matchGroup.left, matchGroup.right, matchGroup.match, matchReplacePass);
  276. break;
  277. }
  278. return builder;
  279. }, '');
  280. }
  281. return matchReplacePass(rv);
  282. }
  283. function processIncludeDirective(isStatic, context, opts, match, linePrefix, file) {
  284. file = (file || '').trim();
  285. var indent = linePrefix.replace(/\S/g, ' ');
  286. var includedContext = copy(context);
  287. var includedOpts = copy(opts);
  288. includedContext.src = path.join(opts.srcDir,file);
  289. includedOpts.srcDir = path.dirname(includedContext.src);
  290. var fileContents = getFileContents(includedContext.src, opts.fileNotFoundSilentFail, context.src);
  291. if (fileContents.error) {
  292. return linePrefix + fileContents.contents;
  293. }
  294. var includedSource = fileContents.contents;
  295. if (isStatic) {
  296. includedSource = fileContents.contents;
  297. } else {
  298. includedSource = preprocessor(fileContents.contents, includedContext, includedOpts, true);
  299. }
  300. includedSource = normalizeEol(includedSource, indent);
  301. if (includedSource) {
  302. return linePrefix + includedSource;
  303. } else {
  304. return linePrefix;
  305. }
  306. }
  307. function getTestTemplate(test) {
  308. /*jshint evil:true*/
  309. test = test || 'true';
  310. test = test.trim();
  311. // force single equals replacement
  312. // fixed by xxxxxx 不替换,会影响 >= 等判断
  313. // test = test.replace(/([^=!])=([^=])/g, '$1==$2');
  314. // fixed by xxxxxx
  315. test = test.replace(/-/g, '_')
  316. return new Function("context", "with (context||{}){ return ( " + test + " ); }");
  317. }
  318. // fixed by xxxxxx
  319. function testPasses(test, context) {
  320. var testFn = getTestTemplate(test)
  321. try {
  322. return testFn(context, getDeepPropFromObj)
  323. } catch (e) {}
  324. return false
  325. }
  326. function getFileContents(path, failSilent, requesterPath) {
  327. try {
  328. fs.statSync(path);
  329. } catch (e) {
  330. if (failSilent) {
  331. return {error: true, contents: path + ' not found!'};
  332. } else {
  333. var errMsg = path;
  334. errMsg = requesterPath ? errMsg + ' requested from ' + requesterPath : errMsg;
  335. errMsg += ' not found!';
  336. throw new Error(errMsg);
  337. }
  338. }
  339. return {error: false, contents: fs.readFileSync(path).toString()};
  340. }
  341. function copy(obj) {
  342. return Object.keys(obj).reduce(function (copyObj, objKey) {
  343. copyObj[objKey] = obj[objKey];
  344. return copyObj;
  345. }, {});
  346. }
  347. function getDeepPropFromObj(obj, propPath) {
  348. propPath.replace(/\[([^\]+?])\]/g, '.$1');
  349. propPath = propPath.split('.');
  350. // fast path, no need to loop if structurePath contains only a single segment
  351. if (propPath.length === 1) {
  352. return obj[propPath[0]];
  353. }
  354. // loop only as long as possible (no exceptions for null/undefined property access)
  355. propPath.some(function (pathSegment) {
  356. obj = obj[pathSegment];
  357. return (obj == null);
  358. });
  359. return obj;
  360. }
  361. // fixed by xxxxxx
  362. const splitRE = /\r?\n/g
  363. function padContent(content) {
  364. return Array(content.split(splitRE).length).join('\n')
  365. }