getify / moduloze

Convert CommonJS (CJS) modules to UMD and ESM formats
MIT License
208 stars 11 forks source link

Add option for ES bundling, as alternative/mode for ES index output #11

Open getify opened 4 years ago

getify commented 4 years ago

Take inspiration from some very clever re-naming logic in rollup that handles collisions well:

main.js:

import { hello as h1 } from "./a.js";

import { hello as h2, foo } from "./b.js";

h1();
h2();
console.log(f2);
export { foo };

a.js:

var foo = 2;
export function hello() { console.log(foo); }

b.js:

var foo = 3;
export function hello() { console.log(foo); }
export { foo }

Produces...

output.js:

var foo = 2;
function hello() { console.log(foo); }

var foo$1 = 3;
function hello$1() { console.log(foo$1); }

hello();
hello$1();
console.log(f2);

export { foo$1 as foo };
getify commented 4 years ago

Also interesting issues to address differently from how Rollup behaves:

  1. How to handle name conflicts when re-naming won't work? Rollup issues a warning and just drops the duplicate stuff (via tree shaking, I guess?)?

    rollup example

    main.js:

    export * from "./a.js";
    export * from "./b.js";

    a.js:

    var foo = 2;
    export function hello() { console.log(foo); }

    b.js:

    var foo = 3;
    export function hello() { console.log(foo * 2); }

    Rollup does issue a warning that there's a collision between the two hello identifiers both being exported. Unfortuantely, the warning isn't fatal, and then Rollup just drops the entire contents of b.js (tree shaking, I assume?):

    var foo = 2;
    function hello() { console.log(foo); }
    
    export { hello };
  2. How to just stitch two peer modules together without some main entry-point module that "re-exports" them?

    rollup example

    main.js:

    var foo = 2;
    export function hello() { console.log(foo); }

    a.js:

    var foo = 3;
    export function hello2() { console.log(foo * 2); }

    Unfortunately, Rollup expects a main entry-point module, so (via tree shaking?) produces just this:

    var foo = 2;
    function hello() { console.log(foo); }
    
    export { hello };

    This emphasizes that Moduloze provides better for this use-case than Rollup, in that Moduloze (not relying on tree-shaking) doesn't require a single entry-point file to key off of, it just bundles a tree of peer files together.

    TBF, while Rollup doesn't support this directly, there's a plugin that seems to do so: multi-entry.

richytong commented 4 years ago

I am interested in this feature. Is there anything I can help with to get this done?