Webpack: Is it possible to evaluate a javascript expression at compile time?

I am trying to configure webpack for my project. The project is large enough and is available in several languages. I want each of my entry points provided in each language as separate files. My language files are not just JSON, but JavaScript. Therefore, the i18n plugin does not meet my needs.

The solution is similar to the i18n plugin:

var languages = ['en', 'fr', 'de'];
module.exports = languages.map(function (lang) {
  return {
    name: lang,
    // some other language-dependent config
  }
})

Then, in some of my scripts, I want to require a localization file using an environment variable:

var lang = ...; // some environment variable, available only at compile time
var l10n = require('./lang/' + lang);

But by default, webpack tries to keep this expression between parentheses, assuming that it will later be in the browser.

So, is there a way to tell webpack to immediately evaluate this?

Or maybe someone has a better solution to my problem?

+4
1

Webpack DefinePlugin .

, :

var l10n = require('./lang/' + APPLICATION_LANGUAGE);

,

plugins: [
  new webpack.DefinePlugin({
    APPLICATION_LANGUAGE: JSON.stringify(lang)
  })
]

script ​​ "lang" env - .

+10

All Articles