-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathwebpack.config.js
More file actions
121 lines (117 loc) · 3.14 KB
/
webpack.config.js
File metadata and controls
121 lines (117 loc) · 3.14 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
const path = require('path');
const RefreshWebpackPlugin = require('@pmmmwh/react-refresh-webpack-plugin');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
const TerserPlugin = require('terser-webpack-plugin');
const webpack = require('webpack');
module.exports = (env) => {
const { NODE_ENV } = env;
console.log(`[NODE_ENV] >>> ${NODE_ENV}`);
if (!['production', 'development'].includes(NODE_ENV))
throw Error('[NODE_ENV] must be production or development');
const DEV = NODE_ENV === 'development';
const mode = DEV ? 'development' : 'production';
const devtool = DEV ? 'eval-source-map' : false;
const lastCssLoader = DEV ? 'style-loader' : MiniCssExtractPlugin.loader;
const miniCssExtractPlugin = DEV
? { apply: () => {} }
: new MiniCssExtractPlugin({ filename: 'css/style.css' });
const refreshWebpackPlugin = DEV ? new RefreshWebpackPlugin() : { apply: () => {} };
const refreshBabel = DEV ? 'react-refresh/babel' : {};
const BASE_URL = DEV ? '' : 'fe-vm';
const definePlugin = new webpack.DefinePlugin({
BASE_URL: JSON.stringify(BASE_URL),
});
return {
mode,
devtool,
resolve: {
fallback: { fs: false, path: false },
alias: {
'@': path.resolve(__dirname, 'src'),
},
extensions: ['.js', '.ts', '.jsx', '.tsx'],
},
entry: {
main: './src/index.tsx',
},
module: {
rules: [
{
test: /\.(ts|tsx|js|jsx)$/,
loader: 'babel-loader',
exclude: /node_modules/,
options: {
presets: [
[
'@babel/preset-env',
{
useBuiltIns: 'usage',
corejs: { version: 3, proposals: true },
debug: true,
},
],
'@babel/preset-react',
'@babel/preset-typescript',
],
plugins: [refreshBabel, 'styled-components'],
},
},
{
test: /\.(sc|c|sa)ss$/,
use: [lastCssLoader, 'css-loader'],
},
],
},
plugins: [
new webpack.ProvidePlugin({
React: 'react',
}),
definePlugin,
miniCssExtractPlugin,
new HtmlWebpackPlugin({
template: path.join(__dirname, 'public', 'index.html'),
}),
refreshWebpackPlugin,
],
devServer: {
open: true,
historyApiFallback: true,
static: {
directory: path.join(__dirname, 'public'),
},
hot: true,
client: {
overlay: {
errors: true,
warnings: false,
},
},
},
output: {
clean: true,
path: path.resolve(__dirname, 'dist'),
},
optimization: {
usedExports: true,
minimize: true,
minimizer: [
new TerserPlugin({
terserOptions: {
compress: {
drop_console: true,
},
},
}),
],
splitChunks: {
chunks: 'all',
},
},
performance: {
hints: false,
maxEntrypointSize: 512000,
maxAssetSize: 512000,
},
};
};