Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Webpack to load files as chunks and not bundle up into one-Dynamic require #3364

Closed
PoojaSSW opened this issue Nov 23, 2016 · 5 comments
Closed
Labels

Comments

@PoojaSSW
Copy link

I want to load the translation files(react-intl-yahoo lib). I want the webpack to load seperate chunks of files at the compile time instead of bundling them up into one. I tried using the require.ensure to optimize loading of locale data files:

let data= {};
  require.ensure([], function(require) {
    data = require("./languages/"+locale+".json");
  });

but I see that Webpack is loading the entire languages-folder with all the files bundled during compile time instead a separate one.
What is the best way to chunk the translation files separately and load asynchronously at the compile time?

@PoojaSSW PoojaSSW changed the title Webpack to load files as chunks and not bumdle up into one-Dynamic require Webpack to load files as chunks and not bundle up into one-Dynamic require Nov 23, 2016
@TheLarkInn
Copy link
Member

If you are using webpack v2, you can use import("./languages/"+local+".json") and this should create x separate chunks for each language in a separate file.

@TheLarkInn
Copy link
Member

Or if you are not on latest webpack 2 beta, you can use System.import("./languages/"+local+".json")

@jharris4
Copy link
Contributor

I'm running into the same issue with require.ensure generating one bundle for all files in the same directory/context. It'd be nice if we had some kind of control over how/when it groups things from the same context into a giant bundle.

System.import (I assume it's the same for import) is not a workable solution for me, because it doesn't support several features that require.ensure does:

  1. Named chunks (Named chunks using System.imports  #3132)
  2. recursive / batched code splitting (description below):

Let's say we have 3 libraries:

  • libraryA - 50 KB
  • libraryB - 60 KB (but 10 KB that is not part of libraryA)
  • libraryC - 70 KB (but 20 KB that is not part of libraryA)

So basically, libraryB and libraryC both require/import libraryA.

Using require.ensure, we can do the following:

require.ensure([], (require) => {
  require('libraryA');
  require.ensure([], (require) => {
    require('libraryB');
    // do something
  }, 'bundleB');
}, 'bundleA');

require.ensure([], (require) => {
  require('libraryA');
  require.ensure([], (require) => {
    require('libraryC');
    // do something
  }, 'bundleC');
}, 'bundleA');

The resulting bundles will be:

  • bundleA - 50 KB
  • bundleB - 10 KB
  • bundleC - 20 KB

Using System.import, we can do the following:

System.import('libraryA').then(() => {
  System.import('libraryB').then(() => {
    // do something
  });
});

System.import('libraryA').then(() => {
  System.import('libraryC').then(() => {
    // do something
  });
});

The resulting bundles will be:

  • bundleA - 50 KB
  • bundleB - 60 KB
  • bundleC - 70 KB

I know that using code splitting recursively like this is not very common, but it has been a very powerful feature to reduce bundle sizes on projects I've worked on.

I guess the named chunks are something I could live without (although you can see with examples like recursive splitting how useful they are). But I really don't want to give up the clever bundle-size reduction require.ensure gives, unless it can be replicated in System.import / import.

@webpack-bot
Copy link
Contributor

This issue had no activity for at least half a year.

It's subject to automatic issue closing if there is no activity in the next 15 days.

@webpack-bot
Copy link
Contributor

Issue was closed because of inactivity.

If you think this is still a valid issue, please file a new issue with additional information.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

5 participants
@TheLarkInn @PoojaSSW @jharris4 @webpack-bot and others