Skip to content
This repository was archived by the owner on Dec 16, 2021. It is now read-only.
This repository was archived by the owner on Dec 16, 2021. It is now read-only.

Can I write an npm package compatible with preact and react? #497

Open

Description

@qwertie

As I was typing npm install react, I thought to myself: I'm sure my package would be preact-compatible, but if anybody writes npm i mypackage they are going to get react as a dependency, which maybe they don't want. Is there a way I can publish this thing in such a way that it's compatible with both?

Activity

  1. marvinhagemeister commented on Aug 16, 2018

    @marvinhagemeister
    Member

    Third party packages usually opt for one of the following ways to be compatible with react/preact:

    1. Kepp importing react and tell users to overwrite react import via webpack aliasing
    2. Publish dedicated preact entrypoint mypackage/preact
    3. Publish a completely separate package mypackage-preact + mypackage-react
  2. qwertie commented on Aug 16, 2018

    @qwertie
    Author

    Hmm, but if you go with option 1 or 2 - what do you do, mark React as an optional dependency?

  3. marvinhagemeister commented on Aug 16, 2018

    @marvinhagemeister
    Member

    For all options you'd mark react or preact as a peerDependency. Otherwise the user would likely end up with multiple versions of (p)react

  4. developit commented on Aug 17, 2018

    @developit
    Member

    You might find the technique shown here to be useful:
    https://jasonformat.com/universal-vdom-components-with-factory-loader/

  5. qwertie commented on Aug 17, 2018

    @qwertie
    Author

    I don't think that technique would work in TypeScript (which I'm using) as the compiler would be unable to load the pseudo-module. And I doubt the technique could be used with Parcel (which I'm also using). Hmm...

  6. developit commented on Aug 24, 2018

    @developit
    Member

    I've always wanted to release an npm package that tries to infer which virtual-dom library a given project is using. It'd be neat:

    import { createElement, Component, cloneElement, render } from 'any-vdom';
    // if you have preact installed you get the methods from preact
    // if you have react installed you get its methods
    // etc
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions