-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebpack.config.ts
113 lines (105 loc) · 2.56 KB
/
webpack.config.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
import { CleanWebpackPlugin } from "clean-webpack-plugin";
import del from "del";
import HtmlWebpackPlugin from "html-webpack-plugin";
import path from "path";
import webpack, { Stats } from "webpack";
import nodeExternals from "webpack-node-externals";
if (
!["development", "test", "production"].some(
env => process.env.NODE_ENV === env
)
) {
throw new Error(
"Environment must be set in the npm script eg. ENV=development, ENV=test, ENV=production, got:"
);
}
const isDev = process.env.NODE_ENV === "development";
export const clientConfig: webpack.Configuration = {
entry: "./client/index.tsx",
output: {
filename: "client.bundle.js",
path: path.resolve(__dirname, "dist", "public"),
publicPath: "/"
},
mode: isDev ? "development" : "production",
target: "web",
devtool: "inline-source-map",
optimization: {
// We no not want to minimize our code.
minimize: false
},
module: {
rules: [
{
test: /\.tsx?$/,
use: "ts-loader",
exclude: /node_modules/
},
{
use: ["style-loader", "css-loader"],
test: /\.css$/
}
]
},
resolve: {
extensions: [".ts", ".tsx", ".js", ".jsx"]
},
plugins: [
new HtmlWebpackPlugin({
template: "client/index.html"
}),
new CleanWebpackPlugin()
]
};
export const serverConfig: webpack.Configuration = {
entry: "./index.ts",
output: {
filename: "server.bundle.js",
path: path.resolve(__dirname, "dist"),
publicPath: "/"
},
mode: isDev ? "development" : "production",
target: "node",
devtool: "inline-source-map",
optimization: {
// We no not want to minimize our code.
minimize: false
},
node: {
// Need this when working with express, otherwise the build fails
__dirname: false, // if you don't put this is, __dirname
__filename: false // and __filename return blank or /
},
externals: [nodeExternals()],
module: {
rules: [
{
test: /\.tsx?$/,
use: "ts-loader",
exclude: /node_modules/
}
]
},
resolve: {
extensions: [".ts", ".tsx", ".js", ".jsx"]
}
};
if (process.env.NODE_ENV === "production") {
del.sync(path.join("dist", "**", "*"));
}
webpack([serverConfig, clientConfig], (err: Error, stats: Stats) => {
if (err) {
console.error(err.stack || err);
if ((err as any).details) {
console.error((err as any).details);
}
return;
}
const info = stats.toJson();
if (stats.hasErrors()) {
console.error(info.errors);
}
if (stats.hasWarnings()) {
console.warn(info.warnings);
}
});