Skip to content

Commit

Permalink
Implement writeBundle hook
Browse files Browse the repository at this point in the history
  • Loading branch information
lukastaegert committed Jan 5, 2019
1 parent 27cde2a commit ae43694
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 5 deletions.
7 changes: 6 additions & 1 deletion docs/05-plugins.md
Expand Up @@ -90,7 +90,7 @@ Cf. [`output.banner/output.footer`](guide/en#output-banner-output-footer).
#### `generateBundle`
Type: `(options: OutputOptions, bundle: { [fileName: string]: AssetInfo | ChunkInfo }, isWrite: boolean) => void | Promise<void>`

Called at the end of `bundle.generate()` or `bundle.write()`. `bundle` provides the full list of files being written or generated along with their details:
Called at the end of `bundle.generate()` or immediately before the files are written in `bundle.write()`. To modify the files after they have been written, use the [`writeBundle`](guide/en#writebundle) hook. `bundle` provides the full list of files being written or generated along with their details:

```
// AssetInfo
Expand Down Expand Up @@ -176,6 +176,11 @@ Type: `(id: string) => void`

Notifies a plugin whenever rollup has detected a change to a monitored file in `--watch` mode.

#### `writeBundle`
Type: `( bundle: { [fileName: string]: AssetInfo | ChunkInfo }) => void | Promise<void>`

Called only at the end of `bundle.write()` once all files have been written. Similar to the [`generateBundle`](guide/en#generatebundle) hook, `bundle` provides the full list of files being written along with their details.

### Deprecated

☢️ These hooks have been deprecated and may be removed in a future Rollup version.
Expand Down
4 changes: 3 additions & 1 deletion src/rollup/index.ts
Expand Up @@ -319,7 +319,9 @@ export default function rollup(rawInputOptions: GenericConfigObject): Promise<Ro
Object.keys(bundle).map(chunkId => {
return writeOutputFile(graph, result, bundle[chunkId], outputOptions);
})
).then(() => createOutput(bundle));
)
.then(() => graph.pluginDriver.hookSeq('writeBundle', [bundle]))
.then(() => createOutput(bundle));
});
})
};
Expand Down
1 change: 1 addition & 0 deletions src/rollup/types.d.ts
Expand Up @@ -212,6 +212,7 @@ export interface Plugin {
bundle: OutputBundle,
isWrite: boolean
) => void | Promise<void>;
writeBundle?: (this: PluginContext, bundle: OutputBundle) => void | Promise<void>;
intro?: AddonHook;
load?: LoadHook;
name: string;
Expand Down
35 changes: 32 additions & 3 deletions test/hooks/index.js
Expand Up @@ -787,6 +787,7 @@ module.exports = input;
}),
{
generateBundle(options, outputBundle, isWrite) {
assert.strictEqual(isWrite, false);
const chunk = outputBundle['input.js'];

// can detect that b has been tree-shaken this way
Expand All @@ -802,9 +803,37 @@ module.exports = input;
}
]
})
.then(bundle => {
return bundle.generate({ format: 'es' });
});
.then(bundle => bundle.generate({ format: 'es' }));
});

it('supports writeBundle hook', () => {
const file = path.join(__dirname, 'tmp/bundle.js');
let bundle;
let callCount = 0;
return rollup
.rollup({
input: 'input',
plugins: [
loader({
input: `export { a as default } from 'dep';`,
dep: `export var a = 1; export var b = 2;`
}),
{
generateBundle(options, outputBundle, isWrite) {
bundle = outputBundle;
assert.strictEqual(isWrite, true);
}
},
{
writeBundle(outputBundle) {
assert.deepStrictEqual(outputBundle, bundle);
callCount++;
}
}
]
})
.then(bundle => bundle.write({ format: 'esm', file }))
.then(() => assert.strictEqual(callCount, 1));
});

it('supports this.cache for plugins', () => {
Expand Down

0 comments on commit ae43694

Please sign in to comment.