Open Leninsky opened 7 years ago
Weird. Works for me without the custom config.
Aaaah sorry, didn't see you use jsx extension. Works out of the box if you use .js instead
You can also use Babel-plugin-module-resolver
to add support for more extensions
Spent a bunch of time trying to figure this out as well.
Yeah, I spend a while figuring this out as well. Here's an example using a babel plugin:
module.exports = {
module: {
rules: [
{
test: /\.jsx?$/,
loader: "babel-loader",
options: {
plugins: ['transform-decorators-legacy'],
presets: ["es2015", "react", "stage-1"]
}
}
]
},
resolve: {
extensions: ['.js', '.jsx']
}
}
@jcity If you use babel-module-resolver
you can add extension support using only .babelrc
file
This bit me as well. I'm very new to electron and fairly new to webpack, but not particularly new to react.
Is the file extension intentionally supposed to be .js
for some reason? It does work, but if it has JSX in it then not using the .jsx
extension throws my editor off.
Yeah, I ran into the same thing... If you're using JSX/TSX the file extension needs to be .jsx/.tsx respectively. @brandonweiss
@brandonweiss regardless of this specific issue - always try to let your editor know who's boss and who's not. :) I mean.. you shouldn't have to make project decisions to please an IDE or editor, it should be the other way round.
@loopmode Please add this to the docs as it was really confusing for me, but eventually I figured it out through Github issues
Beginners won't be able to find this so I think it should be mentioned in the Add-ons>React JSX
docs & it gives error without custom config like webpack.renderer.config.js
Here's my example 👉 https://github.com/deadcoder0904/end-of-life
Note - Directly installing @babel/preset-react
does not work as stated in the docs
In case someone runs into this who, like me, also wants to use Typescript, it appears that you can sidestep this issue by using Typescript instead of Babel to process the .jsx/.tsx files. Don't install @babel/react
. Instead, follow the instructions for the Typescript add-on, then add JSX support to tsconfig.json
.
So far everything (HMR, etc.) seems to be working correctly…not sure if this setup will cause issues down the road?
@deadcoder0904 Thanks for providing your solution. I ran into the same issue and your repo and comment here were very helpful.
@starkos it is not working for me. Can't get JSX to work at all, not with Babel and not with Typescript.
@starkos Typescript + TSX didn't work for me even though I followed the docs precisely.
@deadcoder0904 this solution gets JSX to work but breaks HMR for me.
For me the issue was having @babel/preset-react
in dependencies
. It must be placed in devDependencies
.
Any devDependencies - not regular dependencies - starting with @babel/preset or @babel/plugin (same with older babel-* variants) get automatically added to the babel-loader config in webpack.
in package.json
I am using
In the documentation it says that the presets are automatically handled.
All direct dev dependencies babel-preset-* and babel-plugin-* are automatically configured also.
The fix was to add :
in my webpack config for renderer.