Skip to content

renderChunk hook to replace transformChunk - #2406

Merged
lukastaegert merged 6 commits into
masterfrom
render-chunk-fix
Aug 25, 2018
Merged

lukastaegert merged 6 commits into
masterfrom
render-chunk-fix

Conversation

@guybedford

Copy link
Copy Markdown
Contributor

This PR contains:

  • bugfix
  • feature
  • refactor
  • tests
  • documentation
  • metadata

Breaking Changes?

  • yes
  • no

If yes, please describe the breakage.

Please Describe Your Changes

In the PR #2208 I introduced a breaking change to the transformChunk hook that provided the full Chunk.ts class interface as the third argument, completely unintentionally. It also does not match the type and what is documented.

There was supposed to be something like this, which got removed while I was working on that (an earlier commit in that same PR) - cc13455#diff-15e7c9d00e111928150170af289cf765.

To change at this point would be breaking (I noticed this in https://github.com/surma/rollup-plugin-loadz0r/blob/master/index.js), so instead the suggestion is to make a new hook "renderChunk" and have it replace "transformChunk" with transformChunk being deprecated as soon as possible. Such a short sweet life this poor hook.

Sincere apologies for this, can tell I was close to burnout back in May :)

The renderChunk hook takes the form renderChunk (code, { modules, exports, imports, fileName, isEntry }, outputOptions) and is allowed to return a new source to transform the rendered output, exactly as before.

The test includes checks for all of these properties, so hopefully we don't have a similar regression anytime soon.

@lukastaegert lukastaegert left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Dammit, transformChunk was a really nice name. But I guess renderChunk works well enough. Great of you to spot this, especially via other people's code!

Two points:

  • Did you contact @surma about this? Would be nice to get some feedback from him if the new hook would work for him and if he has further suggestions how to improve this.
  • At some point—at the latest with the 1.0.0 release IMO—we should have deprecation warnings if one of the deprecated hooks is used. As the transform(Bundle|Chunk) hooks are not that widely used (minification is the only important exception that comes to my mind) we could also consider adding warnings now. Otherwise this should not be forgotten.

Comment thread test/hooks/index.js
});
});

it('supports renderChunk in place of transformBundle and transformChunk', () => {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To have comprehensive tests for the only Chunk transform hook we want to keep (as of today 😉), I think we should extend this test to check that without returning anything, the output remains unchanged (this is the "get information about the Chunk" part of the hook) and add a second test that only tests that this hook actually transforms the chunk.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For a long term perspective, I think that actually all transformBundle tests should be reflected in corresponding renderChunk tests as otherwise the new hooks are not nearly as well tested as the old hooks while it should actually be the other way around. Once we reach hard deprecations for the old hooks, quite a few tests can be removed again.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed, will do.

Comment thread src/rollup/types.d.ts Outdated
}

export interface OutputChunk {
export interface RenderChunk {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe the name should actually be RenderedChunk as it is not a chunk for rendering but a chunk that will be rendered? This would also match the RenderedModule type nicely.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure.

@surma

surma commented Aug 14, 2018

Copy link
Copy Markdown

RIP transformChunk 💀 And here I was wondering how an object as complex as chunk didn’t have any proper documentation (DevTools explorative coding happened) 😅

Quick question: The reason I went so deep into the internals ther, is because I needed to figure out if the chunk is an entry module.

Now, the transformChunk API already exposed chunk.isEntryPointFacade (or something similar), but that flag is also true for import() boundaries. Is this any different here? If not, will chunk.modules.some(module => inputsFromOptions.includes(module.id)); work?

@guybedford

Copy link
Copy Markdown
Contributor Author

At some point—at the latest with the 1.0.0 release IMO—we should have deprecation warnings if one of the deprecated hooks is used. As the transform(Bundle|Chunk) hooks are not that widely used (minification is the only important exception that comes to my mind) we could also consider adding warnings now. Otherwise this should not be forgotten.

As mentioned before I will be working on a deprecation PR to ensure all the right alignments here for 1.0, but thanks for following up on this.

@surma thanks for trying these new hooks out - it's helped a lot already, I wouldn't have caught this otherwise! The isEntry boolean property will currently be true for dynamic imports yes. Currently all dynamic imports are treated as entry modules, but it would definitely be an improvement if we could better handle them as chunks. I've created an issue to track this further in #2407. If that can go through soon I think that would solve your use case, but if there are delays on that for any reason we can look at extending meta here too as well. We can track in that issue further on this.

@TrySound

Copy link
Copy Markdown
Member

size-snapshot also uses transformBundle. What should I use instead?

@surma

surma commented Aug 14, 2018

Copy link
Copy Markdown

@TrySound You are using transformBundle, which is even older. But just skimming your code it seems like you are not using the 3rd chunk parameter, so all you need to do is to rename to renderChunk once this lands.

@guybedford guybedford mentioned this pull request Aug 14, 2018
4 of 8 tasks
@guybedford
guybedford changed the base branch from master to watch-events August 14, 2018 17:24
@guybedford

Copy link
Copy Markdown
Contributor Author

(I've rebased this to the watch-events PR for consistent merging)

@lukastaegert lukastaegert left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good!

@lukastaegert
lukastaegert changed the base branch from watch-events to master August 21, 2018 04:44
@lukastaegert
lukastaegert merged commit bf84765 into master Aug 25, 2018
@guybedford

Copy link
Copy Markdown
Contributor Author

Thanks, note we shouldn't release until this has been properly documented. Will aim to get something together shortly.

@lukastaegert
lukastaegert deleted the render-chunk-fix branch August 25, 2018 13:47
@lukastaegert

Copy link
Copy Markdown
Member

Damn it, was a little quick on this one. Will hold back with tweeting until we have some docs.

@guybedford

Copy link
Copy Markdown
Contributor Author

No problem, it is backwards compatible so we can just wait on the documentation and note this later on as part of any other release notes. No reason to wait on publicising this, it's more just about ensuring good process that we don't forget these things.

@guybedford

Copy link
Copy Markdown
Contributor Author

*no reason to wait on publicising the release I mean

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

Successfully merging this pull request may close these issues.

4 participants