rollup-plugin-replace.es.js 3.2 KB

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