React JS project - treat *.js files as JSX #3112
-
I have a project created with "create-react-app". Each JS file in it is actually JSX. I would like to try ViteJS to develop such project. The problem is that when I create ViteJS 'react' template project and try to rename '.jsx' to '.js', I am getting syntax errors as Vite parses such files as 'javascript' files. I am wondering if there is a way to tell Vite that all (or some) JS files are actually JSX files... If anyone knows any trick, please, let me know. Thank you. |
Beta Was this translation helpful? Give feedback.
Replies: 6 comments 19 replies
-
You need to rename your files to .jsx, Vite needs to know the difference between a javascript file and JSX file. |
Beta Was this translation helpful? Give feedback.
-
Overall, I found my question here: #769 |
Beta Was this translation helpful? Give feedback.
-
Please look at the discussion here: #3448. I described a workaround there that you could use to test Vite, but a rename is still recommended. |
Beta Was this translation helpful? Give feedback.
-
If you'd like to simplify the process of renaming the files you could try running a simple node script for this. This could easily be customized and improved in many ways, but I just wrote it for my own purposes. Note: In my project, React was always manually imported (pre-v17 pattern), so it made it easier to differentiate between standard js files. const fs = require('fs').promises;
const glob = require('glob');
const chalk = require('chalk');
const matchingReactString = "from 'react'";
glob(
// Modify the paths as you like
'./src/**/*.js',
// Add anything else you want to ignore here
{ ignore: ['./src/**/*.spec.js', './src/**/*.config.js', './src/**/*.test.js', './src/**/*.stories.js'] },
(err, matches) => {
if (err) return;
console.log(chalk.blue`Found these files matching the globs specified:`);
console.log(matches);
matches.forEach(async path => {
const result = await fs.readFile(path);
if (!result) return;
if (result.includes(matchingReactString)) {
const newPath = path.concat('x'); // Pretty much add 'x' to the end to make it 'jsx'
console.log(chalk.yellow`Renaming ${path} to ${newPath}`);
fs.rename(path, newPath);
}
});
}
); |
Beta Was this translation helpful? Give feedback.
-
You can try this cli tool,
|
Beta Was this translation helpful? Give feedback.
-
I totally disagree with renaming files to make vite happy. In my case not all the js files are react components. There are services, redux stores, selectors, redux-thunks and so on. If I keep them js and components jsx, yeah it works. HMR only kicks in if I edit a jsx file, but if I edit any other files, hot-reload kicks in and entire page reloads. All state lost. I also tried to rename all files to jsx, results has not changed. Because import react doesn't exist on those files, instead of hmr, hot-reload kicks in. Vite is great, fast and such but from what I seen in some of the tickets shared, vite collaborators, devs, support is not there yet. One of the posts closed with an answer I will deal with hot-reload for now, but if anyone found a solution much appreciated. |
Beta Was this translation helpful? Give feedback.
Please look at the discussion here: #3448. I described a workaround there that you could use to test Vite, but a rename is still recommended.