0👍
✅
require
does not work perfectly dynamically, because webpack does not know the path of your path
argument. However, if you add some kind of template literal in your require
it will work (e.g. my-fonts/${path}
).
import
imports something dynamically and lazy.
You can find more details on Webpack import
function here.
0👍
For sake of clarity
const getFontDeclaration = (path) => {
// using a variable directly will not work
require(path);
// removing a part of the path and prepending it with a static string will work
const reqPath = url.replace('@foo/', '');
require(`@foo/${reqPath}`);
}
Source:stackexchange.com