Skip to content

Commit c441495

Browse files
author
developerworks
committed
Fix hot loader for typescript
1 parent 414a22e commit c441495

9 files changed

+887
-204
lines changed
File renamed without changes.

package.json

+6-1
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,13 @@
44
"main": "index.js",
55
"license": "MIT",
66
"scripts": {
7-
"watch": "nodemon --watch webpack.config.js --exec \"webpack-dev-server\"",
7+
"watch": "webpack-dev-server",
88
"watch_dll": "nodemon --watch webpack.config.dll.js --exec \"webpack --progress --config webpack.config.dll.js --watch\"",
99
"build_dll": "rm -rf dist && webpack --config webpack.config.dll.js --progress"
1010
},
1111
"dependencies": {
12+
"@types/react-router-dom": "^4.0.4",
13+
"antd": "^2.10.4",
1214
"react": "^15.5.4",
1315
"react-dom": "^15.5.4",
1416
"react-router-dom": "^4.1.1"
@@ -19,6 +21,9 @@
1921
"@types/webpack-env": "^1.13.0",
2022
"add-asset-html-webpack-plugin": "^2.0.1",
2123
"awesome-typescript-loader": "^3.1.3",
24+
"babel-core": "^6.24.1",
25+
"babel-loader": "^7.0.0",
26+
"babel-polyfill": "^6.23.0",
2227
"file-loader": "^0.11.1",
2328
"html-webpack-plugin": "^2.28.0",
2429
"react-hot-loader": "next",

src/App.tsx

+3-6
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,22 @@
11
import * as React from 'react'
2-
import * as ReactDOM from 'react-dom'
32

4-
5-
export interface HelloProps {
3+
interface HelloProps {
64
compiler: string;
75
framework: string;
86
}
97

108

11-
class App extends React.Component<HelloProps, any> {
9+
export default class App extends React.Component<HelloProps, any> {
1210
constructor(props: HelloProps){
1311
super(props);
1412
}
1513
render() {
1614
return (
1715
<div>
18-
<h2>Hello {this.props.compiler} and {this.props.framework}!</h2>
16+
<h2>Hello <span style={{color: "blue"}}>{this.props.compiler}</span> and {this.props.framework}!!</h2>
1917
<p>You can edit the <code>./src/App.tsx</code> to feel react hot load with typescript.</p>
2018
</div>
2119
)
2220
}
2321
}
2422

25-
export default App

src/index.html

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,6 @@
55
<title><%= htmlWebpackPlugin.options.title %></title>
66
</head>
77
<body>
8-
<div id="example"></div>
8+
<div id="app"></div>
99
</body>
1010
</html>

src/index.tsx

+18-16
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,28 @@
1-
import * as React from "react";
2-
import * as ReactDOM from "react-dom";
3-
import { AppContainer } from "react-hot-loader";
1+
import * as React from "react"
2+
import * as ReactDOM from "react-dom"
3+
import { AppContainer } from "react-hot-loader"
4+
import { BrowserRouter, Route, Link } from "react-router-dom"
5+
import { message, notification, Icon } from 'antd'
6+
47
import App from "./App";
58

6-
const rootElement = document.getElementById("example");
9+
const rootEl = document.getElementById("app");
710

811
ReactDOM.render(
912
<AppContainer>
10-
<App compiler="TypeScript" framework="React"/>
13+
<App compiler="TypeScript" framework="React" />
1114
</AppContainer>,
12-
document.getElementById('example')
13-
);
15+
rootEl
16+
)
1417

1518
if (module.hot) {
16-
1719
module.hot.accept("./App", () => {
18-
const NextApp = require<RequireImport>("./App").default;
20+
const NextApp = require<RequireImport>("./App").default
1921
ReactDOM.render(
2022
<AppContainer>
21-
<NextApp compiler="TypeScript" framework="React"/>
23+
<NextApp compiler="TypeScript" framework="React" />
2224
</AppContainer>,
23-
document.getElementById('example')
25+
rootEl
2426
)
2527
})
2628
}
@@ -31,16 +33,16 @@ if (module.hot) {
3133
// <Component compiler="TypeScript" framework="React" />,
3234
// </AppContainer>,
3335
// document.getElementById("example")
34-
// );
36+
// )
3537
// }
3638

37-
// type AppComponent = new(...args: any[]) => React.Component<HelloProps, any>;
39+
// type AppComponent = new(...args: any[]) => React.Component<HelloProps, any>
3840

3941
// const render = (component: AppComponent) => {
4042
// const AppInit = React.createElement(component, {
4143
// compiler: "TypeScript",
4244
// framework: "React"
43-
// });
45+
// })
4446
// ReactDOM.render(
4547
// <AppContainer>
4648
// <AppInit />
@@ -49,9 +51,9 @@ if (module.hot) {
4951
// )
5052
// }
5153

52-
// render(App);
54+
// render(App)
5355
// if (module.hot) {
54-
// module.hot.accept('./App', () => { render(App) });
56+
// module.hot.accept("./App", () => { render(App) })
5557
// }
5658

5759

tsconfig.json

+6-3
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,21 @@
11
{
22
"compilerOptions": {
3+
"moduleResolution": "node",
4+
"allowSyntheticDefaultImports": true,
35
"outDir": "./dist/",
46
"sourceMap": true,
57
"noImplicitAny": true,
68
"module": "commonjs",
79
"target": "es5",
8-
"jsx": "react"
10+
"jsx": "react",
11+
"skipLibCheck": true
912
},
1013
"files": [
11-
"./src/declarations.d.ts",
14+
"./declarations.d.ts",
1215
"./node_modules/@types/webpack-env/index.d.ts",
1316
"./src/index.tsx"
1417
],
1518
"exclude": [
1619
"node_modules"
1720
]
18-
}
21+
}

webpack.config.dll.js

+2
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ module.exports = {
2424
test: /\.tsx?$/,
2525
exclude: /node_modules/,
2626
use: [{
27+
loader: 'react-hot-loader/webpack'
28+
}, {
2729
loader: 'awesome-typescript-loader'
2830
}]
2931
},

webpack.config.js

+7-3
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,12 @@ const open_write_file = false;
77

88
const config = {
99
entry: {
10+
vendors: [
11+
'react-hot-loader/patch'
12+
],
1013
app: [
1114
'react-hot-loader/patch',
15+
'webpack/hot/only-dev-server',
1216
'./src/index.tsx'
1317
]
1418
},
@@ -21,7 +25,7 @@ const config = {
2125
},
2226
devtool: 'cheap-module-source-map',
2327
resolve: {
24-
extensions: ['.ts', '.tsx', '.js', '.jsx', '.json']
28+
extensions: ['.tsx', '.ts', '.jsx', '.js', '.json']
2529
},
2630
module: {
2731
rules: [
@@ -31,7 +35,7 @@ const config = {
3135
include: path.resolve(__dirname, "src"),
3236
use: [{
3337
loader: 'react-hot-loader/webpack'
34-
},{
38+
}, {
3539
loader: 'awesome-typescript-loader'
3640
}]
3741
},
@@ -65,7 +69,7 @@ const config = {
6569
manifest: require('./dist/vendor-manifest.dll.json')
6670
}),
6771
new AddAssetHtmlPlugin([
68-
{filepath: require.resolve('./dist/vendor.55026a8bdf762f1533ba.dll.js')},
72+
{filepath: require.resolve('./dist/vendor.c455ba929e1f42ea8c79.dll.js')},
6973
]),
7074
],
7175
devServer: {

0 commit comments

Comments
 (0)