-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebpack.common.ts
175 lines (169 loc) · 4.65 KB
/
webpack.common.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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
import { readdirSync } from "fs";
import { resolve as resolvePath } from "path";
import ForkTsCheckerWebpackPlugin from "fork-ts-checker-webpack-plugin";
import HtmlWebpackPlugin from "html-webpack-plugin";
import ImageMinimizerPlugin from "image-minimizer-webpack-plugin";
import MiniCssExtractWebpackPlugin from "mini-css-extract-plugin";
import TerserWebpackPlugin from "terser-webpack-plugin";
import TsconfigPathsWebpackPlugin from "tsconfig-paths-webpack-plugin";
import { Configuration, DefinePlugin } from "webpack";
function generateStylesheetAliases() {
const aliases = {};
const basePath = "src/app/Assets/Styles";
const stylesheets = readdirSync(resolvePath(__dirname, basePath)).filter(
(s) => !s.includes(".d.ts")
);
for (const stylesheet of stylesheets) {
aliases[stylesheet.substring(1, stylesheet.indexOf("."))] = resolvePath(
__dirname,
`${basePath}/${stylesheet}`
);
}
return aliases;
}
const imageMinimizerPluginFileMatcher = /\.(jpe?g|png|svg)$/i;
const baseConfig: Configuration = {
entry: "./src/main.tsx",
output: {
path: resolvePath(__dirname, "dist"),
filename: "[name].[chunkhash].js",
publicPath: "/"
},
resolve: {
extensions: [".ts", ".tsx", ".js", ".jsx", ".jpg", ".png"],
plugins: [new TsconfigPathsWebpackPlugin()],
alias: {
...generateStylesheetAliases(),
"@SVG": resolvePath(__dirname, "src/app/Assets/SVG"),
"@Images": resolvePath(__dirname, "src/app/Assets/Images")
}
},
plugins: [
new DefinePlugin({
__REACT_DEVTOOLS_GLOBAL_HOOK__: "({ isDisabled: true })"
}),
new ForkTsCheckerWebpackPlugin({
typescript: {
diagnosticOptions: {
semantic: true,
syntactic: true
},
mode: "write-references",
build: false,
configFile: resolvePath(__dirname, "./tsconfig.json"),
configOverwrite: {
compilerOptions: {
outDir: "./dist"
}
}
}
}),
new HtmlWebpackPlugin({
template: resolvePath(__dirname, "public/index.html"),
favicon: resolvePath(__dirname, "public/favicon.ico"),
inject: true
}),
new MiniCssExtractWebpackPlugin({
filename: "assets/styles/css/[name].[contenthash:8].css",
chunkFilename: "assets/styles/css/[name].[contenthash:8].chunk.css"
})
],
optimization: {
minimize: true,
minimizer: [
new TerserWebpackPlugin(),
new ImageMinimizerPlugin({
minimizer: {
implementation: ImageMinimizerPlugin.imageminMinify,
options: {
test: imageMinimizerPluginFileMatcher,
plugins: ["imagemin-mozjpeg", "imagemin-pngquant", "imagemin-svgo", "svgo"]
}
}
})
],
runtimeChunk: "single",
splitChunks: {
cacheGroups: {
vendor: {
test: /[\\/]node_modules[\\/]/,
name: "vendors",
chunks: "all"
}
}
}
},
performance: {
hints: false
},
module: {
rules: [
{
test: /\.svg$/i,
type: "asset",
resourceQuery: /url/ // *.svg?url
},
// {
// test: imageFileMatcher,
// loader: ImageMinimizerPlugin.loader,
// enforce: "pre",
// options: {
// minimizer: {
// implementation: ImageMinimizerPlugin.imageminMinify,
// options: imageMinimizerPluginOptions
// }
// }
// },
{
test: /\.(png|jpg|jpeg|gif)$/i,
type: "asset/resource"
},
{
test: /\.svg$/i,
issuer: /\.[jt]sx?$/,
resourceQuery: { not: [/url/] }, // exclude react component if *.svg?url
use: ["@svgr/webpack"]
},
{
test: /\.ts(x)?$/,
exclude: /node_modules/,
use: {
loader: "babel-loader",
options: {
cacheDirectory: true,
cacheCompression: false
}
}
},
{
test: /\.scss$/,
use: [
MiniCssExtractWebpackPlugin.loader,
{
loader: "@teamsupercell/typings-for-css-modules-loader",
options: {
formatter: "prettier",
prettierConfigFile: resolvePath(__dirname, ".prettierrc.js")
}
},
{
loader: "css-loader",
options: { modules: true }
},
"sass-loader"
]
},
{
test: /\.css$/,
exclude: "/node_modules/",
use: [
MiniCssExtractWebpackPlugin.loader,
"@teamsupercell/typings-for-css-modules-loader",
"css-loader",
"postcss-loader"
]
}
]
}
};
export default baseConfig;