preactjs / preact-compat

ATTENTION: The React compatibility layer for Preact has moved to the main preact repo.
http://npm.im/preact-compat
MIT License
950 stars 148 forks source link

Should babel module loader work for react / reactish dependencies inside node_modules? #513

Closed nineohnine closed 5 years ago

nineohnine commented 5 years ago

I am trying to build a ssr & client side app using preact. I am using babel-node with your module-loader configuration. I am getting errors from inside my dependency that it cannot load resolve react. Is this to be expected ? I can resolve react (preact-compat) from my project files. But when a dependency of my project does it throws. Is this the expected behavior or am i doing something wrong?

Thanks!

marvinhagemeister commented 5 years ago

I am using babel-node with your module-loader configuration

Can you elaborate on your setup? In general mixin preact with react only works by rewriting (aka aliasing) imports. If you're using babel-node alone you'll likely need to add a plugin that switches out imports to react.

nineohnine commented 5 years ago

yep so I am using this setup in my babelrc from the readme:

{
    // ...
    "plugins": [
        ["module-resolver", {
        "root": ["."],
        "alias": {
            "react": "preact-compat",
            "react-dom": "preact-compat",
            // Not necessary unless you consume a module using `createClass`
            "create-react-class": "preact-compat/lib/create-react-class",
            // Not necessary unless you consume a module requiring `react-dom-factories`
            "react-dom-factories": "preact-compat/lib/react-dom-factories"
        }
        }]
    ],
    "presets": [
        "react"
    ]
    // ...
}
developit commented 5 years ago

If you're not running Babel on node_modules (most people don't), the aliases set in your Babel config are not applied to those files.

You should use module-alias instead, which applies to all requires:

https://github.com/developit/preact-compat/issues/299#issuecomment-280499913