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

39 lines
992 B
JavaScript
Raw Normal View History

2019-12-07 16:58:52 +05:30
// @ts-nocheck
2019-11-27 13:39:51 +05:30
function transform(src, modulePath, rootContext) {
const json = JSON.parse(src);
const moduleId = modulePath
.replace(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('packages/app/', '')
.replace(/^\/|\/$/g, '')
.replace(/\//g, '.');
return JSON.stringify(
Object.keys(json).reduce(
(translations, key) => ({
...translations,
[key]: {
id: `${moduleId}.${key}`,
defaultMessage: json[key],
},
}),
{},
),
);
2019-11-27 13:39:51 +05:30
}
module.exports = function (content) {
this.cacheable && this.cacheable();
2019-11-27 13:39:51 +05:30
content = transform(content, this.context, this.rootContext);
return `import { defineMessages } from 'react-intl';
2019-07-01 09:09:59 +05:30
export default defineMessages(${content})`;
};
2019-11-27 13:39:51 +05:30
module.exports.transform = transform;