Skip to content

Instantly share code, notes, and snippets.

@juliomerisio
Created February 20, 2021 01:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save juliomerisio/627ab840531118131eb855daf18a5370 to your computer and use it in GitHub Desktop.
Save juliomerisio/627ab840531118131eb855daf18a5370 to your computer and use it in GitHub Desktop.
Require.context example
function getFileNames() {
return require.context(
'./',
true,
process.env.NODE_ENV === 'production' ? /\.js$/ : /\.ts$/
)
}
const context = getFileNames()
const files = context
.keys()
.filter((fileName) => fileName !== './index.ts')
.reduce((translations, fileName) => {
const fileTranslations = context(fileName)
const [language] = fileName
.replace('./', '')
.replace(process.env.NODE_ENV === `production` ? '.js' : '.ts', '')
.split('/')
return {
...translations,
[language]: fileTranslations[language],
}
}, {})
export const en = files
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment