accounts-frontend/webpack-utils/intl-loader.js

31 lines
932 B
JavaScript
Raw Normal View History

module.exports = function(content) {
this.cacheable && this.cacheable();
content = JSON.parse(content);
const moduleId = this.context
2019-07-01 09:09:59 +05:30
.replace(this.rootContext, '')
// TODO: can't find the way to strip out this path part programmatically
// this is a directory from resolve.modules config
// may be this may work: .replace(this._compiler.options.resolve.root, '')
.replace('src/', '')
.replace(/^\/|\/$/g, '')
.replace(/\//g, '.');
2019-07-01 09:09:59 +05:30
content = JSON.stringify(
Object.keys(content).reduce(
(translations, key) => ({
...translations,
[key]: {
id: `${moduleId}.${key}`,
defaultMessage: content[key]
}
}),
{}
)
);
2019-07-01 09:09:59 +05:30
return `import { defineMessages } from 'react-intl';
2019-07-01 09:09:59 +05:30
export default defineMessages(${content})`;
};