-
Notifications
You must be signed in to change notification settings - Fork 1
/
webpack.config.ts
150 lines (145 loc) · 4.76 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
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
import { CleanWebpackPlugin } from "clean-webpack-plugin";
import CopyPlugin from "copy-webpack-plugin";
import HtmlWebpackPlugin from "html-webpack-plugin";
import * as path from "path";
import TsconfigPathsPlugin from "tsconfig-paths-webpack-plugin";
import * as webpack from "webpack";
import WebpackDevServer from "webpack-dev-server";
import webpackNodeExternals from "webpack-node-externals";
import MiniCssExtractPlugin from "mini-css-extract-plugin";
const clientConfig: (env: "dev" | "prod") => WebpackDevServer.Configuration & webpack.Configuration = (env: "dev" | "prod") => {
const isDev = env === "dev";
return {
name: "client",
mode: isDev ? "development" : "production",
watch: isDev,
devtool: "source-map",
target: "web",
entry: {
app: isDev ? ["webpack-hot-middleware/client?reload=true&quiet=true", "./src/client/ts/app.ts"] : ["./src/client/ts/app.ts"]
},
output: {
path: path.resolve(__dirname, "dist/client"),
filename: "[name].js",
publicPath: ""
},
resolve: {
extensions: [".js", ".ts", ".tsx"],
plugins: [
new TsconfigPathsPlugin({
configFile: "src/client/ts/tsconfig.json"
})
]
},
module: {
rules: [
{
test: /\.tsx?$/,
use: [{
loader: "ts-loader",
options: {
configFile: "src/client/ts/tsconfig.json"
}
}],
},
{
test: /\.s[ac]ss$/i,
use: [
isDev ? "style-loader" : MiniCssExtractPlugin.loader,
{
loader: "css-loader",
options: { url: false }
},
"sass-loader"
]
}
]
},
plugins: [
new CleanWebpackPlugin({
cleanOnceBeforeBuildPatterns: [path.join(__dirname, "dist/client/**/*")],
protectWebpackAssets: true,
cleanStaleWebpackAssets: false
}),
new webpack.HotModuleReplacementPlugin(),
new HtmlWebpackPlugin({
template: "!!raw-loader!src/client/dom/index.ejs",
filename: "index.ejs"
}),
new CopyPlugin({
patterns: [
{ from: "src/client/images", to: "images" },
//{ from: "src/client/fonts", to: "fonts" },
]
}),
new webpack.DefinePlugin({
__IS_DEV__: JSON.stringify(isDev)
}),
isDev ? () => {} : new MiniCssExtractPlugin(),
].filter(Boolean)
}
};
const serverConfig: (env: any) => webpack.Configuration = (env) => {
const isDev = env === "dev";
return {
name: "server",
mode: isDev ? "development" : "production",
watch: isDev,
devtool: "source-map",
target: "node",
node: {
__dirname: true,
__filename: true
},
externals: [webpackNodeExternals()],
entry: {
app: path.resolve(__dirname, "src/server/app.ts"),
},
output: {
path: path.resolve(__dirname, "dist/server"),
filename: "[name].js"
},
resolve: {
extensions: [".js", ".ts", ".tsx"],
plugins: [
new TsconfigPathsPlugin({
configFile: "src/server/tsconfig.json"
}),
],
modules: [
path.resolve(__dirname, "node_modules"),
path.resolve(__dirname, "src/server")
],
alias: {
common: path.resolve(__dirname, "src/common")
}
},
module: {
rules: [
{
test: /\.tsx?$/,
use: [{
loader: "ts-loader",
options: {
configFile: path.resolve(__dirname, "src/server/tsconfig.json")
}
}],
}
]
},
plugins: [
new CleanWebpackPlugin({
cleanOnceBeforeBuildPatterns: [path.join(__dirname, "dist/server/**/*")]
}),
new webpack.DefinePlugin({
__IS_DEV__: JSON.stringify(isDev)
})
]
}
};
export default (env: "dev" | "prod") => {
return [
clientConfig(env),
serverConfig(env)
];
}