UNPKG

5.09 kBJavaScriptView Raw
1import MagicString from 'magic-string';
2import { createFilter } from '@rollup/pluginutils';
3
4function escape(str) {
5 return str.replace(/[-[\]/{}()*+?.\\^$|]/g, '\\$&');
6}
7
8function ensureFunction(functionOrValue) {
9 if (typeof functionOrValue === 'function') { return functionOrValue; }
10 return function () { return functionOrValue; };
11}
12
13function longest(a, b) {
14 return b.length - a.length;
15}
16
17function getReplacements(options) {
18 if (options.values) {
19 return Object.assign({}, options.values);
20 }
21 var values = Object.assign({}, options);
22 delete values.delimiters;
23 delete values.include;
24 delete values.exclude;
25 delete values.sourcemap;
26 delete values.sourceMap;
27 delete values.objectGuards;
28 return values;
29}
30
31function mapToFunctions(object) {
32 return Object.keys(object).reduce(function (fns, key) {
33 var functions = Object.assign({}, fns);
34 functions[key] = ensureFunction(object[key]);
35 return functions;
36 }, {});
37}
38
39var objKeyRegEx =
40 /^([_$a-zA-Z\xA0-\uFFFF][_$a-zA-Z0-9\xA0-\uFFFF]*)(\.([_$a-zA-Z\xA0-\uFFFF][_$a-zA-Z0-9\xA0-\uFFFF]*))+$/;
41function expandTypeofReplacements(replacements) {
42 Object.keys(replacements).forEach(function (key) {
43 var objMatch = key.match(objKeyRegEx);
44 if (!objMatch) { return; }
45 var dotIndex = objMatch[1].length;
46 var lastIndex = 0;
47 do {
48 // eslint-disable-next-line no-param-reassign
49 replacements[("typeof " + (key.slice(lastIndex, dotIndex)) + " ===")] = '"object" ===';
50 // eslint-disable-next-line no-param-reassign
51 replacements[("typeof " + (key.slice(lastIndex, dotIndex)) + " !==")] = '"object" !==';
52 // eslint-disable-next-line no-param-reassign
53 replacements[("typeof " + (key.slice(lastIndex, dotIndex)) + "===")] = '"object"===';
54 // eslint-disable-next-line no-param-reassign
55 replacements[("typeof " + (key.slice(lastIndex, dotIndex)) + "!==")] = '"object"!==';
56 // eslint-disable-next-line no-param-reassign
57 replacements[("typeof " + (key.slice(lastIndex, dotIndex)) + " ==")] = '"object" ===';
58 // eslint-disable-next-line no-param-reassign
59 replacements[("typeof " + (key.slice(lastIndex, dotIndex)) + " !=")] = '"object" !==';
60 // eslint-disable-next-line no-param-reassign
61 replacements[("typeof " + (key.slice(lastIndex, dotIndex)) + "==")] = '"object"===';
62 // eslint-disable-next-line no-param-reassign
63 replacements[("typeof " + (key.slice(lastIndex, dotIndex)) + "!=")] = '"object"!==';
64 lastIndex = dotIndex + 1;
65 dotIndex = key.indexOf('.', lastIndex);
66 } while (dotIndex !== -1);
67 });
68}
69
70function replace(options) {
71 if ( options === void 0 ) options = {};
72
73 var filter = createFilter(options.include, options.exclude);
74 var delimiters = options.delimiters; if ( delimiters === void 0 ) delimiters = ['\\b', '\\b(?!\\.)'];
75 var preventAssignment = options.preventAssignment;
76 var objectGuards = options.objectGuards;
77 var replacements = getReplacements(options);
78 if (objectGuards) { expandTypeofReplacements(replacements); }
79 var functionValues = mapToFunctions(replacements);
80 var keys = Object.keys(functionValues).sort(longest).map(escape);
81 var lookahead = preventAssignment ? '(?!\\s*=[^=])' : '';
82 var pattern = new RegExp(
83 ((delimiters[0]) + "(" + (keys.join('|')) + ")" + (delimiters[1]) + lookahead),
84 'g'
85 );
86
87 return {
88 name: 'replace',
89
90 buildStart: function buildStart() {
91 if (![true, false].includes(preventAssignment)) {
92 this.warn({
93 message:
94 "@rollup/plugin-replace: 'preventAssignment' currently defaults to false. It is recommended to set this option to `true`, as the next major version will default this option to `true`."
95 });
96 }
97 },
98
99 renderChunk: function renderChunk(code, chunk) {
100 var id = chunk.fileName;
101 if (!keys.length) { return null; }
102 if (!filter(id)) { return null; }
103 return executeReplacement(code, id);
104 },
105
106 transform: function transform(code, id) {
107 if (!keys.length) { return null; }
108 if (!filter(id)) { return null; }
109 return executeReplacement(code, id);
110 }
111 };
112
113 function executeReplacement(code, id) {
114 var magicString = new MagicString(code);
115 if (!codeHasReplacements(code, id, magicString)) {
116 return null;
117 }
118
119 var result = { code: magicString.toString() };
120 if (isSourceMapEnabled()) {
121 result.map = magicString.generateMap({ hires: true });
122 }
123 return result;
124 }
125
126 function codeHasReplacements(code, id, magicString) {
127 var result = false;
128 var match;
129
130 // eslint-disable-next-line no-cond-assign
131 while ((match = pattern.exec(code))) {
132 result = true;
133
134 var start = match.index;
135 var end = start + match[0].length;
136 var replacement = String(functionValues[match[1]](id));
137 magicString.overwrite(start, end, replacement);
138 }
139 return result;
140 }
141
142 function isSourceMapEnabled() {
143 return options.sourceMap !== false && options.sourcemap !== false;
144 }
145}
146
147export { replace as default };
148//# sourceMappingURL=index.js.map