Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 7b5e982

Browse files
committed
Better document the webpack files to match the work Grey is doing in v1
1 parent 095bcfc commit 7b5e982

File tree

3 files changed

+116
-43
lines changed

3 files changed

+116
-43
lines changed

site/webpack.common.ts

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/**
2+
* @fileoverview This file contains a set of webpack configurations that should
3+
* be shared between development and production.
4+
*/
5+
6+
7+
import * as path from "path"
8+
import { Configuration } from "webpack"
9+
import HtmlWebpackPlugin from "html-webpack-plugin"
10+
11+
const templatePath = path.join(__dirname, "html_templates")
12+
13+
const plugins = [
14+
// The HTML webpack plugin tells webpack to use our `index.html` and inject
15+
// the bundle script, which might have special naming.
16+
new HtmlWebpackPlugin({
17+
title: "Custom template",
18+
template: path.join(templatePath, "index.html"),
19+
inject: "body",
20+
})
21+
]
22+
23+
export const commonWebpackConfig: Configuration = {
24+
// entry defines each "page" or "chunk". Currently, for v2, we only have one bundle -
25+
// a bundle that is shared across all of the UI. However, we may need to eventually split
26+
// like in v1, where there is a separate entry piont for dashboard & terminal.
27+
entry: path.join(__dirname, "Main.tsx"),
28+
29+
// modules specify how different modules are loaded
30+
// See: https://webpack.js.org/concepts/modules/
31+
module: {
32+
rules: [
33+
{
34+
test: /\.tsx?$/,
35+
use: ["ts-loader"],
36+
exclude: [/node_modules/],
37+
},
38+
],
39+
},
40+
41+
resolve: {
42+
// Let webpack know to consider ts/tsx files for bundling
43+
// See: https://webpack.js.org/guides/typescript/
44+
extensions: [".tsx", ".ts", ".js"]
45+
},
46+
47+
// output defines the name and location of the final bundle
48+
output: {
49+
// The chunk name along with a hash of its content will be used for the
50+
// generated bundle name.
51+
//
52+
// REMARK: It's important to use [contenthash] here to invalidate caches.
53+
filename: "bundle.[contenthash].js",
54+
path: path.resolve(__dirname, "out"),
55+
},
56+
target: "web",
57+
}

site/webpack.dev.ts

Lines changed: 38 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,39 @@
11
import ReactRefreshWebpackPlugin from "@pmmmwh/react-refresh-webpack-plugin"
2-
import HtmlWebpackPlugin from "html-webpack-plugin"
3-
import * as webpack from "webpack"
2+
import { Configuration } from "webpack"
43
import "webpack-dev-server"
54

6-
import productionConfig from "./webpack.prod"
5+
import { commonWebpackConfig } from "./webpack.common"
76

8-
const config: webpack.Configuration = {
9-
...productionConfig,
7+
const commonPlugins = commonWebpackConfig.plugins || []
8+
9+
const config: Configuration = {
10+
...commonWebpackConfig,
11+
12+
// devtool controls how source maps are generated. In development, we want
13+
// more details (less optimized) for more readability and an easier time
14+
// debugging
15+
devtool: "eval-source-map",
16+
17+
// devServer is the configuration for webpack-dev-server.
18+
//
19+
// REMARK: needs webpack-dev-server import at top of file for typings
1020
devServer: {
1121
allowedHosts: "all",
22+
23+
// client configures options that are observed in the browser/web-client.
1224
client: {
25+
// automatically sets the browser console to verbose when using HMR
26+
logging: "verbose",
27+
28+
// errors will display as a full-screen overlay
1329
overlay: true,
14-
progress: false,
30+
31+
// build % progress will display in the browser
32+
progress: true,
33+
34+
// webpack-dev-server uses a webSocket to communicate with the browser
35+
// for HMR. By setting this to auto://0.0.0.0/ws, we allow the browser
36+
// to set the protocal, hostname and port automatically for us.
1537
webSocketURL: "auto://0.0.0.0:0/ws",
1638
},
1739
devMiddleware: {
@@ -20,22 +42,25 @@ const config: webpack.Configuration = {
2042
headers: {
2143
"Access-Control-Allow-Origin": "*",
2244
},
45+
46+
// historyApiFallback is required when using history (react-router) for
47+
// properly serving index.html on 404s.
2348
historyApiFallback: true,
2449
hot: true,
2550
proxy: {
2651
"/api": "http://localhost:3000",
2752
},
2853
static: ["./static"],
2954
},
55+
56+
// Development mode - see:
57+
// https://webpack.js.org/configuration/mode/#mode-development
3058
mode: "development",
3159
plugins: [
32-
new HtmlWebpackPlugin({
33-
title: "Custom template",
34-
// Load a custom template (lodash by default)
35-
template: "html_templates/index.html",
36-
inject: "body",
37-
hash: true,
38-
}),
60+
...commonPlugins,
61+
62+
// The ReactRefreshWebpackPlugin enables hot-module reloading:
63+
// https://github.com/pmmmwh/react-refresh-webpack-plugin
3964
new ReactRefreshWebpackPlugin({
4065
overlay: true,
4166
}),

site/webpack.prod.ts

Lines changed: 21 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,31 @@
1-
import * as path from "path"
21
import CopyWebpackPlugin from "copy-webpack-plugin"
3-
import HtmlWebpackPlugin from "html-webpack-plugin"
4-
import * as webpack from "webpack"
5-
import "webpack-dev-server"
2+
import { Configuration } from "webpack"
3+
import { commonWebpackConfig } from "./webpack.common"
64

7-
const config: webpack.Configuration = {
8-
entry: "./Main.tsx",
5+
const commonPlugins = commonWebpackConfig.plugins || []
6+
7+
export const config: Configuration = {
8+
...commonWebpackConfig,
99
mode: "production",
10-
module: {
11-
rules: [
12-
{
13-
test: /\.tsx?$/,
14-
use: ["ts-loader"],
15-
exclude: [/node_modules/],
16-
},
17-
],
10+
11+
// Don't produce sourcemaps in production, to minmize bundle size
12+
devtool: false,
13+
14+
output: {
15+
...commonWebpackConfig.output,
16+
17+
// regenerate the entire out/ directory when producing production builds
18+
clean: true,
1819
},
20+
1921
plugins: [
22+
...commonPlugins,
23+
// For production builds, we also need to copy all the static
24+
// files to the 'out' folder.
2025
new CopyWebpackPlugin({
2126
patterns: [{ from: "static", to: "." }],
22-
}),
23-
new HtmlWebpackPlugin({
24-
title: "Custom template",
25-
// Load a custom template (lodash by default)
26-
template: "html_templates/index.html",
27-
inject: "body",
28-
}),
29-
],
30-
resolve: {
31-
extensions: [".tsx", ".ts", ".js"],
32-
},
33-
output: {
34-
filename: "bundle.[contenthash].js",
35-
path: path.resolve(__dirname, "out"),
36-
},
37-
target: "web",
27+
})
28+
]
3829
}
3930

4031
export default config

0 commit comments

Comments
 (0)