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

39 lines
1.1 KiB
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) {
2020-05-24 04:38:24 +05:30
const json = JSON.parse(src);
2020-05-24 04:38:24 +05:30
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, '.');
2020-05-24 04:38:24 +05:30
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) {
2020-05-24 04:38:24 +05:30
this.cacheable && this.cacheable();
2019-11-27 13:39:51 +05:30
2020-05-24 04:38:24 +05:30
content = transform(content, this.context, this.rootContext);
2020-05-24 04:38:24 +05:30
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;