Skip to content

Instantly share code, notes, and snippets.

@explorigin
Last active September 30, 2019 16:13
Show Gist options
  • Save explorigin/29dd6e8a62eac9edf4f7e97278c5d44a to your computer and use it in GitHub Desktop.
Save explorigin/29dd6e8a62eac9edf4f7e97278c5d44a to your computer and use it in GitHub Desktop.
Webpack loader for converting Smartling JSON files into simple key-value JSON mapping.
module.exports = function loader(source) {
if (this.cacheable) this.cacheable();
let value = typeof source === 'string' ? JSON.parse(source) : source;
// Filter out smartling metadata.
const metadata = value.smartling;
if (metadata !== undefined) {
const path = metadata.translate_paths.path.split('/')[1];
delete value.smartling;
Object.keys(value).forEach((key) => {
value[key] = value[key][path];
});
}
value = JSON.stringify(value)
.replace(/\u2028/g, '\\u2028')
.replace(/\u2029/g, '\\u2029');
return `module.exports = ${value}`;
};
// under module.rules, include this rule.
{
test: /\.json$/,
use: ['./smartling-json-loader.js'],
type: 'javascript/auto',
},
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment