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

[Plugin] onEnd callbacks modify the build result #1792

Closed
yanm1ng opened this issue Nov 22, 2021 · 3 comments
Closed

[Plugin] onEnd callbacks modify the build result #1792

yanm1ng opened this issue Nov 22, 2021 · 3 comments

Comments

@yanm1ng
Copy link

yanm1ng commented Nov 22, 2021

I used end callback to modify the build result,it works,but I can't return the new result。

function myPlugin(): Plugin {
  return {
    name: 'my-plugin',
    setup(build: PluginBuild) {
      const options = build.initialOptions;
      options.write = false;

      build.onEnd(async ({ errors, outputFiles }) => {
        if (!errors.length && outputFiles?.length) {
          await Promise.all(outputFiles.map(async file => {
                // some logic to modify output contents...
          }));
        } else {
          console.error(errors);
        }
      });
    },
  };
}

const result = await esbuild({
      entryPoints: [ path.join(__dirname, './entry.js')],
      outdir: path.join(__dirname, 'dist'),
      plugins: [ myPlugin() ]
});

for (let out of result.outputFiles) {
     // still original build result
     console.log(out.path, out.text);
}

also onEnd function type only support void or Promise<void>

image

@evanw
Copy link
Owner

evanw commented Nov 24, 2021

Well text is a lazily-evaluated getter instead of a normal property so you have to use Object.defineProperty(file, 'text', { value: 'something' }) instead of file.text = 'something' but changing it should work fine (I just tried it myself). You can also just mutate the outputFiles property on the result argument directly instead if you want. The onEnd callback doesn't have a return value because the result object is provided as an argument and you can fully modify any aspect of it before you return it.

@yanm1ng
Copy link
Author

yanm1ng commented Nov 24, 2021

I also found text is a getter before you reply. I will try to modify the result object.
Thank you for your reply anyway ❤️

@yanm1ng yanm1ng closed this as completed Nov 24, 2021
@eric-hemasystems
Copy link

FYI. For anybody that comes across this in the future.

I wasn't able to modify my build result by updating the text property (even using Object.defineProperty(file, 'text', { value: 'something' })). But I could do so by modifying the contents property (this also implicitly modifies text since it is a function that reads from contents). Example:

build.onEnd((result) => {
  const js = result.outputFiles.find(f => f.path.match(/\.js$/))
  const modified = doSomething(js.text)
  js.contents = Buffer.from(modified)
})

To see a fuller example (where I use it to instrument for code coverage) see marvinhagemeister/karma-esbuild#33 (comment)

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

3 participants