Answer the question
In order to leave comments, you need to log in
How to set up Type Script with React (.tsx) using babel 7 via settings in webpack 4 config?
How to set up Type Script with React (.tsx) using babel 7 via settings in webpack 4 config?
I use the following babel settings in webpack config:
module.exports = () => ({
module: {
rules: [
{
test: /\.j|tsx?$/,
loader: 'babel-loader',
exclude: /\/node_modules\//,
options: {
presets: [
'@babel/preset-env',
'@babel/preset-react',
["@babel/preset-typescript", {
"isTSX": true,
"allExtensions": true
}]
],
plugins: [
'@babel/plugin-proposal-class-properties'
],
}
}
]
}
});
import React, { PureComponent } from 'react';
import './test.pcss';
interface Props {
name: boolean
}
export default class Test extends PureComponent<Props> {
render() {
const { name } = this.props;
return (
<div className="test">
<span>{name}</span>
</div>
)
}
}
import ReactDOM from 'react-dom';
import React from 'react';
import Test from './tmp';
document.addEventListener('DOMContentLoaded', ()=> {
ReactDOM.render(
<Test name={12312312}/>,
test
);
});
Answer the question
In order to leave comments, you need to log in
I'm no expert, but ts doesn't seem to need babel.
For ts, "ts-loader" or "awesome-typescript-loader" is used. There seems to be a short webpack config for React + ts in the doc.
I made my starting config for projects a couple of months ago, it's very simple and easy to scale, if you want you can use it https://github.com/Riveran/react-redux-ts-scss-boi...
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question