Skip to content

Commit 44d66fb

Browse files
authored
Merge pull request #2 from smotyl/feat/ts-loader
Feat: Add ts-loader
2 parents a354d71 + 87d998d commit 44d66fb

File tree

8 files changed

+222
-13
lines changed

8 files changed

+222
-13
lines changed

configs/webpack.config.js

+24-10
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,36 @@ const HtmlWebpackPlugin = require('html-webpack-plugin');
44
const currDir = process.cwd();
55

66
module.exports = (config) => {
7-
const { mode } = config;
7+
const { mode, typescript } = config;
8+
9+
let babelLoader = {
10+
test: /\.(ts|js)x?$/,
11+
loader: require.resolve('babel-loader'),
12+
exclude: /node_modules/,
13+
options: {
14+
presets: ["@babel/preset-env", "@babel/preset-react"],
15+
}
16+
};
17+
18+
let tsLoader = {
19+
test: /\.tsx?$/,
20+
use: 'ts-loader',
21+
exclude: /node_modules/,
22+
};
23+
24+
let entryFile = typescript ? 'index.tsx' : 'index.js'
825

926
return {
1027
mode: mode,
11-
entry: path.resolve(currDir, 'src', 'index.js'),
28+
entry: path.resolve(currDir, 'src', entryFile),
1229
output: {
1330
path: path.resolve(currDir, 'dist'),
1431
filename: 'bundle.js'
1532
},
33+
devtool: 'source-map',
34+
resolve: {
35+
extensions: [ '.tsx', '.ts', '.js', '.jsx' ],
36+
},
1637
devServer: {
1738
contentBase: path.resolve(currDir, 'src'),
1839
},
@@ -24,14 +45,7 @@ module.exports = (config) => {
2445
],
2546
module: {
2647
rules: [
27-
{
28-
test: /\.(ts|js)x?$/,
29-
loader: require.resolve('babel-loader'),
30-
exclude: /node_modules/,
31-
options: {
32-
presets: ["@babel/preset-env", "@babel/preset-react"],
33-
}
34-
},
48+
typescript ? tsLoader : babelLoader,
3549
]
3650
}
3751
}

package.json

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "basic-react-scripts",
3-
"version": "0.0.7",
3+
"version": "0.0.8",
44
"main": "index.js",
55
"author": "Sandro Motyl",
66
"license": "MIT",
@@ -20,11 +20,15 @@
2020
"@babel/core": "^7.13.14",
2121
"@babel/preset-env": "^7.13.12",
2222
"@babel/preset-react": "^7.13.13",
23+
"@types/react": "^17.0.3",
24+
"@types/react-dom": "^17.0.3",
2325
"babel-loader": "^8.2.2",
2426
"child_process": "^1.0.2",
2527
"choose-port": "^0.3.2",
2628
"html-webpack-plugin": "^5.3.1",
2729
"open": "^8.0.4",
30+
"ts-loader": "^8.1.0",
31+
"typescript": "^4.2.3",
2832
"webpack": "^5.28.0",
2933
"webpack-dev-server": "^3.11.2"
3034
},

scripts/start.js

+2
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,11 @@ process.env.HOST ||
1818
(process.platform === 'win32' ? 'localhost' : '127.0.0.1');
1919
const port = process.env.PORT || 8080;
2020
const levelLog = process.env.LEVEL_LOG || 'none';
21+
const typescript = process.argv.slice(2).includes('--ts');
2122

2223
const config = webpackConfig({
2324
mode: 'development',
25+
typescript: typescript,
2426
});
2527

2628
const start = () => {

src/App.tsx

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import * as React from 'react'
2+
3+
const App = () => {
4+
return <h1>Hello, World!</h1>;
5+
}
6+
7+
export default App;

src/index.html

-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99

1010
<body>
1111
<div id="app"></div>
12-
<script src="./bundle.js" async defer></script>
1312
</body>
1413

1514
</html>

src/index.tsx

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import * as React from 'react'
2+
import * as ReactDOM from 'react-dom'
3+
import App from './App'
4+
5+
ReactDOM.render(<App />, document.getElementById('app'));

tsconfig.json

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"compilerOptions": {
3+
"jsx": "react",
4+
"module": "commonjs",
5+
"outDir": "./dist/",
6+
"noImplicitAny": true,
7+
"removeComments": true,
8+
"preserveConstEnums": true,
9+
"sourceMap": true,
10+
"target": "es5"
11+
},
12+
"include": [
13+
"src/index.tsx"
14+
]
15+
}

0 commit comments

Comments
 (0)