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

Per output plugins #3218

Merged
merged 15 commits into from Nov 12, 2019
Merged

Per output plugins #3218

merged 15 commits into from Nov 12, 2019

Conversation

lukastaegert
Copy link
Member

This PR contains:

  • bugfix
  • feature
  • refactor
  • documentation
  • other

Are tests included?

  • yes (bugfixes and features will not be merged without tests)
  • no

Breaking Changes?

  • yes (breaking changes will not be merged unless absolutely necessary)
  • no

List any relevant issue numbers:
Resolves #3174
Resolves #3098

Description

This is the new big feature I have been working on. It will solve the issues of files emitted from different outputs overwriting each other by giving each output a separate FileEmitter, but more importantly, this will allow different outputs to have different plugins.

Example how this is useful:

// rollup.config.js
import {terser} from 'rollup-plugin-terser';

export default {
  input: 'main.js',
  output: [
    {file: 'bundle.js', format: 'esm'},
    {file: 'bundle.min.js', format: 'iife', plugins: [terser()]},
  ]
};

The big advantage is that you can now generate a minified and a non-minified build together without needing to repeat the analysis step, which will be noticeably faster than run rollup twice.

Rules for output plugins:

  • They can have the same hooks as regular plugins, BUT
  • Hooks run during the build phase will NOT be repeated for them. Instead, a warning is given if a plugin has such hooks. This includes e.g. the options hook.
  • To gain access to input and output options, the renderStart hook will now receive two arguments: outputOptions and inputOptions.

This is a draft PR as it still lacks any documentation. However, it should be fully useable and I invite everyone to test it via npm i rollup/rollup#per-output-plugins.

cc @marijnh

@codecov
Copy link

codecov bot commented Nov 8, 2019

Codecov Report

Merging #3218 into master will increase coverage by 0.62%.
The diff coverage is 100%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #3218      +/-   ##
==========================================
+ Coverage   90.64%   91.26%   +0.62%     
==========================================
  Files         167      170       +3     
  Lines        5911     5922      +11     
  Branches     1793     1794       +1     
==========================================
+ Hits         5358     5405      +47     
+ Misses        336      311      -25     
+ Partials      217      206      -11
Impacted Files Coverage Δ
src/ModuleLoader.ts 98.47% <ø> (ø) ⬆️
src/Module.ts 97.3% <ø> (ø) ⬆️
src/utils/addons.ts 100% <ø> (ø) ⬆️
src/utils/mergeOptions.ts 90.47% <100%> (ø) ⬆️
src/utils/PluginContext.ts 100% <100%> (ø)
src/Graph.ts 92.94% <100%> (ø) ⬆️
src/utils/PluginCache.ts 100% <100%> (ø)
src/rollup/index.ts 97.02% <100%> (+3.09%) ⬆️
src/utils/assignChunkIds.ts 100% <100%> (ø) ⬆️
src/utils/transform.ts 94.8% <100%> (ø) ⬆️
... and 14 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 8318bb4...a3a2315. Read the comment docs.

@marijnh
Copy link
Contributor

marijnh commented Nov 8, 2019

this will allow different outputs to have different plugins.

That's wonderful—I needed exactly that earlier today. Thanks for doing this work!

@Andarist
Copy link
Member

Andarist commented Nov 9, 2019

From what I can see this can hook into rendering, but I'm not sure what rendering is - is tree-shaking process? from the prepared input graph to the output one?

I'm wondering if this would allow me to in example build 2 bundles with process.env.NODE_ENV replaced to "production" and "development" in a single config.

@lukastaegert
Copy link
Member Author

Output plugins act only after tree-shaking and chunking are done. Basically what you can do is transform the emitted chunks, add banners/footers etc.

Changing the tree-shaking between outputs would basically mean we need to repeat a lot between outputs and would mostly prevent the possible performance gains.

@lukastaegert lukastaegert force-pushed the per-output-plugins branch 4 times, most recently from a18d8dc to 5869d10 Compare November 12, 2019 06:32
@lukastaegert lukastaegert marked this pull request as ready for review November 12, 2019 08:10
@lukastaegert lukastaegert merged commit 17c14cd into master Nov 12, 2019
@lukastaegert lukastaegert deleted the per-output-plugins branch November 12, 2019 16:27
@calebeby
Copy link

Thanks Lukas this is an amazing feature 🎉

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