Skip to content

Commit b10f819

Browse files
authored
docs: add TS with Babel example [skip ci] (#532)
1 parent cc18673 commit b10f819

File tree

16 files changed

+4185
-2
lines changed

16 files changed

+4185
-2
lines changed
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
module.exports = (api) => {
2+
// This caches the Babel config
3+
api.cache.using(() => process.env.NODE_ENV);
4+
return {
5+
presets: [
6+
'@babel/preset-env',
7+
'@babel/preset-typescript',
8+
// Enable development transform of React with new automatic runtime
9+
['@babel/preset-react', { development: !api.env('production'), runtime: 'automatic' }],
10+
],
11+
// Applies the react-refresh Babel plugin on non-production modes only
12+
...(!api.env('production') && { plugins: ['react-refresh/babel'] }),
13+
};
14+
};
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
{
2+
"name": "using-typescript-with-babel",
3+
"version": "0.1.0",
4+
"private": true,
5+
"dependencies": {
6+
"react": "^17.0.2",
7+
"react-dom": "^17.0.2"
8+
},
9+
"devDependencies": {
10+
"@babel/core": "^7.15.5",
11+
"@babel/preset-env": "^7.15.6",
12+
"@babel/preset-react": "^7.14.5",
13+
"@babel/preset-typescript": "^7.14.5",
14+
"@pmmmwh/react-refresh-webpack-plugin": "^0.5.0",
15+
"@types/react": "^17.0.20",
16+
"@types/react-dom": "^17.0.9",
17+
"babel-loader": "^8.2.2",
18+
"cross-env": "^7.0.3",
19+
"fork-ts-checker-webpack-plugin": "^6.3.3",
20+
"html-webpack-plugin": "^5.3.2",
21+
"react-refresh": "^0.10.0",
22+
"typescript": "4.4.3",
23+
"webpack": "^5.52.1",
24+
"webpack-cli": "^4.8.0",
25+
"webpack-dev-server": "^4.1.1"
26+
},
27+
"scripts": {
28+
"start": "webpack serve --hot",
29+
"build": "cross-env NODE_ENV=production webpack"
30+
}
31+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8" />
5+
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
6+
<title>TS React App</title>
7+
</head>
8+
<body>
9+
<div id="app"></div>
10+
</body>
11+
</html>
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import { lazy, Suspense } from 'react';
2+
import { ArrowFunction } from './ArrowFunction';
3+
import ClassDefault from './ClassDefault';
4+
import { ClassNamed } from './ClassNamed';
5+
import FunctionDefault from './FunctionDefault';
6+
import { FunctionNamed } from './FunctionNamed';
7+
8+
const LazyComponent = lazy(() => import('./LazyComponent'));
9+
10+
function App() {
11+
return (
12+
<div>
13+
<ClassDefault />
14+
<ClassNamed />
15+
<FunctionDefault />
16+
<FunctionNamed />
17+
<ArrowFunction />
18+
<Suspense fallback={<h1>Loading</h1>}>
19+
<LazyComponent />
20+
</Suspense>
21+
</div>
22+
);
23+
}
24+
25+
export default App;
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export const ArrowFunction = () => <h1>Arrow Function</h1>;
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { Component } from 'react';
2+
3+
class ClassDefault extends Component {
4+
render() {
5+
return <h1>Default Export Class</h1>;
6+
}
7+
}
8+
9+
export default ClassDefault;
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { Component } from 'react';
2+
3+
export class ClassNamed extends Component {
4+
render() {
5+
return <h1>Named Export Class</h1>;
6+
}
7+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
function FunctionDefault() {
2+
return <h1>Default Export Function</h1>;
3+
}
4+
5+
export default FunctionDefault;
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export function FunctionNamed() {
2+
return <h1>Named Export Function</h1>;
3+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
function LazyComponent() {
2+
return <h1>Lazy Component</h1>;
3+
}
4+
5+
export default LazyComponent;

0 commit comments

Comments
 (0)