Is there any way to import all files from a directory? #9979
-
I'm facing a problem that I must dynamically import svg files based on a string, I have a component like this: import { type ComponentProps, type FC, useEffect, useRef, useState } from 'react';
export const useLazySvgImport = (name: string) => {
const importRef = useRef<FC<ComponentProps<'svg'>> | null>(null);
const [loading, setLoading] = useState(true);
const [error, setError] = useState<Error | null>(null);
useEffect(() => {
const importIcon = async () => {
try {
if (name) {
const module = await import(`react:~/assets/${name}.svg`);
importRef.current = module.default;
setLoading(false);
}
} catch (err) {
setError(err as Error);
setLoading(false);
}
};
setLoading(true);
setError(null);
importIcon();
}, [name]);
return {
error,
loading,
Svg: importRef.current,
};
}; However, since import path is not static I think parcel is not including all the svgs in the final bundle. So I thought to do something like |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
https://parceljs.org/features/dependency-resolution/#glob-specifiers |
Beta Was this translation helpful? Give feedback.
https://parceljs.org/features/dependency-resolution/#glob-specifiers