L
L
LionG2020-06-28 14:15:25
JavaScript
LionG, 2020-06-28 14:15:25

Is it possible to somehow use constants in the import'a path?

import default_locale from `~static/i18n/${config.lang}.json`
messages[config.lang] = default_locale


I'm using i18n with several translations and need to load a "default" translation which will be used in the prerender. In this case, the application is sent to the pre-render several times for different locales. There is a reset settings file in which the default locale code is specified, but unfortunately I cannot substitute it as a constant in the import path.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Aetae, 2020-06-28
@LionG

Only dynamic import()supports the use of variables , while all files corresponding to the pattern get into the bundle.
If we are talking specifically about constants , then at the compilation webpack stage it still does not know what you have in there config. It doesn't run your code - it builds it.
If your config file is static, then you can include it in the webpack config itself and make there, for example, an alias of the form:

`~static/i18n/CONFIG_LANG.json`:  path.resolve(`./static/i18n/${config.lang}.json`)
and in the code, respectively, write:
import default_locale from `~static/i18n/CONFIG_LANG.json`

This is the easiest way, but in general, you can do a lot more with webpack. (even write your own loader, which will still (try) to decode the constants in the import)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question