5

I use the experimentalCodeSplitting: true feature of rollup 0.61.2 to get nice code splitting. Because my project consists also of assets I created a plugin which copies and minifies the asset files accordingly. The problem is that the hooks I used are called for every chunk which is created. Therefore the assets are copied and minified multiple time. The only workaround I found is, to create some flag which is set to true after everything is done correctly. Is there a functionality to call a rollup hook after everything (or before everything) is finished and not on every chunk? Now my plugin looks something like the following code (I removed some parts and simplified for readability):

export default function copy(userOptions = {}) {
    const name = 'copyAndMinify';
    const files = userOptions.files || [];
    let isCopyDone = false;

    return {
        name: name,
        // also tried onwrite, ongenerate, buildEnd and generateBundle
        buildStart() { 
            if (isCopyDone) {
                return;
            }
            for (let key in files) {
                const src = key;
                const dest = files[key];

                try {
                    minifyFile(src, dest);
                } catch (err) {
                    fatal(name, src, dest, err);
                }
            }
            isCopyDone = true;
        }
    };
};

Maybe there is a better way of doing this kind of stuff because with this implementation I always have to completely restart rollup to execute my plugin

tschoartschi
  • 1,453
  • 2
  • 14
  • 23

1 Answers1

5

The rollup site lists all the available plugin hooks.

generateBundle seems like what you'd want.

generateBundle (formerly onwrite and ongenerate) - a ( outputOptions, bundle, isWrite ) => void function hook called when bundle.generate() or bundle.write() is being executed; you can also return a Promise. bundle provides the full list of files being written or generated along with their details.

Tivac
  • 2,553
  • 18
  • 21