How to use .js
instead of .jsx
#3448
-
I understand using Another reason VSCode IntelliSense doesn't recognize import Home from 'components/home' where components/home/index.jsx To make IntelliSense work, I have to do: components/home/index.js So, I think we should give developers the choice to opt-in the usage of |
Beta Was this translation helpful? Give feedback.
Replies: 21 comments 61 replies
-
There aren't plans to include this as a native option in Vite, but since #2991 (released in For example: import { defineConfig } from 'vite'
export default defineConfig(() => ({
esbuild: {
loader: 'jsx',
},
optimizeDeps: {
esbuildOptions: {
loader: {
'.js': 'jsx',
},
},
},
}) |
Beta Was this translation helpful? Give feedback.
-
@marialovesbeans here's a working config: import fs from 'fs/promises';
export default defineConfig(() => ({
esbuild: {
loader: "jsx",
include: /src\/.*\.jsx?$/,
// loader: "tsx",
// include: /src\/.*\.[tj]sx?$/,
exclude: [],
},
optimizeDeps: {
esbuildOptions: {
plugins: [
{
name: "load-js-files-as-jsx",
setup(build) {
build.onLoad({ filter: /src\/.*\.js$/ }, async (args) => ({
loader: "jsx",
contents: await fs.readFile(args.path, "utf8"),
}));
},
},
],
},
},
})); I would only recommend using this to try out Vite in your project - the plugin circumvents Vite's internal onLoad esbuild plugins, and could result in undefined behaviour. As of today, this would make using I saw that microsoft/vscode#123998 is solvable with a |
Beta Was this translation helpful? Give feedback.
-
Hi, did u resolve this problem? May I view your code? thx |
Beta Was this translation helpful? Give feedback.
-
may i see your code? i donnot know what should i modify...
…------------------ 原始邮件 ------------------
发件人: "vitejs/vite" ***@***.***>;
发送时间: 2021年5月31日(星期一) 上午9:26
***@***.***>;
***@***.******@***.***>;
主题: Re: [vitejs/vite] How to use `.js` instead of `.jsx` (#3448)
yes problem resolved
thanks for help
—
You are receiving this because you commented.
Reply to this email directly, view it on GitHub, or unsubscribe.
|
Beta Was this translation helpful? Give feedback.
-
I have created a CLI app which can help updating the extension from This will make sure that file with React components are only changed and thus follows vite guidelines. |
Beta Was this translation helpful? Give feedback.
-
thx bro, u help me to kill the problem which bother me a long time, thx!
…------------------ 原始邮件 ------------------
发件人: "vitejs/vite" ***@***.***>;
发送时间: 2021年6月1日(星期二) 中午11:07
***@***.***>;
***@***.******@***.***>;
主题: Re: [vitejs/vite] How to use `.js` instead of `.jsx` (#3448)
not sure am i correct
i can see load in to the page after i modified the regex
filter: /src\\.*\.js$/
import { defineConfig } from 'vite' import reactRefresh from ***@***.***/plugin-react-refresh' import {resolve} from 'path' import fs from 'fs/promises'; // https://vitejs.dev/config/ export default defineConfig({ server:{ port: 4200 }, resolve:{ alias:{ 'src': resolve(__dirname,'src'), } }, esbuild: { loader: "jsx", include: /src\/.*\.jsx?$/, // loader: "tsx", // include: /src\/.*\.[tj]sx?$/, exclude: [], }, optimizeDeps: { esbuildOptions: { plugins: [ { name: "load-js-files-as-jsx", setup(build) { build.onLoad({ filter: /src\\.*\.js$/ }, async (args) => ({ // i modified the regex here loader: "jsx", contents: await fs.readFile(args.path, "utf8"), })); }, }, ], }, }, plugins: [reactRefresh()] })
—
You are receiving this because you commented.
Reply to this email directly, view it on GitHub, or unsubscribe.
|
Beta Was this translation helpful? Give feedback.
-
You can use python. Here is the script that I've used to convert all JS to JSX. import os
import sys
rootdir = 'src'
for subdir, dirs, files in os.walk(rootdir):
for file in files:
address = os.path.join(subdir,file)
split = os.path.splitext(address)
if split[1]=='.js':
os.rename(address,split[0]+'.jsx') |
Beta Was this translation helpful? Give feedback.
-
For anyone looking for a script to rename files containing jsx to
|
Beta Was this translation helpful? Give feedback.
-
One liner to rename file in src in terminal:
|
Beta Was this translation helpful? Give feedback.
-
Another one liner with the regex of @dons20 to make sure only JSX files get renamed: find src -type f | grep "\.[jt]s$" | xargs -n1 grep -HE "^[^*\n]*(<\/?[a-zA-Z]*>)[\s\w]*$" | cut -d: -f1 | uniq | awk '{print "mv "$1" "$1"x"}' | sh |
Beta Was this translation helpful? Give feedback.
-
Building on @maximilize answer, I'd recommend tweaking the regex to the following to catch some more edge cases:
|
Beta Was this translation helpful? Give feedback.
-
a lot of answers here are using plugins with this is how I managed to actually get this working with a very simple plugin, just injecting the file into esbuild directly: {
// ...
plugins: [
{
name: 'load-js-files-as-jsx',
async load(id) {
if (!id.match(/src\/.*\.js$/)) {
return
}
const file = await fs.readFile(id, 'utf-8')
return esbuild.transformSync(file, { loader: 'jsx' })
}
}
]
// ...
} |
Beta Was this translation helpful? Give feedback.
-
Enabled Intellisense via adding |
Beta Was this translation helpful? Give feedback.
-
try this cli,
See here for details,github |
Beta Was this translation helpful? Give feedback.
-
I have a dependency in a project that uses JSX inside of *.js files. The project itself is written in TypeScript. optimizeDeps: {
esbuildOptions: {
loader: {
'.js': 'jsx',
},
},
}, in |
Beta Was this translation helpful? Give feedback.
-
The answer from @rogeriochaves show's part of the solution. You need to add I got this to work with // top of file `vite.config.ts`
import * as esbuild from "esbuild";
// in defineConfig:
export default defineConfig({
// ...
build: {
manifest: true,
target: "es2018",
outDir: "...",
emptyOutDir: true,
sourcemap: true,
rollupOptions: {
/// ...
plugins: [
{
name: "load-js-files-as-jsx",
async load(id) {
// fix for modules using JSX in .js files (add to regex here for any modules that throw errors)
if (
!id.match(/svgs\/index\.js/) &&
!id.match(/react-native-reanimated\/lib\/.*\.js/)
) {
return;
}
const file = await fs.readFile(id, "utf-8");
return esbuild.transformSync(file, { loader: "jsx" });
},
},
],
},
commonjsOptions: {
transformMixedEsModules: true,
},
}, |
Beta Was this translation helpful? Give feedback.
-
Thankyou @tscritch, this works! I've adapted your solution a little so that it's easier to track which configuration had been added to vite to handle this (in the hope that one day it may be removed :) ). It also adds some configuration I found necessary for // vite/jsx-in-js.js
import esbuild from "esbuild";
import fs from "fs";
// Vite doesn't support JSX in .js files by default so we need to use esbuild to transform them
const rollupPlugin = matchers => ({
name: "js-in-jsx",
async load(id) {
if (matchers.some(matcher => matcher.test(id))) {
const file = await fs.readFileSync(id, { encoding: "utf-8" });
return esbuild.transformSync(file, { loader: "jsx" });
}
}
});
export default {
build: {
rollupOptions: {
plugins: matchers => [rollupPlugin(matchers)]
},
commonjsOptions: {
transformMixedEsModules: true
}
},
optimizeDeps: {
esbuildOptions: {
loader: {
".js": "jsx"
}
}
} import jsxInJs from "./vite/jsx-in-js";
export default defineConfig({
...
build: {
outDir: "../dist",
rollupOptions: {
plugins: jsxInJs.build.rollupOptions.plugins([/.*files\/containing\/jsx-code.*js/])
},
commonjsOptions: {
transformMixedEsModules: jsxInJs.build.commonjsOptions.transformMixedEsModules
}
},
optimizeDeps: {
esbuildOptions: {
loader: jsxInJs.optimizeDeps.esbuildOptions.loader
}
}
} |
Beta Was this translation helpful? Give feedback.
-
Expanding on earlier work in this thread, here is the
|
Beta Was this translation helpful? Give feedback.
-
FYI here's the config I'm now using: https://stackblitz.com/edit/vitejs-vite-ka3qkc?file=vite.config.js The key steps (after a lot of trial an error, and following guidance in this thread): 1. We do not need a esbuild plugin. We just need to tell esbuild to use the jsx loader
Note that this is the 2. Add a Vite plugin (as one of the posts above suggests) - but use the
If you don't set This probably doesn't happen in TypeScript projects where Vite or Esbuild must know somehow to use the jsx option from the tsconfig.json. Notice that I'm using the Grateful for all the responses above, but I'm still not certain what the most correct option is. |
Beta Was this translation helpful? Give feedback.
-
Heres the solution please create config file for vite import { defineConfig, transformWithEsbuild } from 'vite' export default defineConfig({
], optimizeDeps: { |
Beta Was this translation helpful? Give feedback.
@marialovesbeans here's a working config:
I would only recommend using this to try out Vite in your project - the plugin ci…