#335: configure webpack to allow usage of single source of supported languages (i18n/index.json)

This commit is contained in:
SleepWalker 2017-06-08 22:44:28 +03:00
parent a026ee3adf
commit 59000926ab
2 changed files with 19 additions and 34 deletions

View File

@ -1,28 +1,6 @@
import locales from 'i18n/index.json'; import locales from 'i18n/index.json';
import { addLocaleData } from 'react-intl'; import { addLocaleData } from 'react-intl';
import beLocaleData from 'react-intl/locale-data/be';
import enLocaleData from 'react-intl/locale-data/en';
import idLocaleData from 'react-intl/locale-data/id';
import plLocaleData from 'react-intl/locale-data/pl';
import roLocaleData from 'react-intl/locale-data/ro';
import ruLocaleData from 'react-intl/locale-data/ru';
import slLocaleData from 'react-intl/locale-data/sl';
import ptLocaleData from 'react-intl/locale-data/pt';
import ukLocaleData from 'react-intl/locale-data/uk';
import viLocaleData from 'react-intl/locale-data/vi';
// till we have not so many locales, we can require their data at once
addLocaleData(beLocaleData);
addLocaleData(enLocaleData);
addLocaleData(idLocaleData);
addLocaleData(plLocaleData);
addLocaleData(roLocaleData);
addLocaleData(ruLocaleData);
addLocaleData(slLocaleData);
addLocaleData(ptLocaleData);
addLocaleData(ukLocaleData);
addLocaleData(viLocaleData);
const SUPPORTED_LANGUAGES = Object.keys(locales); const SUPPORTED_LANGUAGES = Object.keys(locales);
const DEFAULT_LANGUAGE = 'en'; const DEFAULT_LANGUAGE = 'en';
@ -49,23 +27,24 @@ export default {
require(locale) { require(locale) {
const promises = [ const promises = [
new Promise(require(`bundle?name=[name]!react-intl/locale-data/${locale}.js`)),
new Promise(require(`bundle?name=[name]!i18n/${locale}.json`)) new Promise(require(`bundle?name=[name]!i18n/${locale}.json`))
]; ];
if (needPolyfill) { if (needPolyfill) {
promises.push(new Promise((resolve) => { promises.push(
require.ensure([], () => { new Promise(require('bundle?name=intl!intl')),
require('intl'); );
require.context( promises.push(
'bundle?name=[name]-polyfill-data!intl/locale-data/jsonp', new Promise(require(`bundle?name=[name]-polyfill-data!intl/locale-data/jsonp/${locale}.js`)),
false, );
new RegExp(`\\./(${SUPPORTED_LANGUAGES.join('|')})\\.js$`)
)(`./${locale}.js`)(resolve);
});
}));
} }
return Promise.all(promises) return Promise.all(promises)
.then(([messages]) => ({locale, messages})); .then(([localeData, messages]) => {
addLocaleData(localeData);
return {locale, messages};
});
} }
}; };

View File

@ -10,6 +10,7 @@ const CircularDependencyPlugin = require('circular-dependency-plugin');
const cssUrl = require('webpack-utils/cssUrl'); const cssUrl = require('webpack-utils/cssUrl');
const cssImport = require('postcss-import'); const cssImport = require('postcss-import');
const SUPPORTED_LANGUAGES = Object.keys(require('./src/i18n/index.json'));
const rootPath = path.resolve('./src'); const rootPath = path.resolve('./src');
const outputPath = path.join(__dirname, 'dist'); const outputPath = path.join(__dirname, 'dist');
@ -131,7 +132,12 @@ const webpackConfig = {
}), }),
new webpack.ProvidePlugin({ new webpack.ProvidePlugin({
React: 'react' React: 'react'
}) }),
// restrict webpack import context, to create chunks only for supported locales
// @see services/i18n.js
new webpack.ContextReplacementPlugin(
/locale-data/, new RegExp('/(' + SUPPORTED_LANGUAGES.join('|') + ')\\.js')
)
], ],
module: { module: {