“Unexpected token ‘<'" with electron forge + vite + react

huangapple go评论52阅读模式
英文:

"Unexpected token '<' " with electron forge + vite + react

问题

I'm building an electron app using electron forge, the vite template, and react. I built my app using:

npm init electron-app@latest my-new-app -- --template=vite

This displays the hello world vite application just fine. Then I installed react:

npm install --save react react-dom

This is my renderer.js file:

import React from 'react';
import ReactDOM from 'react-dom/client';

import App from './App.jsx';

const el = document.getElementById('root');
const root = ReactDOM.createRoot(el);

root.render(<App />);

But when I execute the program, I get the following error: caught SyntaxError: Unexpected token '<', and it points to the line root.render(<App />);

Additionally, I've installed @vitejs/plugin-react, and my vite.renderer.config.mjs looks like this:

import { defineConfig } from 'vite';
import react from '@vitejs/plugin-react';

// https://vitejs.dev/config
export default defineConfig({
  plugins: [react()],
})

What I'm reading online is that it's not transpiling the JSX code to JavaScript properly, and most of the solutions involve using Babel with Webpack to accomplish this. However, my understanding was that Vite was supposed to provide this function, and I haven't found any help with Vite.

Edit:

Here is App.jsx:

import React from 'react';

const App = () => {
  return (
    <div>
      <h1>Hello World</h1>
    </div>
  )
};

export default App;
英文:

I'm building an electron app using electron forge, the vite template, and react. I built my app using

npm init electron-app@latest my-new-app -- --template=vite

Which displays the hello world vite application just fine. Then I installed react

npm install --save react react-dom

This is my renderer.js file

import React from &#39;react&#39;;
import ReactDOM from &#39;react-dom/client&#39;;

import App from &#39;./App.jsx&#39;

const el = document.getElementById(&#39;root&#39;);
const root = ReactDOM.createRoot(el);

root.render(&lt;App /&gt;);

But when I execute the program I get the following error. caught SyntaxError: Unexpected token &#39;&lt;&#39; and it points to the line root.render(&lt;App /&gt;);

Additionally I've installed @vitejs/plugin-react and my vite.renderer.config.mjs looks like this.

import { defineConfig } from &#39;vite&#39;;
import react from &#39;@vitejs/plugin-react&#39;

// https://vitejs.dev/config
export default defineConfig({
  plugins: [react()],
})

What I'm reading online is that it's not transpiling the jsx code to javascript properly, and most of the solutions involve using babel with webpack to accomplish this. However, my understanding was that vite was supposed to provide this function and I haven't found any help with vite.

edit:

Here is App.jsx

import React from &#39;react&#39;;

const App = () =&gt; {
  return (
    &lt;div&gt;
      &lt;h1&gt;Hello World&lt;/h1&gt;
    &lt;/div&gt;
  )
};

export default App;

答案1

得分: 1

Vite 不支持在 .js 文件中使用 JSX,您需要将 renderer.js 重命名为 renderer.jsx。这里有关于这个主题的讨论

英文:

Vite doesn't support JSX in .js files, you need to rename renderer.js to renderer.jsx. Here is a discussion on this subject.

huangapple
  • 本文由 发表于 2023年5月14日 13:47:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/76246035.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定