-
Notifications
You must be signed in to change notification settings - Fork 0
/
webpack.config.js
165 lines (158 loc) · 4.5 KB
/
webpack.config.js
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
const path = require('path');
const { CleanWebpackPlugin } = require('clean-webpack-plugin');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const TsconfigPathsPlugin = require('tsconfig-paths-webpack-plugin');
const TerserPlugin = require('terser-webpack-plugin');
const BundleAnalyzerPlugin = require('webpack-bundle-analyzer').BundleAnalyzerPlugin;
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
const CopyWebpackPlugin = require('copy-webpack-plugin');
const webpack = require('webpack');
const analyzeBundle = false;
module.exports = (_, argv) => {
/** phaseは、local | development | production */
const phase = argv.env.phase;
/** modeは、phaseがlocalならdevelopment、それ以外ならproduction */
const mode = phase === 'local' ? 'development' : 'production';
/** isBuildは、modeがproductionかどうか */
const isBuild = mode === 'production';
/** 環境変数の設定 */
const envConfig = require('dotenv').config({
path: path.resolve(__dirname, `./config/.env.${phase}`),
});
const config = {
mode: mode,
entry: {
index: path.resolve(__dirname, 'src', 'index.tsx'),
},
output: {
path: path.resolve(__dirname, 'dist'),
filename: `static/js/[name].${isBuild ? '[contenthash]' : 'bundle'}.js`,
chunkFilename: `static/js/[name].${isBuild ? '[contenthash]' : 'bundle'}.js`,
publicPath: '/',
},
module: {
rules: [
{
test: /\.[jt]sx?$/,
use: [
{
loader: 'babel-loader',
},
{
loader: 'ts-loader',
},
],
exclude: /[\\/]node_modules[\\/]/,
},
{
test: /\.html$/,
use: [
{
loader: 'html-loader',
},
],
},
{
test: /[\\/]public[\\/]/,
type: 'asset/resource',
exclude: /\.html$/,
},
{
test: /\.(sa|sc|c)ss$/,
use: [
{
loader: isBuild ? MiniCssExtractPlugin.loader : 'style-loader',
},
{
loader: 'css-loader',
},
{
loader: 'sass-loader',
},
],
exclude: /[\\/]public[\\/]/,
},
{
test: /\.(ico|gif|png|jpg|jpeg|svg)$/i,
use: [
{
loader: 'file-loader',
options: {
name: `[name]${isBuild ? '.[contenthash]' : ''}.[ext]`,
outputPath: 'static/images',
},
},
],
exclude: /[\\/]public[\\/]/,
},
],
},
resolve: {
extensions: ['.tsx', '.ts', '.js', '.jsx'],
plugins: [new TsconfigPathsPlugin({ configFile: path.resolve(__dirname, 'tsconfig.json') })],
},
optimization: {
splitChunks: isBuild
? {
cacheGroups: {
default: false,
vendor: {
test: /[\\/]node_modules[\\/]/,
name: 'vendor',
chunks: 'all',
},
},
}
: false,
minimize: isBuild,
minimizer: [
new TerserPlugin({
terserOptions: {
output: {
comments: false,
},
compress: {
drop_console: isBuild,
},
},
}),
],
},
plugins: [
new CopyWebpackPlugin({
patterns: [
{
from: path.resolve(__dirname, './src/assets'),
to: 'static/assets',
},
],
}),
new webpack.DefinePlugin({
'process.env': JSON.stringify({
...envConfig.parsed,
}),
}),
isBuild ? new CleanWebpackPlugin() : undefined,
isBuild
? new MiniCssExtractPlugin({
filename: `static/css/[name].[contenthash].css`,
})
: undefined,
new HtmlWebpackPlugin({
template: path.resolve(__dirname, './public/index.html'),
}),
analyzeBundle ? new BundleAnalyzerPlugin() : undefined,
].filter((plugin) => plugin),
devServer: {
static: {
directory: path.join(__dirname, 'public'),
},
port: 3000,
compress: true,
hot: true,
open: true,
historyApiFallback: true,
},
};
return config;
};