This repository has been archived by the owner on Jan 2, 2022. It is now read-only.
generated from hosonokotaro/react-typescript-starter
/
webpack.config.ts
132 lines (129 loc) · 3.11 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
import { CleanWebpackPlugin } from 'clean-webpack-plugin';
import CopyWebpackPlugin from 'copy-webpack-plugin';
import ForkTsCheckerWebpackPlugin from 'fork-ts-checker-webpack-plugin';
import HTMLWebpackPlugin from 'html-webpack-plugin';
import MiniCssExactPlugin from 'mini-css-extract-plugin';
import path from 'path';
import webpack from 'webpack';
import { BundleAnalyzerPlugin } from 'webpack-bundle-analyzer';
const dev = process.env.NODE_ENV === 'development';
const config: webpack.Configuration = {
mode: dev ? 'development' : 'production',
entry: {
index: './src/pages/index.tsx',
edit: './src/pages/edit/index.tsx',
},
output: {
path: path.resolve(__dirname, 'dist/'),
filename: '[name].bundle.[contenthash].js',
publicPath: '/',
},
optimization: {
splitChunks: {
chunks: 'all',
},
},
module: {
rules: [
{
test: /\.(ts|tsx)$/,
exclude: /node_modules/,
use: [
{
loader: 'ts-loader',
options: {
transpileOnly: true,
},
},
],
},
{
test: /\.css$/,
use: [
{
loader: MiniCssExactPlugin.loader,
},
{
loader: 'css-loader',
options: {
sourceMap: dev,
},
},
],
},
{
test: /\.(png|jpe?g|gif)$/i,
use: ['file-loader'],
},
],
},
resolve: {
extensions: ['*', '.js', '.ts', '.tsx'],
alias: {
'~': path.resolve(__dirname, 'src/'),
'@': path.resolve(__dirname, 'src/components/'),
},
},
devServer: {
static: {
directory: path.join(__dirname, 'public/'),
},
port: 3000,
host: '0.0.0.0',
hot: true,
historyApiFallback: {
rewrites: [
{ from: /^\/$/, to: '/index.html' },
{ from: /^\/edit/, to: '/edit/index.html' },
],
},
},
watchOptions: {
poll: 1000,
ignored: ['node_modules'],
},
devtool: dev ? 'inline-source-map' : 'source-map',
plugins: [
new webpack.HotModuleReplacementPlugin(),
new HTMLWebpackPlugin({
chunks: ['index'],
template: './src/pages/index.html',
filename: path.resolve(__dirname, 'dist/index.html'),
}),
new HTMLWebpackPlugin({
chunks: ['edit'],
template: './src/pages/edit/index.html',
filename: path.resolve(__dirname, 'dist/edit/index.html'),
}),
new CleanWebpackPlugin(),
new CopyWebpackPlugin({
patterns: [
{
from: '**/*',
context: 'public/',
noErrorOnMissing: true,
},
],
}),
new ForkTsCheckerWebpackPlugin({
typescript: {
diagnosticOptions: {
semantic: true,
syntactic: true,
},
},
eslint: {
files: './src/**/*.{ts,tsx,js,jsx}',
},
}),
new BundleAnalyzerPlugin({
openAnalyzer: false,
analyzerMode: 'static',
reportFilename: path.resolve(__dirname, 'report/index.html'),
}),
new MiniCssExactPlugin({
filename: 'styles-[contenthash].css',
}),
],
};
export default config;