tc39 / proposal-module-expressions

https://tc39.es/proposal-module-expressions/
MIT License
438 stars 18 forks source link

Useful for code splitting #52

Closed zdila closed 3 years ago

zdila commented 3 years ago

Maybe it would be worth mentioning that one usecase could be a code splitting (eg. with webpack).

nicolo-ribaudo commented 3 years ago

For code splitting you can already use normal modules (one module per file/chunk).

This could be useful for the opposite: to easily put multiple modules in the same file/chunk.

surma commented 3 years ago

Please see the Bundling section in the FAQ of the README :)

zdila commented 3 years ago

Putting multiple modules in the same chunk is default behaviour of Webpack (ie. it compiles many source js files to a single one).

What I am missing is to have a single source js file compiled by webpack to multiple chunks.

nicolo-ribaudo commented 3 years ago

Oh something like this?

element.addEventListener("click", () => import(clickHandler).then(m => m.default()));

// This module is not used immediately, we can tell webpack to put it in a separate lazy-loaded chunk.
const clickHandler = module {
  export default function () {
    console.log("Clicked!");
  }
}
zdila commented 3 years ago

@nicolo-ribaudo exactly

surma commented 3 years ago

Again, if you look at the READAME Bundling section I linked earlier, you’ll see why it works in simple situations, but not as a general solution for bundling.