'Create a bundle with dependencies emitted as nested functions within a target function export

I'd like to create a bundle with dependencies emitted as nested functions within a target function export.

Here is an example of what I'm trying to do...

a.js

export function hello() {
  console.log('hello from a.js');
}
export function goodbye() {
  console.log('goodbye from a.js');
}

b.js

export function hello() {
  console.log('hello from b.js');
}
export function goodbye() {
  console.log('goodbye from b.js');
}

main.js

import * as a from "./a";
import * as b from "./b";

export function main() {
  a.hello();
  b.goodbye();
}

bundle.js (desired output)

export function main() {
  function hello() {
    console.log('hello from a.js');
  }    
  function goodbye() {
    console.log('goodbye from b.js');
  }
  hello();
  goodbye();
}

The reason I'd like to do this is so I can inject a function export composed from ES6 modules into a remote Javascript execution environment as a self-contained unit with no dependencies. Ideally I'd like to have numerous function exports (main1, main2, etc.).

Is there any way to achieve something like this using rollup, parcel, webpack, or some other tool?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source