| 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 function () { 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 | 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 | return values;
|
|---|
| 28 | }
|
|---|
| 29 |
|
|---|
| 30 | function mapToFunctions(object) {
|
|---|
| 31 | return Object.keys(object).reduce(function (fns, key) {
|
|---|
| 32 | var functions = Object.assign({}, fns);
|
|---|
| 33 | functions[key] = ensureFunction(object[key]);
|
|---|
| 34 | return functions;
|
|---|
| 35 | }, {});
|
|---|
| 36 | }
|
|---|
| 37 |
|
|---|
| 38 | function replace(options) {
|
|---|
| 39 | if ( options === void 0 ) options = {};
|
|---|
| 40 |
|
|---|
| 41 | var filter = createFilter(options.include, options.exclude);
|
|---|
| 42 | var delimiters = options.delimiters;
|
|---|
| 43 | var preventAssignment = options.preventAssignment;
|
|---|
| 44 | var functionValues = mapToFunctions(getReplacements(options));
|
|---|
| 45 | var keys = Object.keys(functionValues).sort(longest).map(escape);
|
|---|
| 46 | var lookahead = preventAssignment ? '(?!\\s*=[^=])' : '';
|
|---|
| 47 | var pattern = delimiters
|
|---|
| 48 | ? new RegExp(
|
|---|
| 49 | ((escape(delimiters[0])) + "(" + (keys.join('|')) + ")" + (escape(delimiters[1])) + lookahead),
|
|---|
| 50 | 'g'
|
|---|
| 51 | )
|
|---|
| 52 | : new RegExp(("\\b(" + (keys.join('|')) + ")\\b" + lookahead), 'g');
|
|---|
| 53 |
|
|---|
| 54 | return {
|
|---|
| 55 | name: 'replace',
|
|---|
| 56 |
|
|---|
| 57 | buildStart: function buildStart() {
|
|---|
| 58 | if (![true, false].includes(preventAssignment)) {
|
|---|
| 59 | this.warn({
|
|---|
| 60 | message:
|
|---|
| 61 | "@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`."
|
|---|
| 62 | });
|
|---|
| 63 | }
|
|---|
| 64 | },
|
|---|
| 65 |
|
|---|
| 66 | renderChunk: function renderChunk(code, chunk) {
|
|---|
| 67 | var id = chunk.fileName;
|
|---|
| 68 | if (!keys.length) { return null; }
|
|---|
| 69 | if (!filter(id)) { return null; }
|
|---|
| 70 | return executeReplacement(code, id);
|
|---|
| 71 | },
|
|---|
| 72 |
|
|---|
| 73 | transform: function transform(code, id) {
|
|---|
| 74 | if (!keys.length) { return null; }
|
|---|
| 75 | if (!filter(id)) { return null; }
|
|---|
| 76 | return executeReplacement(code, id);
|
|---|
| 77 | }
|
|---|
| 78 | };
|
|---|
| 79 |
|
|---|
| 80 | function executeReplacement(code, id) {
|
|---|
| 81 | var magicString = new MagicString(code);
|
|---|
| 82 | if (!codeHasReplacements(code, id, magicString)) {
|
|---|
| 83 | return null;
|
|---|
| 84 | }
|
|---|
| 85 |
|
|---|
| 86 | var result = { code: magicString.toString() };
|
|---|
| 87 | if (isSourceMapEnabled()) {
|
|---|
| 88 | result.map = magicString.generateMap({ hires: true });
|
|---|
| 89 | }
|
|---|
| 90 | return result;
|
|---|
| 91 | }
|
|---|
| 92 |
|
|---|
| 93 | function codeHasReplacements(code, id, magicString) {
|
|---|
| 94 | var result = false;
|
|---|
| 95 | var match;
|
|---|
| 96 |
|
|---|
| 97 | // eslint-disable-next-line no-cond-assign
|
|---|
| 98 | while ((match = pattern.exec(code))) {
|
|---|
| 99 | result = true;
|
|---|
| 100 |
|
|---|
| 101 | var start = match.index;
|
|---|
| 102 | var end = start + match[0].length;
|
|---|
| 103 | var replacement = String(functionValues[match[1]](id));
|
|---|
| 104 | magicString.overwrite(start, end, replacement);
|
|---|
| 105 | }
|
|---|
| 106 | return result;
|
|---|
| 107 | }
|
|---|
| 108 |
|
|---|
| 109 | function isSourceMapEnabled() {
|
|---|
| 110 | return options.sourceMap !== false && options.sourcemap !== false;
|
|---|
| 111 | }
|
|---|
| 112 | }
|
|---|
| 113 |
|
|---|
| 114 | export default replace;
|
|---|