Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Transform only works on first instance of file #52

Open
jamestowers opened this issue May 12, 2021 · 1 comment
Open

Transform only works on first instance of file #52

jamestowers opened this issue May 12, 2021 · 1 comment

Comments

@jamestowers
Copy link

jamestowers commented May 12, 2021

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 jamestowers changed the title Transform only seems to work once per file Transform only works on first instance of file May 12, 2021
@elysium001
Copy link

@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 },
  ],
}),

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants