-
-
Notifications
You must be signed in to change notification settings - Fork 345
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Unable to use alias Preact in next
branch
#1552
Comments
This doesn't seem to be webpack related since the error originates on the Node server side. This probably has to do with the fact that react-cosmos/packages/react-cosmos-core/src/index.ts Lines 30 to 34 in 593608c
One potential solution is to decouple the main Also there are some |
Thanks for looking into this. I guess converting every place that imports react to a dynamic import would complicate things too much? I had a quick look at how to set up import aliases with node and it looks like it might be possible with |
I tried getting this to work today by using |
I also tried using module-alias and a #!/usr/bin/env node
import moduleAlias from 'module-alias';
moduleAlias.addAliases({
react: 'preact/compat',
'react-dom': 'preact/compat',
});
await import('react-cosmos/bin/cosmos.js'); I just get:
|
I got a bit closer today using Node's module-based permissions to emulate import maps. policy.json {
"dependencies": true,
"scopes": {
"": {
"cascade": true,
"integrity": true,
"dependencies": {
"react": "./node_modules/preact/compat/dist/compat.mjs"
}
}
}
} (It might be necessary to use Then run:
I now hit a different error though:
That might just be because my webpack config is very complicated and the aliases for react there are not being picked up correctly. |
@birtles Thanks for posting these updates! FYI the Line 50 in 593608c
And this is how aliases are checked: Lines 66 to 76 in 593608c
Also as a general idea. Not sure if this works but couldn't you point |
Thank you! I tried the local paths approach and that works too. That might be better than using Node's module-based permissions since they're still experimental. I haven't debugged why React Cosmos can't find the aliases in my current webpack config but I've confirmed that if I make a separate webpack config for cosmos everything seems to work. I'll probably try to debug that later. For reference, for the local paths approach I used the following {
"name": "react-polyfill",
"version": "4.0.0",
"private": true,
"description": "A wrapper around preact-compat so react-cosmos will load Preact instead of React",
"main": "../node_modules/preact/compat/dist/compat.js",
"module": "../node_modules/preact/compat/dist/compat.module.js",
"umd:main": "../node_modules/preact/compat/dist/compat.umd.js",
"source": "../node_modules/preact/compat/src/index.js",
"types": "../node_modules/preact/compat/src/index.d.ts",
"peerDependencies": {
"preact": "^10.0.0"
}
} And referenced it from the root "react": "file:./react-polyfill/",
"react-cosmos": "6.0.0-beta.6",
"react-cosmos-plugin-webpack": "6.0.0-beta.6",
"react-dom": "file:./react-polyfill/", Regarding this issue, I guess the resolution is to add something to the docs about how to use Preact. |
I went back to this today but after rebasing my work somehow the local paths approach stopped working. As best I can tell, node initially wants to require react using CJS when running When it gets to i.e. node will reject something like the following:
I might have to go back to the policy approach after all. |
The module-based approach seems to work fine so I'll stick with that. It basically boils down to:
{
"dependencies": true,
"scopes": {
"": {
"cascade": true,
"integrity": true,
"dependencies": {
"react": "./node_modules/preact/compat/dist/compat.mjs"
}
}
}
}
Also, I debugged why |
What's wrong?
I'm trying to set up react-cosmos@next on a project using Preact but I get include errors due to some files including
react
:I've tried adding a separate webpack config for cosmos like the following but, as far as I can tell, the error occurs before that is applied:
Steps to reproduce
Sorry, it's going to take me a while to reduce this. But it should be possible to reproduce on any existing project by removing react from the project's dependencies and installing
preact
instead, then aliasing it in webpack config as shown above.Useful info
react-cosmos@next
Additional context
This used to work after I fixed it in #1316.
The text was updated successfully, but these errors were encountered: