-
Notifications
You must be signed in to change notification settings - Fork 0
/
webpack.client.config.ts
153 lines (137 loc) · 3.78 KB
/
webpack.client.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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
import * as path from "path";
import * as webpack from "webpack";
import "webpack-dev-server";
import { CleanWebpackPlugin } from "clean-webpack-plugin";
import template from "html-webpack-template";
import HtmlWebpackPlugin from "html-webpack-plugin";
import MiniCssExtractPlugin from "mini-css-extract-plugin";
import TsconfigPathsPlugin from "tsconfig-paths-webpack-plugin";
export interface ClientWebpackOptions {
dirname?: string;
addHmrMiddleware?: boolean;
}
export function createConfig(options: ClientWebpackOptions = {}) {
const dirname = options.dirname || __dirname;
const isProduction = process.env.NODE_ENV === "production";
console.log(`Client isProduction: ${isProduction}`);
const entry = {
ssr: "./ssr",
client: [isProduction ? "./bootstrap.prod" : "./bootstrap.dev"],
};
const config: webpack.Configuration = {
mode: isProduction ? "production" : "development",
context: path.resolve(dirname, "client"),
entry,
optimization: {
usedExports: true,
providedExports: true,
splitChunks: {
cacheGroups: {
vendor: {
test: /node_modules/,
chunks: (v) => v.name !== "ssr",
name: "vendors",
enforce: true,
},
},
},
},
output: {
path: path.resolve(dirname, "dist/public"),
publicPath: "/",
filename: (c) =>
c.chunk.name === "ssr" ? "[name].js" : "[name].[hash].js",
chunkFilename: "[name].[hash].js",
libraryTarget: "umd",
globalObject: "this",
},
resolve: {
extensions: [".tsx", ".ts", ".js", ".jsx"],
alias: {
"react-dom": isProduction ? "react-dom" : "@hot-loader/react-dom",
},
plugins: [
new TsconfigPathsPlugin({
baseUrl: dirname,
configFile: path.join(dirname, "tsconfig.json"),
}),
],
},
devtool: isProduction ? "nosources-source-map" : "#eval",
devServer: {
contentBase: path.join(__dirname, "dist/public"),
disableHostCheck: true,
historyApiFallback: true,
hot: !isProduction,
port: 3000,
},
module: {
rules: [
{
test: /\.tsx?$/,
exclude: /node_modules/,
loader: "ts-loader",
options: {
compilerOptions: {
target: "ES5",
},
},
},
{
test: /\.(graphql|gql)$/,
exclude: /node_modules/,
loader: "graphql-tag/loader",
},
{
test: /\.css$/i,
use: [
{
loader: MiniCssExtractPlugin.loader,
},
"css-loader",
],
},
{
test: /\.(ttf|eot|svg|woff|woff2|png)(\?v=[0-9]\.[0-9]\.[0-9])?$/,
loader: "file-loader",
options: {
name: "assets/[name].[hash].[ext]",
},
},
],
},
plugins: [
new CleanWebpackPlugin({}),
new MiniCssExtractPlugin({
filename: "[name].[hash].css",
chunkFilename: "[name].[hash].[id].css",
ignoreOrder: false,
}),
new HtmlWebpackPlugin({
template,
title: "Simple Market",
meta: {
viewport: "width=device-width, initial-scale=1",
},
appMountId: "app",
minify: true,
inject: true,
lang: "ru-RU",
excludeChunks: ["ssr"],
}),
new webpack.DefinePlugin({
"process.env.NODE_ENV": JSON.stringify(process.env.NODE_ENV),
}),
],
};
if (options.addHmrMiddleware) {
entry.client = ["webpack-hot-middleware/client?overlay=false"].concat(
entry.client
);
}
if (!isProduction) {
config.plugins?.push(new webpack.HotModuleReplacementPlugin({}));
}
return config;
}
export default createConfig;