1 | 'use strict';
|
2 |
|
3 | var _ = require('lodash'),
|
4 | listing = require('./listing'),
|
5 | minifyEscapes = listing.minifyEscapes;
|
6 |
|
7 |
|
8 | var minNames = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'.split('');
|
9 | _.each(minNames, function(value) { minNames.push(value + value); });
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 | function preprocess(source, options) {
|
21 | source = _.toString(source);
|
22 | options = _.cloneDeep(options);
|
23 |
|
24 |
|
25 | source = source.replace(/@(?:alias|category)\b.*/g, '');
|
26 |
|
27 | if (options.isTemplate) {
|
28 | return source;
|
29 | }
|
30 |
|
31 | source = source.replace(/(reEmptyString\w+\s*=)([\s\S]+?)(?=[,;]\n)/g, function(match, left, value) {
|
32 | return left + value.replace(/\s|\\n/g, '');
|
33 | });
|
34 |
|
35 |
|
36 | source = source.replace(/^( *)function template\b[\s\S]+?\n\1}/m, function(snippet) {
|
37 |
|
38 | snippet = snippet.replace(/^((?:[ "'$\w]+:)?\s*)"[^"\n\\]*?(?:\\.[^"\n\\]*?)*"|'[^'\n\\]*?(?:\\.[^'\n\\]*?)*'/gm, function(string, left) {
|
39 |
|
40 | if (left) {
|
41 | string = string.slice(left.length);
|
42 | }
|
43 |
|
44 | string = string.replace(/\[object |delete |else (?!{)|function | in | instanceof |return\s+["'$\w]|throw |typeof |use strict|var |# |(["'])(?:\\n| )\1|\\\\n|\\n|\s+/g, function(match) {
|
45 | return match == false || match == '\\n' ? '' : match;
|
46 | });
|
47 |
|
48 | return (left || '') + string;
|
49 | });
|
50 |
|
51 |
|
52 | snippet = snippet
|
53 | .replace('"__p += \'"', '"__p+=\'"')
|
54 | .replace('"\';\\n"', '"\';"');
|
55 |
|
56 |
|
57 | snippet = snippet.replace('";__p+=\'"', '";\\n__p+=\'"');
|
58 |
|
59 |
|
60 | return snippet.replace(/^( *var\s+sourceURL\s*=\s*)[\s\S]+?(?=;\n$)/m, function(match, left) {
|
61 | return left + "'sourceURL' in options ? '//# sourceURL=' + options.sourceURL + '\\n' : ''";
|
62 | });
|
63 | });
|
64 |
|
65 |
|
66 | (function() {
|
67 | var funcNames = [
|
68 | 'baseOrderBy',
|
69 | 'baseSortBy',
|
70 | 'compareMultiple',
|
71 | ];
|
72 |
|
73 | var props = [
|
74 | 'criteria',
|
75 | 'index',
|
76 | 'value'
|
77 | ];
|
78 |
|
79 |
|
80 | var snippets = source.match(RegExp('^( *)(?:var|function) +(?:' + funcNames.join('|') + ')\\b[\\s\\S]+?\\n\\1}', 'gm'));
|
81 | _.each(snippets, function(snippet) {
|
82 | var modified = snippet;
|
83 | _.each(props, function(prop, index) {
|
84 | var minName = minNames[index],
|
85 | reDotProp = RegExp('\\.' + prop + '\\b', 'g'),
|
86 | rePropName = RegExp("'" + prop + "'", 'g');
|
87 |
|
88 | modified = modified
|
89 | .replace(rePropName, "'" + minName + "'")
|
90 | .replace(reDotProp, "['" + minName + "']");
|
91 | });
|
92 |
|
93 |
|
94 | source = source.replace(snippet, function() {
|
95 | return modified;
|
96 | });
|
97 | });
|
98 | }());
|
99 |
|
100 |
|
101 |
|
102 | return source.replace(RegExp('(["\'])(?:(?!\\1)[^\\n\\\\]|\\\\.)*\\1|\\.(' + minifyEscapes.join('|') + ')\\b', 'g'), function(match, quote, prop) {
|
103 | return quote ? match : "['" + prop + "']";
|
104 | });
|
105 | }
|
106 |
|
107 | module.exports = preprocess;
|