-
-
Notifications
You must be signed in to change notification settings - Fork 236
Expand file tree
/
Copy pathbase.config.ts
More file actions
172 lines (161 loc) · 4.51 KB
/
base.config.ts
File metadata and controls
172 lines (161 loc) · 4.51 KB
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
import path from "path";
import webpack, {
Configuration,
WebpackPluginInstance,
RuleSetUseItem,
} from "webpack";
import { WebpackManifestPlugin } from "webpack-manifest-plugin";
import TerserPlugin from "terser-webpack-plugin";
import MiniCssExtractPlugin from "mini-css-extract-plugin";
import ForkTsCheckerWebpackPlugin from "fork-ts-checker-webpack-plugin";
import LoadablePlugin from "@loadable/webpack-plugin";
import { BundleAnalyzerPlugin } from "webpack-bundle-analyzer";
export const isDev = process.env.NODE_ENV === "development";
const getStyleLoaders = (isWeb: boolean, isSass?: boolean) => {
let loaders: RuleSetUseItem[] = [
{
loader: "css-loader",
options: {
importLoaders: isSass ? 2 : 1,
modules: {
auto: true,
localIdentName: isDev ? "[path][name]__[local]" : "[hash:base64]",
exportOnlyLocals: !isWeb,
},
},
},
{ loader: "postcss-loader" },
];
if (isWeb) loaders = [MiniCssExtractPlugin.loader, ...loaders];
if (isSass) loaders = [...loaders, { loader: "sass-loader" }];
return loaders;
};
const getPlugins = (isWeb: boolean) => {
let plugins = [
new webpack.ProgressPlugin(),
new WebpackManifestPlugin({
fileName: path.resolve(process.cwd(), "public/webpack-assets.json"),
filter: (file) => file.isInitial,
}),
new LoadablePlugin({
writeToDisk: true,
filename: "../loadable-stats.json",
}),
// Setting global variables
new webpack.DefinePlugin({
__CLIENT__: isWeb,
__SERVER__: !isWeb,
__DEV__: isDev,
}),
];
if (isDev)
plugins = [
...plugins,
// Runs TypeScript type checker on a separate process
new ForkTsCheckerWebpackPlugin({
// (Required) Same as eslint command
eslint: { files: "./src/**/*.{js,jsx,ts,tsx}" },
}),
];
if (!isDev)
plugins = [
...plugins,
// Visualize size of webpack output files, see: https://github.com/webpack-contrib/webpack-bundle-analyzer
new BundleAnalyzerPlugin({
analyzerMode:
process.env.NODE_ENV === "analyze" ? "server" : "disabled",
}),
];
return plugins;
};
const config = (isWeb = false): Configuration => ({
mode: isDev ? "development" : "production",
stats: "minimal",
context: path.resolve(process.cwd()),
output: { clean: true },
optimization: {
minimizer: [
new TerserPlugin({
// See more options: https://github.com/webpack-contrib/terser-webpack-plugin#terseroptions
terserOptions: { compress: { drop_console: true } },
}),
],
},
plugins: getPlugins(isWeb) as WebpackPluginInstance[],
module: {
rules: [
{
test: /\.(t|j)sx?$/,
exclude: /node_modules/,
loader: "babel-loader",
options: {
caller: { target: isWeb ? "web" : "node" },
cacheDirectory: isDev,
},
},
{
test: /\.css$/,
use: getStyleLoaders(isWeb),
},
{
test: /\.(scss|sass)$/,
use: getStyleLoaders(isWeb, true),
},
{
test: /\.(woff2?|eot|ttf|otf)$/i,
type: "asset",
generator: { emit: isWeb },
},
{
test: /\.(png|jpe?g|gif)$/i,
type: "asset",
generator: { emit: isWeb },
},
{
test: /\.svg$/i,
oneOf: [
{
// automatically chooses between exporting a data URI and emitting a separate file for SVG's referenced in CSS/SCSS
type: "asset",
generator: { emit: isWeb },
issuer: {
and: [/\.(sa|sc|c)ss$/],
},
},
{
// use SVG as the URI or ReactComponent
use: [
{
loader: "@svgr/webpack",
options: {
prettier: false,
svgo: true,
svgoConfig: {
plugins: [{ removeViewBox: false }],
},
titleProp: true,
},
},
{
loader: "file-loader",
options: {
publicPath: "/assets/",
},
},
],
type: "javascript/auto",
generator: { emit: isWeb },
issuer: {
and: [/\.(t|j)sx?$/],
},
},
],
},
],
},
resolve: {
modules: ["src", "node_modules"],
extensions: [".ts", ".tsx", ".js", ".jsx", ".json"],
},
});
export default config;