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

How to get final version of build.initialOptions inside a plugin? #3615

Closed
koshic opened this issue Jan 27, 2024 · 2 comments
Closed

How to get final version of build.initialOptions inside a plugin? #3615

koshic opened this issue Jan 27, 2024 · 2 comments

Comments

@koshic
Copy link

koshic commented Jan 27, 2024

My use case: I'm an author of pluginA and I have to rely on some esbuild options to provide consistent output (as an example, absWorkingDir value affects source maps generation, entrypoints style affects metafile analysis, etc.).

Obvious and well documented solution is to use https://esbuild.github.io/plugins/#build-options. But, those options can be mutated by other plugins, so in the following setup build.initialOptions available inside pluginA is... incorrect:

plugins: [
  pluginA(),
  pluginB() // hahaha, absWorkingDir will be changed 👿 
]

Changing the order can help only in limited situations: what if plugin count > 2 (5, 10, 20...)? What if pluginB also need actual settings which can mutated by pluginA? It looks like a circular dependency.

Is it possible to get actual options now? Or it's need to add something like build.getFinalOptions()to the API?

@evanw
Copy link
Owner

evanw commented Jan 27, 2024

I'm confused. Can't you just access build.initialOptions in your plugin? Something like this:

const esbuild = require('esbuild')

const pluginA = () => ({
  name: 'pluginA',
  setup(build) {
    build.onStart(() => {
      console.log(build.initialOptions)
    })
  }
})

const pluginB = () => ({
  name: 'pluginB',
  setup(build) {
    build.initialOptions.absWorkingDir = '/'
  }
})

esbuild.build({
  plugins: [
    pluginA(),
    pluginB() // hahaha, absWorkingDir will be changed 👿
  ]
})

You just need to wait until setup is complete. One way to do that is with onStart. I suppose it could be a problem if some plugin continues to mutate build.initialOptions over time. But esbuild doesn't attempt to defend against plugins that are actively trying to break things.

@koshic
Copy link
Author

koshic commented Jan 27, 2024

Can't you just access build.initialOptions in your plugin?

Yeah, it works. Sorry for such stupid issue, I though that 'build' object is not shared 'as is' for all plugins :-/

I suppose it could be a problem if some plugin continues to mutate build.initialOptions over time.

For me it's a 'broken' plugin should be fixed / replaced so that's is not an issue.

And thank you for warm, prompt answer and such beautiful tool!

@koshic koshic closed this as completed Jan 27, 2024
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