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

Modifying buffer output in onEnd callback doesn't update text output #2423

Closed
ArnaudBarre opened this issue Jul 30, 2022 · 5 comments
Closed

Comments

@ArnaudBarre
Copy link

Following #1792

Repro

version: 0.14.51

const { build } = require("esbuild");

build({
  write: false,
  stdin: { contents: "const a = 1;" },
  plugins: [
    {
      name: "updateOutput",
      setup: (build) => {
        build.onEnd((result) => {
          const file = result.outputFiles[0];
          const transformed = file.text.replace("1", "2");
          file.contents = Buffer.from(transformed);
          // Object.defineProperty(file, "text", { value: transformed });
        });
      },
    },
  ],
}).then((result) => {
  const file = result.outputFiles[0];
  console.log(file.contents.toString().trim(), file.text.trim());
});

Result

Logs const a = 2; const a = 1;

Expected result

Logs const a = 2; const a = 2;

Workaround

Uncommenting the defineProperty line gives the expected result.

@evanw
Copy link
Owner

evanw commented Jul 30, 2022

This is not a feature. There is nothing in the documentation that says this is supposed to work. I recommend calling Object.defineProperty as you suggested.

@ArnaudBarre
Copy link
Author

ArnaudBarre commented Jul 30, 2022

I agree this not documented that it should work as simply as this, but this is what I expected when reading It can modify the build result in this section and seeing that text was a getter.

Would you accept a PR for:

  1. Adding a small paragraph to document how to modify build result (i.e. setting both text and contents like above)
  2. Update convertOutputFiles to
function convertOutputFiles({ path, contents }: protocol.BuildOutputFile): types.OutputFile {
  let text: string | null = null;
  let mutableContents = contents;
  return {
    path,
    get contents() {
      return mutableContents;
    },
    set contents(value) {
      mutableContents = value;
      text = null
    },
    get text() {
      if (text === null) text = protocol.decodeUTF8(mutableContents);
      return text;
    },
    set text(value) {
      text = value;
      mutableContents = protocol.encodeUTF8(value)
    },
  }
}

@evanw evanw closed this as completed in 2f6c899 Jul 31, 2022
@ArnaudBarre
Copy link
Author

Thanks for the change. I understand the performance reason for not having a setter for text, but this not obvious at first that this is only a getter. This could be made clearer by adding a small precision in the doc like: It can modify the build result (by mutating `contents`) and adding a readonly modifier for text on the TS types.

@evanw
Copy link
Owner

evanw commented Jul 31, 2022

Changing the type of text on the TS types makes sense to do, but people would likely complain if I did that because it's a backwards-incompatible type change. I'll try to remember to do that with the next breaking change release.

@ArnaudBarre
Copy link
Author

Having a TS error because some part of your code didn't make sense is not a breaking change IMO because it doesn't break you code (.i.e nothing change at runtime) and you can just add an ts-ignore comment. But I understand that for a project of that size there is always people complaining

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