Comments (9)
Have you tried including the file extension in the import path?
from react.
Have you tried including the file extension in the import path?
Hello, yes it doesnt work, here is a repo for easy reproducible! with working example with remix for example and phosphor not working!
https://github.com/hugobarragon/phosphor-icon-issue
from react.
This works for me.
- Vite 4.5.1
import { lazy, Suspense } from 'react';
import { IconProps } from '@phosphor-icons/react';
const ICONS_PATH = '../../../../node_modules/@phosphor-icons/react/dist/csr';
const ICON_EXTENSION = '.mjs';
const lazyIconMap = import.meta.glob('../../../../node_modules/@phosphor-icons/react/dist/csr/*.mjs');
type Props = IconProps & { name: string };
const LazyIcon = ({ name, ...iconProps }: Props) => {
const Icon = lazy(async () => {
const module = await lazyIconMap[`${ICONS_PATH}/${name}${ICON_EXTENSION}`]();
return {
default: module[name]
};
});
return (
<Suspense fallback={null}>
<Icon {...iconProps} />
</Suspense>
);
};
export default LazyIcon;
from react.
@chinnawatp hi, you are refering to node modules, we are using yarn 4 with pnp feature, can you make a repo like i did so i can test it out? does that import.meta.glob feature works the same? looks like vite feature that does not exist on cra
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/import
with vite i tested with react-ts plugin yarn create vite my-app --template react-ts
from react.
chinnawatp
After analysing your approach its not ideal, its importing everything in the package and then in runtime just acessing as an object lazyIconMap[
${ICONS_PATH} ...` the bundle size sould be huge, send me a screen show of the network when you load the page to check it out our the repo please
from react.
chinnawatp
After analising your approach its not ideal, its importing everything in the package and then in runtime just acessing as an object
lazyIconMap[
${ICONS_PATH} ...` the bundle size sould be huge, send me a screen show of the network when you load the page to check it out our the repo please
Hey @hugobarragon ,
Sorry for the late reply. Please check out this repo https://github.com/chinnawatp/react-vite-phosphor-dynamic-import-poc.
from react.
chinnawatp
After analising your approach its not ideal, its importing everything in the package and then in runtime just acessing as an object
lazyIconMap[
${ICONS_PATH} ...` the bundle size sould be huge, send me a screen show of the network when you load the page to check it out our the repo pleaseHey @hugobarragon ,
Sorry for the late reply. Please check out this repo https://github.com/chinnawatp/react-vite-phosphor-dynamic-import-poc.
Hi, ok i tested and it works on vite with import meta glob, but only on vite because import.met.glob does not exist on other bundlers, the problem here is i have to transpile code, its a npm module so on our company we can reuse this costumized, in order to release a npm module transpiled it must be es6 so dynamic import its the normal and only solution, vite transforms import.meta.glob as it says on their docs to import(...) but its a theis bundler thing, it does not exist anywhere else, cra uses babel, and babel its what transpiles code or tsc for modules, vite only builds final work aplications like cra, to run directly on web
Vite builds import.meta.glob to import() so the problem precists, on vite yarn dev just doesnt show the error, but it runs
import.meta.glob is importing on build time and not run time, this is building to everything because of *
, csr/*.mjs'
on the code , in run time you will have a json with paths and compiled to dynamic imports, kinda of the same
another problem is on your code you have path to node modules and it does not work with yarn pnp there is no node modules
from react.
@hugobarragon I may not be understanding your use-case fully, but you should probably not be transpiling dynamic imports into library code. If you are deploying a customized version of the icon library for internal use at your company, just re-export the icons you need in that package. Your app code is the one who will dynamic import from that package.
from react.
@rektdeckard Yes I went with kinda of that approach, but for this to work, i had to import and re export all existing icons from phosphor so that the rexported ones, had file termination .mjs - > .tsx so that dynamic import would work, this because .mjs is ECMAScript modules and not .cjs that is CommonJS, and to avoid migrating the aplications to ECMAScript modules
from react.
Related Issues (20)
- Add `@preview` tag to enable icon previews in jsdoc HOT 4
- Could not find a declaration file when using SSR HOT 2
- SSR Components missing default size="1em"
- SSR component types are not resolving
- allow to add Contributing.md file.
- Importing icons from `@phosphor-icons/react/dist/ssr` renders the icons, but gives issues in the editor HOT 5
- TypeError: Cannot create property 'displayName' on number 'Infinity' HOT 7
- Icon instance className overwrites context className HOT 8
- New Twitter (x.com) icon? HOT 1
- Cannot find package on node 20 HOT 6
- ./node_modules/@phosphor-icons/react/dist/lib/context.mjs Can't import the named export 'createContext' from non EcmaScript module (only default export is available) HOT 10
- UnhandledRejection Error: Cannot Find React Package HOT 23
- 'use client' in client icons and IconContext HOT 2
- React warning after update to 2.1.4 HOT 1
- Issue updating past 2.1.3 HOT 2
- Incorrect exports specified in `package.json` HOT 1
- Jest test fails if importing directly from `/dist` folder
- Custom icons: Typescript and ESLint errors in Next.js 14 HOT 1
- React web pack build error in phosphor icons HOT 5
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from react.