Skip to content
This repository was archived by the owner on Apr 19, 2024. It is now read-only.

Commit c684701

Browse files
authored
Merge pull request #1 from rodolfo2488/master
Added hot-loader to react starter kit and production config
2 parents 3d7126d + 2159779 commit c684701

File tree

9 files changed

+194
-63
lines changed

9 files changed

+194
-63
lines changed

index.html

Lines changed: 0 additions & 17 deletions
This file was deleted.

package.json

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55
"main": "index.js",
66
"scripts": {
77
"test": "jest",
8-
"serve": "node_modules/.bin/webpack-dev-server"
8+
"dev": "cross-env NODE_ENV=development webpack-dev-server --hot --color",
9+
"prod": "cross-env NODE_ENV=production webpack"
910
},
1011
"keywords": [
1112
"typescript",
@@ -19,16 +20,23 @@
1920
"@types/enzyme": "^2.7.7",
2021
"@types/jasmine": "^2.5.47",
2122
"awesome-typescript-loader": "^3.1.2",
23+
"cross-env": "4.0.0",
2224
"enzyme": "^2.8.0",
25+
"extract-text-webpack-plugin": "2.1.0",
26+
"html-webpack-plugin": "2.28.0",
2327
"jest": "^19.0.2",
2428
"karma": "^1.6.0",
2529
"karma-chrome-launcher": "^2.0.0",
2630
"karma-typescript": "^3.0.0",
2731
"karma-typescript-preprocessor": "^0.3.1",
2832
"react-addons-test-utils": "^15.5.1",
33+
"react-hot-loader": "1.3.1",
2934
"source-map-loader": "^0.2.1",
35+
"stylelint-webpack-plugin": "0.7.0",
3036
"typescript": "^2.2.2",
31-
"webpack-dev-server": "^2.4.2"
37+
"webpack-dev-server": "^2.4.2",
38+
"webpack-merge": "4.1.0",
39+
"write-file-webpack-plugin": "4.0.2"
3240
},
3341
"dependencies": {
3442
"@types/react": "^15.0.21",

src/index.html

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<meta charset="UTF-8" />
5+
<title>Hello React!</title>
6+
</head>
7+
<body>
8+
<div id="hello"></div>
9+
</body>
10+
</html>

src/main/hello.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,6 @@ export interface HelloProps {}
44

55
export class Hello extends React.Component<HelloProps, undefined> {
66
render() {
7-
return <h1>Hello typescript and react!</h1>;
7+
return <h1>Hello typescript and react!, hot-load mode on!</h1>;
88
}
99
}

tsconfig.json

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,21 @@
11
{
2-
"compilerOptions": {
3-
"outDir": "./dist/",
4-
"sourceMap": true,
5-
"noImplicitAny": true,
6-
"module": "commonjs",
7-
"target": "es6",
8-
"jsx": "react"
9-
},
10-
"include": [
11-
"./src/**/*"
12-
],
13-
"files": [
14-
"src/**/*"
2+
"compilerOptions": {
3+
"baseUrl": ".",
4+
"sourceMap": true,
5+
"noImplicitAny": true,
6+
"module": "commonjs",
7+
"target": "es5",
8+
"noEmit": true,
9+
"jsx": "react",
10+
"strictNullChecks": true,
11+
"lib": [
12+
"dom",
13+
"es2015",
14+
"es5",
15+
"es6"
1516
]
17+
},
18+
"include": [
19+
"src/**/*"
20+
]
1621
}

webpack.config.js

Lines changed: 11 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,15 @@
1-
module.exports = {
2-
entry: "./src/index.tsx",
3-
output: {
4-
filename: "bundle.js",
5-
path: __dirname + "/dist"
6-
},
1+
const webpack = require("webpack");
2+
const prod = require('./webpack/webpack.config.prod');
3+
const dev = require('./webpack/webpack.config.dev');
74

8-
// Enable sourcemaps for debugging webpack's output.
9-
devtool: "source-map",
5+
const isProduction = process.env.NODE_ENV === "production";
106

11-
resolve: {
12-
// Add '.ts' and '.tsx' as resolvable extensions.
13-
extensions: [".ts", ".tsx", ".js", ".json"]
14-
},
7+
let config;
158

16-
module: {
17-
rules: [
18-
// All files with a '.ts' or '.tsx' extension will be handled by 'awesome-typescript-loader'.
19-
{ test: /\.tsx?$/, loader: "awesome-typescript-loader" },
9+
if (isProduction) {
10+
config = prod;
11+
} else {
12+
config = dev;
13+
}
2014

21-
// All output '.js' files will have any sourcemaps re-processed by 'source-map-loader'.
22-
{ enforce: "pre", test: /\.js$/, loader: "source-map-loader" }
23-
]
24-
},
25-
26-
// When importing a module whose path matches one of the following, just
27-
// assume a corresponding global variable exists and use that instead.
28-
// This is important because it allows us to avoid bundling all of our
29-
// dependencies, which allows browsers to cache those libraries between builds.
30-
externals: {
31-
"react": "React",
32-
"react-dom": "ReactDOM"
33-
},
34-
};
15+
module.exports = config;

webpack/webpack.config.base.js

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
const path = require("path");
2+
const HtmlWebpackPlugin = require("html-webpack-plugin");
3+
4+
const config = {
5+
entry: ["./src/index"],
6+
output: {
7+
path: path.resolve("./dist"),
8+
filename: "[name].js",
9+
sourceMapFilename: "[file].map",
10+
publicPath: "/dist/"
11+
},
12+
devtool: "source-map",
13+
resolve: {
14+
extensions: ['.ts', '.tsx', '.js', '.jsx']
15+
},
16+
module: {
17+
rules: [
18+
{
19+
enforce: 'pre',
20+
test: /\.jsx?$/,
21+
use: ['source-map-loader']
22+
}, {
23+
enforce: 'pre',
24+
test: /\.tsx?$/,
25+
use: ['source-map-loader']
26+
}, {
27+
test: /\.s?css$/,
28+
use: [
29+
{
30+
loader: "style-loader"
31+
}, {
32+
loader: "css-loader"
33+
}, {
34+
loader: "postcss-loader",
35+
options: {
36+
plugins: function () {
37+
return [
38+
require('postcss-smart-import')({/* ...options */}),
39+
require('autoprefixer')({/* ...options */}),
40+
require('precss')({/* ...options */}),
41+
require('doiuse')({browsers: ['ie >= 9', '> 1%'],}),
42+
require('colorguard')({/* ...options */}),
43+
require('postcss-unique-selectors')({/* ...options */}),
44+
require("postcss-reporter")({clearMessages: true})
45+
];
46+
}
47+
}
48+
}, {
49+
loader: "sass-loader"
50+
}
51+
]
52+
}
53+
]
54+
},
55+
plugins: [
56+
new HtmlWebpackPlugin({
57+
filename: "index.html",
58+
template: "./src/index.html",
59+
minify: {
60+
removeComments: true
61+
}
62+
})
63+
]
64+
};
65+
66+
module.exports = config;

webpack/webpack.config.dev.js

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
const webpack = require('webpack');
2+
const path = require("path");
3+
const merge = require('webpack-merge');
4+
const WriteFilePlugin = require("write-file-webpack-plugin");
5+
const base = require("./webpack.config.base");
6+
7+
const hotPort = 8080;
8+
9+
const config = {
10+
output: {
11+
publicPath: "http://localhost:" + hotPort + "/dist/"
12+
},
13+
module: {
14+
rules: [
15+
{
16+
test: /\.tsx?$/,
17+
exclude: /(node_modules|__tests__)/,
18+
use: ['react-hot-loader', 'awesome-typescript-loader']
19+
},
20+
{
21+
test: /\.(png|jpg|woff|woff2|eot|ttf|svg|gif)(\?.+)?(#.+)?$/,
22+
use: ["url-loader?limit=100000"]
23+
}
24+
]
25+
},
26+
devServer: {
27+
publicPath: "http://localhost:" + hotPort + "/dist/",
28+
contentBase: path.resolve("./dist"),
29+
port: hotPort,
30+
quiet: false,
31+
noInfo: false,
32+
historyApiFallback: true,
33+
hot: true,
34+
stats: {
35+
colors: true
36+
}
37+
},
38+
plugins: [
39+
new WriteFilePlugin({test: /\.html/})
40+
]
41+
};
42+
43+
module.exports = merge(base, config);

webpack/webpack.config.prod.js

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
const webpack = require('webpack');
2+
const path = require("path");
3+
const merge = require('webpack-merge');
4+
const ExtractTextPlugin = require("extract-text-webpack-plugin");
5+
const base = require("./webpack.config.base");
6+
7+
const config = {
8+
module: {
9+
rules: [
10+
{
11+
test: /\.tsx?$/,
12+
exclude: /(node_modules|__tests__)/,
13+
use: ['awesome-typescript-loader']
14+
}
15+
]
16+
},
17+
output: {
18+
filename: "[name].[chunkhash].js"
19+
},
20+
plugins: [
21+
new ExtractTextPlugin({
22+
filename: "[name].[contenthash].css",
23+
allChunks: true,
24+
disable: false
25+
}),
26+
new webpack.DefinePlugin({
27+
"process.env": {
28+
"NODE_ENV": JSON.stringify("production")
29+
}
30+
}),
31+
new webpack.optimize.UglifyJsPlugin()
32+
]
33+
};
34+
35+
module.exports = merge(base, config);

0 commit comments

Comments
 (0)