Search code examples
javascriptnode.jsbundlerrolluprollupjs

Rollup.js - Use rollup.config.js in JS API?


I have a working rollup.config.js file but need to run a separate packaging script after Rollup is done. My plan was to use Rollup's watchers via their JS API. However, I cannot get the JS API to work at all.

I am referencing this code from the Rollup site...

const loadConfigFile = require('rollup/dist/loadConfigFile');
const path = require('path');
const rollup = require('rollup');

loadConfigFile(path.resolve(__dirname, 'rollup.config.js'))
  .then(async ({options, warnings}) => {
    warnings.flush();
    const bundle = await rollup.rollup(options);
    await Promise.all(options.output.map(bundle.write));
    rollup.watch(options);
  })

but I keep getting an error Unknown input options: 0.......Error: You must supply options.input to rollup

My rollup.config.js is as follow...

import svelte from 'rollup-plugin-svelte';
import resolve from '@rollup/plugin-node-resolve';
import commonjs from '@rollup/plugin-commonjs';
import livereload from 'rollup-plugin-livereload';
import { terser } from "rollup-plugin-terser";
import replace from '@rollup/plugin-replace';
import json from '@rollup/plugin-json';

const production = !process.env.ROLLUP_WATCH;

export default {
    input: 'src/main.js',
    output: {
        sourcemap: true,
        format: 'iife',
        name: 'app',
        file: 'public/bundle.js'
    },
    plugins: [
        json(),
        production && replace({
            'eruda': ``,
            exclude: 'node_modules/**',
            delimiters: ['import * as eruda from \'', '\'']
        }),
        production && replace({
            'eruda': ``,
            exclude: 'node_modules/**',
            delimiters: ['', '.init()']
        }),
        svelte({
            dev: !production,
            css: css => {
                css.write('public/bundle.css');
            }
        }),
        resolve({ browser: true }),
        commonjs(),

        !production && livereload('public'),
        production && terser()
    ],
    watch: {
        clearScreen: false
    }
};

Any thoughts are appreciated!


Solution

  • I think the example at rollupjs.org is wrong. Shouldn't it be like this instead?

    const loadConfigFile = require('rollup/dist/loadConfigFile')
    const path = require('path')
    const rollup = require('rollup')
    
    // load the config file next to the current script;
    // the provided config object has the same effect as passing "--format es"
    // on the command line and will override the format of all outputs
    loadConfigFile(path.resolve(__dirname, 'rollup.config.js'), {format: 'es'})
      .then(({options, warnings}) => {
        // "warnings" wraps the default `onwarn` handler passed by the CLI.
        // This prints all warnings up to this point:
        console.log(`We currently have ${warnings.count} warnings`)
    
        // This prints all deferred warnings
        warnings.flush()
        
        // options is an "inputOptions" object with an additional "output"
        // property that contains an array of "outputOptions".
        // The following will generate all outputs and write them to disk the same
        // way the CLI does it:
        options.map(async options => {
            const bundle = await rollup.rollup(options)
            await Promise.all(options.output.map(bundle.write))
         
            // You can also pass this directly to "rollup.watch"
            rollup.watch(options)
          })
      })