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