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

generateBundle hook should be executed after writing files when calling bundle.write #2617

Closed
egoist opened this issue Dec 29, 2018 · 5 comments · Fixed by #2643
Closed

generateBundle hook should be executed after writing files when calling bundle.write #2617

egoist opened this issue Dec 29, 2018 · 5 comments · Fixed by #2643

Comments

@egoist
Copy link

egoist commented Dec 29, 2018

  • Rollup Version: 1.0.0
  • Operating System (or Browser): N/A
  • Node Version: N/A

How Do We Reproduce?

Create a simple plugin which uses the generateBundle hook to read output file:

const fs = require('fs')

module.exports = () => ({
  generateBundle({ file }, _, isWrite) {
    if (isWrite) {
      fs.readFileSync(file) // Error because it does not exist yet!
    }
  }
})

This should be a bug since onwrite hook works as expected but it's deprecated in favor of this.

Expected Behavior

Should be able to access output files when isWrite is true.

Actual Behavior

No output files.

It seems generateBundle hook is always executed in the generate function:

return generate(outputOptions, true).then(bundle => {

while writeOutputFile is called after that:

return writeOutputFile(graph, result, bundle[chunkId], outputOptions);

@ndresx
Copy link

ndresx commented Dec 31, 2018

Just stumbled upon the same issue. The generateBundle function gets only called once per bundle, with the isWrite being true. However, the bundle is not available in this moment yet.

Expected Behavior

The generateBundle function should be called twice per bundle and according to the spec, the first time with isWrite being false =>

(ongenerate - Use generateBundle - Function hook called when bundle.generate() is being executed.

and the 2nd time with isWrite being true =>

onwrite - Use generateBundle - Function hook called when bundle.write() is being executed, after the file has been written to disk.

@lukastaegert
Copy link
Member

Thanks for the feedback. I agree that there should be a hook that is called after the write has been performed. Not sure yet if it should be the same hook (after all it is called "generateBundle").

Just to clarify, what functionality do you want to trigger from this hook? After all, everything that is written is also available via the parameter of this hook. This might help in sketching out how this is addressed. At the moment, my feeling is that I would prefer to add a new hook, e.g. writeBundle.

@egoist
Copy link
Author

egoist commented Jan 4, 2019

I prefer a new hook as well, I use onwrite hook in rollup-plugin-hashbang to make the output file executable.

@lukastaegert
Copy link
Member

I use onwrite hook in rollup-plugin-hashbang to make the output file executable

Ah, that makes sense. I'll see what I can do.

@lukastaegert
Copy link
Member

Fix at #2617

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

Successfully merging a pull request may close this issue.

3 participants