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