Skip to content

Commit 0b255cb

Browse files
committed
Use webpack to compile React written in TypeScript and launch BrowserSync
0 parents  commit 0b255cb

File tree

7 files changed

+4406
-0
lines changed

7 files changed

+4406
-0
lines changed

.gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
node_modules
2+
dist
3+
!dist/index.html

package.json

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
{
2+
"scripts": {
3+
"dev": "webpack-dev-server --progress --colors",
4+
"start": "webpack && npm run dev"
5+
},
6+
"dependencies": {
7+
"@types/react": "^16.4.14",
8+
"@types/react-dom": "^16.0.7",
9+
"react": "^16.5.2",
10+
"react-dom": "^16.5.2"
11+
},
12+
"devDependencies": {
13+
"awesome-typescript-loader": "^5.2.1",
14+
"browser-sync": "^2.24.7",
15+
"browser-sync-webpack-plugin": "^2.2.2",
16+
"source-map-loader": "^0.2.4",
17+
"typescript": "^3.0.3",
18+
"webpack": "^4.19.1",
19+
"webpack-cli": "^3.1.1",
20+
"webpack-dev-server": "^3.1.8"
21+
}
22+
}

src/components/Hello.tsx

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import * as React from "react";
2+
3+
export interface HelloProps { compiler: string; framework: string; }
4+
5+
export const Hello = (props: HelloProps) => <h1>Hello from {props.compiler} and {props.framework}!</h1>;

src/index.tsx

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import * as React from "react";
2+
import * as ReactDOM from "react-dom";
3+
4+
import { Hello } from "./components/Hello";
5+
6+
ReactDOM.render(
7+
<Hello compiler="TypeScript" framework="React" />,
8+
document.getElementById("example")
9+
);

tsconfig.json

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

webpack.config.js

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
const path = require('path');
2+
3+
const BrowserSyncPlugin = require('browser-sync-webpack-plugin')
4+
5+
6+
module.exports = {
7+
entry: './src/index.tsx',
8+
output: {
9+
filename: 'bundle.js',
10+
path: path.resolve(__dirname, 'dist')
11+
},
12+
// Enable sourcemaps for debugging webpack's output.
13+
devtool: "source-map",
14+
15+
resolve: {
16+
// Add '.ts' and '.tsx' as resolvable extensions.
17+
extensions: [".ts", ".tsx", ".js", ".json"]
18+
},
19+
20+
21+
module: {
22+
rules: [
23+
// All files with a '.ts' or '.tsx' extension will be handled by 'awesome-typescript-loader'.
24+
{ test: /\.tsx?$/, loader: "awesome-typescript-loader" },
25+
26+
// All output '.js' files will have any sourcemaps re-processed by 'source-map-loader'.
27+
{ enforce: "pre", test: /\.js$/, loader: "source-map-loader" }
28+
]
29+
},
30+
31+
devServer: {
32+
contentBase: path.join(__dirname, 'dist'),
33+
},
34+
// When importing a module whose path matches one of the following, just
35+
// assume a corresponding global variable exists and use that instead.
36+
// This is important because it allows us to avoid bundling all of our
37+
// dependencies, which allows browsers to cache those libraries between builds.
38+
externals: {
39+
// "react": "React",
40+
// "react-dom": "ReactDOM"
41+
},
42+
43+
plugins: [
44+
new BrowserSyncPlugin({
45+
host: 'localhost',
46+
port: 3000,
47+
proxy: 'http://localhost:8080/',
48+
files: ['./dist/**/*'],
49+
browser: 'google chrome'
50+
})
51+
]
52+
};

0 commit comments

Comments
 (0)