is it possible to run a rollup plugin without an input file (in a multi bundle instance)?
Asked Answered
S

2

9

I have a situation where I am bundling multiple files via rollup cli an example of this is available in documentation.

I export an array of bundles like so:

export default [
    {
        input: 'packages/A/index.js',
        output: {
            name: '[name]Bundle',
            file: '[name].umd.js',
            format: 'umd'
        }
    },
    {
        input: 'packages/B/index.js',
        output: {
            name: '[name]Bundle',
            file: '[name].umd.js',
            format: 'umd'
        }
    }
];

And then I have a function that adds a common config to each bundle (think plugins) so something like:

import path from "path";

import alias from '@rollup/plugin-alias';
import resolve from '@rollup/plugin-node-resolve';


const augment = configs => {

    const generateAlias = (symbol, location) => {
        return {
            find: symbol,
            replacement: path.resolve(process.cwd(), location)
        };
    }

    const entries = [
        generateAlias("@", "src/"),
        generateAlias("~", "src/assets/scss/"),
        generateAlias("#", "src/assets/img/"),
        generateAlias("%", "config/"),
    ];

    const plugins = [
        alias({
            entries
        }),
        resolve({ browser: true }),
    ];

    return configs.map(entry => {
        return {
            ...entry,
            plugins
        }
    });
}

export {
    augment
}

And the I wrap the above exported array in augment like:

const bundles = [/* above example */];

export default augment(bundles);

Now this all works fine, but I have two plugins that I don't actually want to apply to each bundle I just want to run them once all the bundles have built, those two plugins are; rollup-plugin-serve and rollup-plugin-workbox now neither of these actually have anything to do with any of the files being bundled, and make no sense to instantiate more than once.

What I would like to do as part of the augment function to to append them to the end of the returned array something like:

const exportedArray = configs.map(/* function from above */);

exportedArray.push(...[
        {
                plugins: [
                        serve({
                                host,
                                port,
                                contentBase: 'dist/'
                        })
                ]
        }
]);

return exportedArray;

And this would not contain an input, I have tried the above and rollup complains, the alternative option would be to add these plugins to the final bundle on the array, I just prefer the idea of instantiating them without them being tied to a particular bundle.

Salimeter answered 9/1, 2020 at 3:3 Comment(1)
Did you figure this out?Enstatite
A
3

Rollup needs an input file to work, but that doesn't mean you cannot simply throw away the result of the file. You can make an empty file empty.js (or maybe add some comment in it) and use that as the input. Just leave out the output properties so that the result is not written anywhere.

---EDIT---

a bit like this:

export default {
    input: './empty.js',
    plugins: [{
        name: 'test plugin',
        load() {
            console.log('test plugin')
        }
    }]
}

---EDIT---

Alternatively you can write to a dummy file and remove it again

const fs = require('fs')

export default {
    input: './empty.js',
    output: {
        file: './dummy',
    },
    plugins: [{
        name: 'test plugin',
        writeBundle(options) {
            fs.unlinkSync(options.file)
        }
    }]
}
Afterwards answered 16/1, 2020 at 11:16 Comment(6)
output is required.Enstatite
no it doesn't, the code I just added as an example works fineAfterwards
It's in the doc rollupjs.org/guide/en/#configuration-filesEnstatite
I know it's in the docs, but it works without, if you really want to you can also just dump it to a dummy fileAfterwards
It fails with [!] Error: You must specify "output.file" or "output.dir" for the build. I think you're using an older version.Enstatite
2.7.6 (latest) very strangeAfterwards
J
2

I faced a similar problem in our own rollup plugin. We use rollup to generate many bundles from many inputs. However, since the build takes a long time, we determine which inputs need to be built and which is unchanged. So there may be a situation where the rollup runs without any input.

The only solution is to create an empty file, which will not be written to disk as a result.

Config has no any input:

const rollupOptions: rollup.RollupOptions = {
    output: {
        dir: 'some-dir',
    },
    plugins: [buildManager(entryFiles)],
}

Plugin buildManager creates inputs on buildStart hook:

export function buildManager(entryFiles: string[]): rollup.Plugin {
    const emptyRunId = 'no-input-for-this-run';

    return {
        name: 'build-manager',
        buildStart() {
            for (const entryFile of entryFiles) {
                // if some condition met, emit chunk (~ create an input)
            }

            // if the condition does not met even once, no chunk is created, but rollup needs at least one input
            this.emitFile({
                id: emptyRunId,
                fileName: emptyRunId,
                type: 'chunk',
            });
        },
        resolveId(source) {
            if (source === emptyRunId) {
                // The leading \0 instructs other plugins and rollup not to try to resolve, load or transform this module
                return `\0${emptyRunId}`;
            }
            return null;
        },
        load(id) {
            if (id.includes(emptyRunId)) {
                // some random code to avoid rollup empty chunk warning
                return 'export const empty = true;';
            }

            return null;
        },
        generateBundle(options, bundle) {
            // remove empty chunk from final bundle to not write that file
            if (bundle[emptyRunId]) {
                delete bundle[emptyRunId];
            }
        },
    };
}

So it could be easily used as standalone plugin to handle empty run.

Justness answered 29/4, 2022 at 6:46 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.