Last active
May 21, 2018 08:42
-
-
Save ogonkov/bb415854f6a27e39471d391672e43003 to your computer and use it in GitHub Desktop.
Reduce `charmap.json` of `transliteration` w/ Webpack loader
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
const ucs2decode = require('transliteration/lib/node/utils').ucs2decode; | |
// getAlphabet is just function that returns array of required chars for transliteration | |
const transliterationRanges = getAlphabet('а', 'я') | |
// A little bit of magic from `transliteration` sources, that used | |
// for searching symbols by charmap.json | |
.map((char) => ucs2decode(char) >> 8) | |
// Leave only unique values | |
.filter((char, i, chars) => ( | |
chars.includes(char, Math.min(i + 1, chars.length)) === false | |
)); | |
exports.optimizeTransliterationCharmap = function optimizeTransliterationCharmap() { | |
return { | |
module: { | |
rules: [ | |
{ | |
test: require.resolve('transliteration/data/charmap.json'), | |
use: [ | |
{ | |
loader: 'modify-json-loader', | |
options: { | |
// Convert map ranges to loader format | |
include: transliterationRanges.map((range) => `/${range}`), | |
// A little bit of magic for loader to work | |
stringified: true | |
} | |
} | |
] | |
} | |
] | |
} | |
}; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment