Dynamic Javascript Imports

0

Hello, I need to dynamically import the paths to the assets of an application, for example:

var cliente = clienteX

import ../../caminho/${cliente}/caminho/arquivo.less

It will not be changed at runtime, it would just pull from a configuration file, which client it will pull the assets in, given that the Core system is the same.

So I researched import is static, so I wanted some help or idea of some workaround for that situation.

Thank you :)

Edit 1: Structure Example

  • System
  • Customers

    • customer 1

      • css
        • main.css
    • customer 2

      • css
        • main.css

Examples of imports:

require ('../../Clientes/'+ cliente + '/css/main.css');

import ../../Clientes/${cliente}/css/main.css;

    
asked by anonymous 28.03.2017 / 15:42

1 answer

0

You can not create imports dynamic. As you mentioned, the import of javascript is static, so it does not accept strings interpolation.

What you can do is use require .

require ('../../Clientes/'+ cliente + '/css/main.css');

or

require ('../../Clientes/${cliente}/css/main.css');

They should work. If they do not work your mistake should be another one. Can you verify that path is correct? If you do the same static%% with the path of one of the clients does it work?

Example:

require ('../../Clientes/cliente1/css/main.css');

In this case, can you verify that the value of the require variable is correct?

If you can not statically import, check your compiler configuration ( cliente ) to check if there is no error in Babel .

If it goes wrong, can you post the error log to the terminal?

    
28.03.2017 / 16:22