Last active
September 30, 2019 16:13
-
-
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}`; | |
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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