Try using dynamic imports or webpack.
Dynamic imports
Your import will look something like this:
let logo;
import(`./assets/flavour/${process.env.REACT_APP_API_BASE_DIR}/logo.png`).then((module) => {
logo = module.default; // <= base64 image
});
...
<img src={logo} alt="logo" />
But in this way you will get the desired logo value only at the next render and if logo was loaded before.
Therefore, there is the option to use this useState and imports within our functional components:
export const Logo = () => {
const [logo, setLogo] = useState('');
import(`./assets/flavour/${process.env.REACT_APP_API_BASE_DIR}/logo.png`).then((module) => {
setLogo(module.default);
});
return (
<img src={logo} />
);
};
If you have a lot of such images import, then this component can be rewritten so that it is reusable:
export const Image = ({path, alt}) => {
const [imageSrc, setImageSrc] = useState('');
import(path).then((module) => {
setImageSrc(module.default);
});
return (
<img src={imageSrc} alt={alt} />
);
};
Wepback aliases
The second option that came to my mind is try using Webpack aliases. To do this add to your webpack.config.js
module.exports = (env) => ({
// ...
resolve: {
// ...
alias: {
'@assets': path.join(__dirname, 'assets', 'flavour', env.REACT_APP_API_BASE_DIR),
},
},
// ...
})
And then use in your code:
import logo from `@assets/logo.png`;