Coder Social home page Coder Social logo

Comments (9)

rektdeckard avatar rektdeckard commented on May 1, 2024

Have you tried including the file extension in the import path?

from react.

hugobarragon avatar hugobarragon commented on May 1, 2024

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.

chinnawatp avatar chinnawatp commented on May 1, 2024

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.

hugobarragon avatar hugobarragon commented on May 1, 2024

@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.

hugobarragon avatar hugobarragon commented on May 1, 2024

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 avatar chinnawatp commented on May 1, 2024

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.

hugobarragon avatar hugobarragon commented on May 1, 2024

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.

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.

rektdeckard avatar rektdeckard commented on May 1, 2024

@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.

hugobarragon avatar hugobarragon commented on May 1, 2024

@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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.