Giter Site home page Giter Site logo

Tree Shaking about webcomponents HOT 2 OPEN

phosphor-icons avatar phosphor-icons commented on May 2, 2024 7
Tree Shaking

from webcomponents.

Comments (2)

rektdeckard avatar rektdeckard commented on May 2, 2024

Hey @ksweetie, sorry for taking so long to respond 😅.

I recommend importing each icon separately from their fully-qualified path, rather than using the barrel file. Since the import side-effect is what registers the custom elements, you don't need to use the values and can just stick all of these in a file somewhere, then source that file:

icons.ts

import "@phosphor-icons/webcomponents/PhAvocado";
import "@phosphor-icons/webcomponents/PhCheese";
import "@phosphor-icons/webcomponents/PhChefHat";

main.tsx

import "./icons";

export default function App() {
  return (
    <main>
      <ph-avocado size={32} weight="duotone" color="darkolivegreen" />
      <ph-cheese size={32} weight="duotone" color="goldenrod" />
      <ph-chef-hat size={32} weight="duotone" color="white" />
    </main>
  );
}

This is sorta pseudo-code, because I don't know what framework (if any) you're working in, but as long as the icons are imported for effect, you'll be able to use them. Some JSX implementations will need to be taught about the custom elements to have correct type inference in your editor — for example in SolidJS I would include the following in my icons file:

icons.ts

import type { IconAttrs } from "@phosphor-icons/webcomponents";
import "@phosphor-icons/webcomponents/PhAvocado";
import "@phosphor-icons/webcomponents/PhCheese";
import "@phosphor-icons/webcomponents/PhChefHat";

declare module "solid-js" {
  namespace JSX {
    interface IntrinsicElements {
      "ph-avocado": IconAttrs;
      "ph-cheese": IconAttrs;
      "ph-chef-hat": IconAttrs;
    }
  }
}

I believe Typescript should pick up the JSX extension automatically when using them in HTML or React files.

from webcomponents.

rektdeckard avatar rektdeckard commented on May 2, 2024

Just to put a finer point on it, as far as I know, most bundlers will not attempt to tree-shake if modules are marked as having side-effects. When importing something directly from the barrel file as you showed in your example, the bundler has no way of knowing that it is OK not to load the other imports in that file besides PhArrowBendUpRight, because important things might happen when a module is loaded.

We rely on those side-effects to register the custom element type on the window, so we can't really do the barrel-file package structure. We could, but we'd have to then force you to call some method manually to register the custom elements you use:

import { register, PhArrowBendUpRight } from "@phosphor-icons/webcomponents";

register(PhArrowBendUpRight);

from webcomponents.

Related Issues (2)

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.