-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
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
Bug: React not defined (with TypeScript) #1199
Comments
Just TypeScript (leaving out React) works
This gives the expected result at http://localhost:1234/: "hi there" and no console error. (Repo: https://github.com/felixrabe/e-2018-055-parcel-just-typescript-works) |
(This is not a duplicate of the above comment, but the opposite variant – trying out just React instead of just TypeScript.) Just React (leaving out TypeScript) works
This gives the expected result at http://localhost:1234/: big "Hi there" and no console error. (Repo: https://github.com/felixrabe/e-2018-056-parcel-just-react-works) |
I've just read in the 1.7 blog post something about a rewritten resolver. So I tried again using Parcel 1.6.2 to compare former behavior: First example: TypeScript AND React in Parcel 1.6.2
This already fails in the Terminal (which to me is an improvement compared to 1.7's silent non-Terminal console-only failure):
(I did not bother checking the page in the browser after that message. Just ^C-ed the server.) Second example: TypeScript ONLY in Parcel 1.6.2
Works correctly as with Parcel 1.7.1. Third example: React ONLY in Parcel 1.6.2
This time, Parcel 1.6.2 behaves worse than Parcel 1.7.1 and gives me this Terminal error message, similar to the first example above:
The browser page at http://localhost:1234/ shows:
But the Browser console is empty (probably referring to the system console here). To fix it, I can ^C the server, manually install the missing deps, and restart the server:
... and http://localhost:1234/ correctly greets me with a big fat "Hi there" and no error. |
I encountered this as well recently. Using |
@felixrabe Use what @gnijuohz suggests and it will be fine. |
@felixrabe This is a known difference between how babel and typescript handle es5 code when imported via es6 imports respectively. If you want the babel behaviour in TypeScript you can enable it via the |
Thanks @shunia and @marvinhagemeister, I'm new to Typescript :) {
"compilerOptions": {
"allowSyntheticDefaultImports": true
}
} and that didn't fix it. Went back to the docs for this option and it says,
Since it doesn't affect code emit, then it's expected nothing changes in our case... |
This {
"compilerOptions": {
"esModuleInterop": true,
"jsx": "react"
}
} (Make sure to make a small change to the
|
@dtinth esModuleInterop: true is in fact the default in Parcel. If you just use So currently two approaches work:
{
"compilerOptions": {
"jsx": "react"
}
} |
Hi @DeMoorJasper currently Parcel's default for |
@gnijuohz the original thought behind it was the whole pipeline concept inside parcel. Where it starts with typescript and than goes through ts-compiler than babel, therefore I thought this was a good default as babel would take care of JSX |
I'll close this off as there are several responses that answer the question |
Yes, this is fine to be closed. I'm still investigating, but the key to resolve my issue seems to be (exactly as @gnijuohz stated above) to add
This seems to be all that is required, and it makes sense to require it. The option |
clearing the cache {
"compilerOptions": {
"jsx": "react"
}
} |
IMO, |
@dtinth Feel free to open a PR, the original thought behind the configuration of ts-compiler was that react should be processed by babel, as we use something we call the processing pipeline, which basically processes, the asset as follows But after thinking about it, it might not make so much sense after all, because using the original concept you would have to config babel to process react (although in theory parcel should detect react code automatic and change babel config accordingly) Feel free to open a PR with the improved config |
@felixrabe thanks for this bit in particular...
Just caught us out when importing "react-focus-trap" in our component library because we were using Parcel to preview the component in development and the TypeScript compiler without that option for production. |
This should be in the doc. Will try to add it. |
I had to do:
not super clean haha |
@tj did you try the solutions above? {
"compilerOptions": {
"esModuleInterop": true,
"jsx": "react"
}
} |
We ran into this even though the TS config we extend already included {
// This config already sets "jsx" to "react"
"extends": "shared/tsconfig",
// But we still have to override Parcel's default config
"compilerOptions": {
"jsx": "react"
}
} |
@denkristoffer Thx for saving my life! And it seems a bug to me. |
One more important thing. You'll need to |
I have the same issue and I don't use typescript at all. This already produces the error: index.jsx import { useState } from "react" |
I could fix it by simply adding |
When I install both parcel and typescript on my computer, how can I make parcel use the global typescript instead of downloading a copy when I run the parcel command |
That's the main catch here. Why doesn't this happen automatically? |
You saved me!! |
So a general question @marvinhagemeister , or anyone else who can explain. If typescript requires |
@adjenks The topic here only affects modules authored in commonjs or any other module system than es modules. They are not compatible out of the box and both Typescript and babel try to do their best to translate those to es modules. And that's where the slight difference in behavior of these tools resides. There is no right or better way to do the translation, hence the difference in babel and TS. Browsers straight up refuse to load anything other than es modules (or classic script tags). The issue here only pops up if you mix and match different module systems which is common in the react ecosystem as react isn't authored in es modules. Will one be able to stop transpiling module formats and have the code continue to work? That's the hope here, but it requires cooperation from library authors. |
@marvinhagemeister Okay, that makes sense, thank you for clarifying that. 👍 |
This. All my projects with parcel have a "start": "rm -rf .cache && parcel src/index.html" Why isn't this automated? |
This commit adds the typescript declaration file. It further upgrades parcel to version 2. This needed to be done to get rid of the `Invalid Version: undefined` error (see parcel-bundler/parcel#5943). While doing so I also had to change the react import in the `example/index.js` due to a parcel bug (see parcel-bundler/parcel#1199).
I'm aware that this is very likely a duplicate of some other bug. I'll research and close ASAP, just dumping my findings here for reference.
Edit – Related issues:
Edit – Using Node v9.11.1 btw. (Forgot to mention it below.)
Creating a new project like this (I've copy and pasted these lines to verify): (macOS 10.13.4, yarn 1.6.0, Parcel 1.7.1)
... running Parcel:
😯 Current Behavior
... leads to an empty page on http://localhost:1234/ and this console message: (Firefox 59)
🤔 Expected Behavior
I'd expect the page to contain a big fat Hi there and no console error.
Resulting files from above process
(plus a .gitignore can be found here: https://github.com/felixrabe/e-2018-054-parcel-typescript-react)
package.json
index.html
app.tsx
The text was updated successfully, but these errors were encountered: