doowb / unlazy-loader

Webpack loader to transform lazy-cache files into unlazy cached files.
MIT License
11 stars 2 forks source link

Use code splitting #23

Closed alecmev closed 8 years ago

alecmev commented 8 years ago

Without code splitting, Webpack ends up including optional dependencies like coffee-script (e.g. in gray-matter) into the main bundle, making it absolutely huge. This isn't an easy task, but unlazy-loader is hardly viable in production without on-demand loading.

jonschlinkert commented 8 years ago

sidenote, feel free to create an issue on gray-matter about that. It's a compelling reason to change how parsers are registered.

doowb commented 8 years ago

@jeremejevs if you know more about code splitting, I'd be happy to review a PR that allows a user to optionally set this loader to use webpacks code splitting syntax.

alecmev commented 8 years ago

I've confused some things; that coffee-script example from gray-matter has nothing to do with lazy-cache (and hence with unlazy-loader).

Anyway, I just did a proper research, and looks like this is much more complicated than I suspected. Long story short, it is possible to load and execute JavaScript synchronously, but it's so impractical that Webpack / Browserify / etc. don't even bother implementing it (rightfully so). As the result, the code that depends on a dynamically-loaded module has to be located in a callback (see require.ensure for an example). As the function of interest (e.g. insert in markdown-toc) now contains asynchronous code in it, it should also become asynchronous itself, and so on and so forth.

This is fixable (I can imagine a loader turning sync functions into async recursively where needed), but not sure if even worth the time investment (but this is definitely a nice-to-have). Libraries which are made with client-side usage in mind usually don't contain large amounts of optional code in them, so it's enough to just split the whole library and all its dependencies into an on-demand bundle. In the meanwhile, if there would arise a need in making a library like grey-matter browser-friendly as well, dependencies like coffee-script would then have to be either trimmed (e.g. by creating / finding a standalone CSON parser) or moved into plugins (or maybe something else).

This is probably out of scope of unlazy-loader, but I'll leave it up to you.

doowb commented 8 years ago

This is probably out of scope of unlazy-loader

I agree. Closing this since it's something that would be handled in a different loader.