| [9af201e] | 1 | 'use strict';
|
|---|
| 2 |
|
|---|
| 3 | var MagicString = require('magic-string');
|
|---|
| 4 | var pluginutils = require('@rollup/pluginutils');
|
|---|
| 5 |
|
|---|
| 6 | function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
|
|---|
| 7 |
|
|---|
| 8 | var MagicString__default = /*#__PURE__*/_interopDefaultLegacy(MagicString);
|
|---|
| 9 |
|
|---|
| 10 | function escape(str) {
|
|---|
| 11 | return str.replace(/[-[\]/{}()*+?.\\^$|]/g, '\\$&');
|
|---|
| 12 | }
|
|---|
| 13 |
|
|---|
| 14 | function ensureFunction(functionOrValue) {
|
|---|
| 15 | if (typeof functionOrValue === 'function') { return functionOrValue; }
|
|---|
| 16 | return function () { return functionOrValue; };
|
|---|
| 17 | }
|
|---|
| 18 |
|
|---|
| 19 | function longest(a, b) {
|
|---|
| 20 | return b.length - a.length;
|
|---|
| 21 | }
|
|---|
| 22 |
|
|---|
| 23 | function getReplacements(options) {
|
|---|
| 24 | if (options.values) {
|
|---|
| 25 | return Object.assign({}, options.values);
|
|---|
| 26 | }
|
|---|
| 27 | var values = Object.assign({}, options);
|
|---|
| 28 | delete values.delimiters;
|
|---|
| 29 | delete values.include;
|
|---|
| 30 | delete values.exclude;
|
|---|
| 31 | delete values.sourcemap;
|
|---|
| 32 | delete values.sourceMap;
|
|---|
| 33 | return values;
|
|---|
| 34 | }
|
|---|
| 35 |
|
|---|
| 36 | function mapToFunctions(object) {
|
|---|
| 37 | return Object.keys(object).reduce(function (fns, key) {
|
|---|
| 38 | var functions = Object.assign({}, fns);
|
|---|
| 39 | functions[key] = ensureFunction(object[key]);
|
|---|
| 40 | return functions;
|
|---|
| 41 | }, {});
|
|---|
| 42 | }
|
|---|
| 43 |
|
|---|
| 44 | function replace(options) {
|
|---|
| 45 | if ( options === void 0 ) options = {};
|
|---|
| 46 |
|
|---|
| 47 | var filter = pluginutils.createFilter(options.include, options.exclude);
|
|---|
| 48 | var delimiters = options.delimiters;
|
|---|
| 49 | var preventAssignment = options.preventAssignment;
|
|---|
| 50 | var functionValues = mapToFunctions(getReplacements(options));
|
|---|
| 51 | var keys = Object.keys(functionValues).sort(longest).map(escape);
|
|---|
| 52 | var lookahead = preventAssignment ? '(?!\\s*=[^=])' : '';
|
|---|
| 53 | var pattern = delimiters
|
|---|
| 54 | ? new RegExp(
|
|---|
| 55 | ((escape(delimiters[0])) + "(" + (keys.join('|')) + ")" + (escape(delimiters[1])) + lookahead),
|
|---|
| 56 | 'g'
|
|---|
| 57 | )
|
|---|
| 58 | : new RegExp(("\\b(" + (keys.join('|')) + ")\\b" + lookahead), 'g');
|
|---|
| 59 |
|
|---|
| 60 | return {
|
|---|
| 61 | name: 'replace',
|
|---|
| 62 |
|
|---|
| 63 | buildStart: function buildStart() {
|
|---|
| 64 | if (![true, false].includes(preventAssignment)) {
|
|---|
| 65 | this.warn({
|
|---|
| 66 | message:
|
|---|
| 67 | "@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`."
|
|---|
| 68 | });
|
|---|
| 69 | }
|
|---|
| 70 | },
|
|---|
| 71 |
|
|---|
| 72 | renderChunk: function renderChunk(code, chunk) {
|
|---|
| 73 | var id = chunk.fileName;
|
|---|
| 74 | if (!keys.length) { return null; }
|
|---|
| 75 | if (!filter(id)) { return null; }
|
|---|
| 76 | return executeReplacement(code, id);
|
|---|
| 77 | },
|
|---|
| 78 |
|
|---|
| 79 | transform: function transform(code, id) {
|
|---|
| 80 | if (!keys.length) { return null; }
|
|---|
| 81 | if (!filter(id)) { return null; }
|
|---|
| 82 | return executeReplacement(code, id);
|
|---|
| 83 | }
|
|---|
| 84 | };
|
|---|
| 85 |
|
|---|
| 86 | function executeReplacement(code, id) {
|
|---|
| 87 | var magicString = new MagicString__default['default'](code);
|
|---|
| 88 | if (!codeHasReplacements(code, id, magicString)) {
|
|---|
| 89 | return null;
|
|---|
| 90 | }
|
|---|
| 91 |
|
|---|
| 92 | var result = { code: magicString.toString() };
|
|---|
| 93 | if (isSourceMapEnabled()) {
|
|---|
| 94 | result.map = magicString.generateMap({ hires: true });
|
|---|
| 95 | }
|
|---|
| 96 | return result;
|
|---|
| 97 | }
|
|---|
| 98 |
|
|---|
| 99 | function codeHasReplacements(code, id, magicString) {
|
|---|
| 100 | var result = false;
|
|---|
| 101 | var match;
|
|---|
| 102 |
|
|---|
| 103 | // eslint-disable-next-line no-cond-assign
|
|---|
| 104 | while ((match = pattern.exec(code))) {
|
|---|
| 105 | result = true;
|
|---|
| 106 |
|
|---|
| 107 | var start = match.index;
|
|---|
| 108 | var end = start + match[0].length;
|
|---|
| 109 | var replacement = String(functionValues[match[1]](id));
|
|---|
| 110 | magicString.overwrite(start, end, replacement);
|
|---|
| 111 | }
|
|---|
| 112 | return result;
|
|---|
| 113 | }
|
|---|
| 114 |
|
|---|
| 115 | function isSourceMapEnabled() {
|
|---|
| 116 | return options.sourceMap !== false && options.sourcemap !== false;
|
|---|
| 117 | }
|
|---|
| 118 | }
|
|---|
| 119 |
|
|---|
| 120 | module.exports = replace;
|
|---|