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