[英]Typescript [React] not converting .tsx imports to .jsx imports
I keep typescript react components in different files, so I use the following statement to import my sub components. 我将打字稿反应组件保存在不同的文件中,因此我使用以下语句导入子组件。
//app.tsx //app.tsx
import {SideBar} from "./sidebar"
But when typescript compiles app.tsx to app.jsx, the import statement switches to require("sidebar"). 但是,当打字稿将app.tsx编译为app.jsx时,导入语句将切换为require(“ sidebar”)。 I get an error on the javascript as the require statement needs to have "sidebar.jsx" with ".jsx" extension.
由于require语句需要扩展名为“ .jsx”的“ sidebar.jsx”,因此我在javascript上收到错误消息。 Is there a way I could automate that?
有没有办法可以使它自动化?
My tsconfig setting is : 我的tsconfig设置是:
{ "compilerOptions": { "jsx": "preserve", "target": "ES5", "noImplicitAny":false, "module": "commonjs", } }
Maybe I need to add something to the tsconfig file? 也许我需要在tsconfig文件中添加一些内容?
Thanks in advance. 提前致谢。
I get an error on the javascript as the require statement needs to have "sidebar.jsx" with ".jsx" extension.
由于require语句需要扩展名为“ .jsx”的“ sidebar.jsx”,因此我在javascript上收到错误消息。
Fix this error eg if you are using webpack you can add .jsx
as a resolvable path. 修复此错误,例如,如果您使用的是Webpack,则可以将
.jsx
添加为可解析的路径。
resolve: {
// Add `.jsx` as a resolvable extension.
extensions: ['', '.webpack.js', '.web.js', '.jsx']
},
If so maybe even use ts-loader : https://github.com/TypeStrong/ts-loader and not have jsx: preserve
and instead jsx: react
. 如果是这样,甚至可以使用ts-loader: https : //github.com/TypeStrong/ts-loader ,而不需要
jsx: preserve
,而不是jsx: react
。 More : https://basarat.gitbooks.io/typescript/content/docs/jsx/tsx.html 更多: https : //basarat.gitbooks.io/typescript/content/docs/jsx/tsx.html
Use module: es6
instead of "module": "commonjs",
and typescript will not rewrite import
to require
. 使用
module: es6
代替"module": "commonjs",
并且打字稿不会将import
重写为require
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.