score:15

Accepted answer

there are a few things that you need to do.

  1. make sure to require intl/intl which loads the core library and not all of the associated countries. this will reduce the size of the library from from around 900kb to around 150kb.

  2. use webpack's require.ensure or require([]) functions to dynamically require intl.js only when needed. this will create a separate bundle for just the intl.js file, which will be loaded on an as-needed basis.

lib/shim.js

// shim for intl needs to be loaded dynamically
// so we callback when we're done to represent
// some kind of "shimready" event
module.exports = function(callback) {
    if (!window.intl) {
        require(['intl/intl'], function(intl) {
            window.intl = intl;
            callback();
        });
    } else {
        settimeout(callback, 0); // force async
    }
};

app.js

var shimready = require('lib/shim');
shimready(startapp);

note: you may also need access to country-specific information and properties. for my basic needs i didn't actually require this data, but if you do be sure to read the section on intl.js website loading locale data.

score:6

an simple (non-webpack) approach would be to do something like this in your html:

<script>
 if (!window.intl) { document.write('<script src="/js/intl.min.js"><\/script>'); }
</script>

though document.write is not considered a best practice, this would provide a blocking approach to solving the problem, resulting in considerably less code in your the application. this solution does not use webpack, but may be a suitable solution for many people in this situation.


Related Query

More Query from same tag