I am trying to migrate from RequireJS to Webpack, and I'm not sure if the best way is to process our locale files.
We are currently creating a separate JS file for each locale. These files contain 7+ module definitions for i18n messages, as well as library configurations (such as moment). For example, our da_DK file looks something like this:
(function (global, factory) { typeof exports === 'object' && typeof module !== 'undefined' ? factory(require('../moment')) : typeof define === 'function' && define.amd ? define('moment-locale',['moment'], factory) : factory(global.moment) }(this, function (moment) { 'use strict'; var da = moment.defineLocale('da', { ... }); return da; })); define('messages',[],function(){ var da_messages = { ... }; return da_messages; });
At run time, we determine the appropriate language file to download with the rest of our application. Our application code does not know which language is loaded; any locale-dependent modules will do require('moment-locale') and require('messages') .
I want to do something similar with Webpack, but I have not yet found a good way to accomplish this.
I have seen require.context for dynamic needs, but it looks like this will result in combining all possible locales with my application, which I would rather not do.
I also looked into DllPlugin, considering that each locale file could be a "dll", but I noticed that the dll manifest contains certain module names (for example: node_modules / moment / locale / de-at.js), and I think that I need this to be more general, so webpack knows when I require('moment-locale') it should look in this dll.
One of the ways I was able to get this to work was to update the locale code generation code so that it creates an entry for each locale that looks like this:
module.exports = { 'messages': require('messages'), 'moment-locale': require('moment-locale'), ... };
and then in the webpack configuration, I set the library field to the namespace for my application. And then in my webpack config application, I referenced these modules in externals . In other words, when da_DK.js loads, it puts all the modules in the window under the namespace that the application refers to when loading. I would prefer not to use this approach, but this is the only way I have been able to get this to work so far.
Is there any other / better way to accomplish this?
webpack
Sean parmelee
source share