简体   繁体   中英

Run rollup plugin when everything on build is finished

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM