-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
webpack.config.js
126 lines (120 loc) · 3.15 KB
/
webpack.config.js
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
114
115
116
117
118
119
120
121
122
123
124
125
126
const HtmlWebPackPlugin = require("html-webpack-plugin");
const webpack = require("webpack");
const MiniCssExtractPlugin = require("mini-css-extract-plugin");
// const ESLintPlugin = require("eslint-webpack-plugin");
const nodeExternals = require("webpack-node-externals");
const path = require("path");
const glob = require("glob");
module.exports = (env, argv) => {
const isProd = argv.mode === "production";
const isDevserver = process.argv[2] === "serve";
const filename = isProd ? "[contenthash]" : "[name]";
const srcDir = path.resolve(__dirname, "src");
const outDir = path.resolve(__dirname, "out");
const backend = "http://localhost:3000";
const server = {
name: "server",
entry: path.resolve(srcDir, "server/server.ts"),
target: "node",
output: {
path: outDir,
filename: `server.js`,
},
stats: {
errorDetails: true,
},
plugins: [new webpack.ContextReplacementPlugin(/express/)],
externals: [nodeExternals()],
resolve: {
extensions: [".js", ".ts"],
},
module: {
rules: [
{
test: /\.ts$/,
loader: "babel-loader",
options: {
presets: ["@babel/preset-typescript"],
},
},
],
},
};
const client = {
name: "client",
entry: path.resolve(srcDir, "client/client.tsx"),
output: {
path: outDir,
filename: `${filename}.js`,
assetModuleFilename: `files/${filename}[ext]`,
},
resolve: {
extensions: [".js", ".ts", ".tsx"],
modules: [
path.resolve(__dirname, "node_modules"),
path.resolve(srcDir, "client"),
],
},
module: {
rules: [
{
test: /\.(ts|tsx)$/,
loader: "babel-loader",
options: {
presets: [
[
"@babel/preset-react",
{
//Replaces the import source when importing functions.
//Remove for @babel/core 8
runtime: "automatic",
},
],
"@babel/preset-typescript",
],
},
},
{
test: /\.css$/,
use: [
//Extract styles to .css file.
MiniCssExtractPlugin.loader,
"css-loader",
],
},
{
test: /\.(png|jpg|jpeg|gif|svg|webp)$/,
type: "asset/resource",
},
],
},
plugins: [
//Use index as template file.
new HtmlWebPackPlugin({
template: path.resolve(srcDir, "client/index.html"),
favicon: path.resolve(srcDir, "client/favicon.webp"),
}),
//Extract css styles as external file.
new MiniCssExtractPlugin({
filename: `${filename}.css`,
}),
//Lint JS/TS files
// new ESLintPlugin({
// overrideConfigFile: path.resolve(__dirname, lintConf),
// }),
],
devServer: {
proxy: {
"/rest/**": {
target: `${backend}/`,
changeOrigin: true,
},
"/socket.io/**": {
target: `${backend}/`,
changeOrigin: true,
},
},
},
};
return [client, server];
};