rollup-plugin-replace.cjs.js 3.4 KB

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