-
-
Notifications
You must be signed in to change notification settings - Fork 111
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
Inconsistent whitespace handling in JSX between vite dev
and vite build
#360
Comments
Start a new pull request in StackBlitz Codeflow. |
This is because the react plugin enables root.render(<div>{
"Lorem"}
ispum </div>);
//^ this is interpreted as no space here. So it maybe an issue of babel which should preserve leading spaces (using |
@hyrious thanks for your response. Is there a workaround to get development work as production without having to change the code at all places. |
I was checking the transform using https://github.com/antfu-collective/vite-plugin-inspect/ and as @hyrious pointed out, the transform got broken by babel's console.log(<div>{
"Lorem"
} ispum </div>)
// ⇓ vite:react-babel
console.log(<div>{
"Lorem"}
ispum </div>);
// ⇓ vite:esbuild
console.log(/* @__PURE__ */ jsxDEV("div", { children: [
"Lorem",
"ispum "
] }, void 0, true, {
fileName: "/home/hiroshi/code/others/vite-plugin-react/playground/react/App.jsx",
lineNumber: 9,
columnNumber: 13
}, this)); As a workaround, you can lower JSX during babel transform using vite-plugin-react/playground/compiler/vite.config.ts Lines 5 to 15 in 0f7684a
|
After little bit of struggle, realised compiler is not required. It is also giving me error. const babelPlugins = [];
if (command === 'serve') {
babelPlugins.push(['@babel/plugin-transform-react-jsx-development', {}])
} I will give it more testing and let you know if this workaround has a problem. |
Ah, sorry it was probably confusing. Right, what I meant is to only include FYI, I also submitted an issue on babel babel/babel#16774 |
I'm closing this as this as been fixed in Babel and released as v7.25.6. Thanks @hi-ogawa for reporting the issue! |
Describe the bug
Description of the Issue:
After migrating from Create React App (CRA) to Vite, I've encountered an inconsistency in how whitespace is handled in JSX between the development
vite dev
and productionvite build
. Specifically, JSX strings that are expected to have spaces between them in the rendered output are concatenated without spaces when running the development server (vite dev
). However, when building the project usingvite build
, the whitespace is preserved correctly.Dev vs Build
Expected Behavior:
The rendered output in both vite dev and vite build should be consistent and should correctly include the space, i.e., "Lorem ispum".
Additional Context:
This issue might be related to how the Vite development server handles JSX parsing differently from the production build process. Ensuring consistency between development and production builds is important for a smooth developer experience.
Having recently moved from Create React App (CRA), this inconsistency is particularly troubling, as it was not an issue with CRA. Achieving consistent behavior across development and production is crucial to our development workflow.
Minimum reproducible project
whitespace-vite.zip
Note
The reproduction URL is working fine, but when I download the same project and run it in local, it causes the issues.
Reproduction
https://stackblitz.com/edit/vitejs-vite-luvdvh?file=index.html
Steps to reproduce
vite dev
.vite build
and serve the built files.System Info
Used Package Manager
yarn
Logs
No response
Validations
The text was updated successfully, but these errors were encountered: