vladshcherbin / rollup-plugin-copy

Copy files and folders using Rollup

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Transform only works on first instance of file

jamestowers opened this issue · comments

I have a file with multiple instances of the same placeholder text but only the first is transformed, chaining multiple instances fixes it but I wonder if it's possible to replace all instances in one run?

// file to copy + transform
<!DOCTYPE html>
<html lang="en">
  <head></head>
  <body>
    <div>__EXT_VERSION__</div> <!-- Without the chaining below, only this one is transformed -->
    <div>__EXT_VERSION__</div>
    <div>__EXT_VERSION__</div>
  </body>
</html>
function setVersion(contents, filename) {
  return contents.toString()
   // You have to call this as many times as __EXT_VERSION__ appears in a file
    .replace('__EXT_VERSION__', pkg.version)
    .replace('__EXT_VERSION__', pkg.version)
    .replace('__EXT_VERSION__', pkg.version)
);
}

...
copy({
  targets: [
    { src: "index.html", dest: "./", transform: setVersion },
  ],
}),

@jamestowers , I think you would need to add the global option and rewrite as a regex like so:

function setVersion(contents, filename) {
  return contents.toString().replace(/__EXT_VERSION__/g, pkg.version)
);
}

...
copy({
  targets: [
    { src: "index.html", dest: "./", transform: setVersion },
  ],
}),