1

How can I dynamically bundle a module/object into my RollupJs output file? I have tried a ton off different options but can not get the expected output I am looking for.

I put together a short sample project below to help illustrate what I am looking for. The expected output should print "Hello John Doe" from the overrideApp object that is dynamically injected as a dependency.

src/app.js

export default {
  sayHello: function() {
    console.log('Hello Mr.Roboto')
  },
  sayGoodBye: function() {
    console.log('Goodbye Mr.Roboto')
  }
}

index.js

import app from './src/app.js'
import overrideApp from 'overrideApp'
export default { ...app, ...overrideApp }.sayHello()

.rollup.config.js

let overrideApp = {
  sayHello: function() {
    console.log('Hello John Doe')
  }
}
export default [
  {
    input: 'index.js',
    external: ['overrideApp'], // This is not working, expecting to pass overrideApp to index.js
    output: {
      file: './dist/app.js',
      format: 'umd',
      name: 'bundle',
    }
  }
]

1 Answer 1

1

This is totally correct your mixing here a lot of stuff together that does not work together.

You are looking for a virtual module

Install

npm install @rollup/plugin-virtual --save-dev

Usage

Note. Use this plugin before any others such as node-resolve or commonjs, so they do not alter the output.

Suppose an entry file containing the snippet below exists at src/entry.js, and attempts to load batman and src/robin.js from memory:

// src/entry.js
import batman from 'batman';
import robin from './robin.js';

console.log(batman, robin);

Create a rollup.config.js configuration file and import the plugin:

import virtual from '@rollup/plugin-virtual';

export default {
  entry: 'src/entry.js',
  // ...
  plugins: [
    virtual({
      batman: `export default 'na na na na na'`,
      'src/robin.js': `export default 'batmannnnn'`
    })
  ]
};

https://github.com/rollup/plugins/edit/master/packages/virtual

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.