admin管理员组文章数量:1322838
It's a mon thing to create a index.js file in an React application with the only purpose to export several modules, in order to avoid having too many import statements on other ponents. This can be done by:
index.js
export { Credits } from './Credits.js';
export { SocialMedia } from './SocialMedia.js';
any module that might use those exports:
import * as var_name from index.js
And this is very nice. It wraps exports into a single file. However, when I changed my project to React with typescript, I found that .tsx
files cannot be exported like that. The image below is the error I got after changing the project to typescript and the extensions became .tsx
Is there a way of 'bundle' export React .tsx
files with the structure shown above? If not, what is the simplest way of centralizing .tsx
files export?
My webpack.config.js:
module.exports = {
module: {
rules: [{
test: /\.scss$/,
use: ["sass-loader"]
}]
}
};
It's a mon thing to create a index.js file in an React application with the only purpose to export several modules, in order to avoid having too many import statements on other ponents. This can be done by:
index.js
export { Credits } from './Credits.js';
export { SocialMedia } from './SocialMedia.js';
any module that might use those exports:
import * as var_name from index.js
And this is very nice. It wraps exports into a single file. However, when I changed my project to React with typescript, I found that .tsx
files cannot be exported like that. The image below is the error I got after changing the project to typescript and the extensions became .tsx
Is there a way of 'bundle' export React .tsx
files with the structure shown above? If not, what is the simplest way of centralizing .tsx
files export?
My webpack.config.js:
module.exports = {
module: {
rules: [{
test: /\.scss$/,
use: ["sass-loader"]
}]
}
};
Share
Improve this question
edited Jul 31, 2021 at 19:53
Pelicer
asked Jul 31, 2021 at 19:47
PelicerPelicer
1,5846 gold badges27 silver badges63 bronze badges
8
-
Are you sure that isn't possible? Perhaps your Webpack/... config is configured to use
index.tsx
(and notindex.ts
) as entry point? – Kelvin Schoofs Commented Jul 31, 2021 at 19:48 - The only configuration in my webpack is about sass-loader, to use .scss files as modules. I'll updated it in the question. I'm fairly new to typescript. Is it necessary to add configurations to webpack in order to do this? – Pelicer Commented Jul 31, 2021 at 19:50
-
1
That's a very small
webpack.config.js
. Did you omit anything, or are you using a certain framework such asreact-scripts
? – Kelvin Schoofs Commented Jul 31, 2021 at 19:52 - Indeed I am. The project was wrapped with create-react-app --template typscript. – Pelicer Commented Jul 31, 2021 at 19:53
- 1 Glad to hear that worked. I'll submit it as an answer to the question in case anyone es here in the future and glosses over the ments section. – Tim Ernsberger Commented Jul 31, 2021 at 22:26
1 Answer
Reset to default 6You can definitely use the same style of having an index file to group up exports for a whole folder. The simplest way around your problem would be to omit the file extension (assuming you only have one "index" file in the folder).
For example, let's say you have a ponent in 'mon/Example.tsx':
import React from 'react'
export const Example = () => (<div>I'm an example ponent</div>)
You can then export it in an index file 'mon/index.tsx':
export { Example } from './Example'
And import it from somewhere else, e.g. 'App.tsx':
import { Example } from './mon'
本文标签: javascriptUse index file to export tsx modulesStack Overflow
版权声明:本文标题:javascript - Use index file to export .tsx modules - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742116497a2421491.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论