valotas / preact-context

React new Context API for preact
Other
151 stars 8 forks source link

Aliasing create-react-context #17

Closed czycha closed 6 years ago

czycha commented 6 years ago

Hi! This module is great but I have one very minor suggestion. If you change how createContext is exported, this module can then be used as a drop-in replacement for React components that use create-react-context (in the same way that you can drop-in preact-compat to replace react-dom).

For example, in Webpack you could do something like:

module.exports = {
    ...
    resolve: {
        alias: {
            "react": "preact-compat",
            "react-dom": "preact-compat",
            "create-react-context": "preact-context"
        }
    },
    ...
}

However, since create-react-context exports on default and preact-context exports on createContext, this is not possible without something hacky.

This would be a breaking change so I understand if you'd want to refrain from doing this.

valotas commented 6 years ago

Hi @czycha, I could easily export both default and a named function but this does not mean that it would work. Can you share some more info regarding your setup in order to experiment a little bit with that?

czycha commented 6 years ago

@valotas So here's an example repo that puts my plan into action: https://github.com/czycha/example-preact-unstated. It uses a packed version of my fork in place of preact-context. Basically the example repository uses the React state management system Unstated which would usually depend on create-react-context. By changing up the Webpack config (see above comment), I can just have it alias that module with preact-context and it works perfectly!

valotas commented 6 years ago

Thanks a lot for putting it together. It looks like webpack can handle export.default just fine, so I'll release a fix later today.