-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathwebpack.config.js
172 lines (164 loc) · 5.17 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
166
167
168
169
170
171
172
const path = require('path');
const webpack = require('webpack');
const app = require('./package.json');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const CleanWebpackPlugin = require('clean-webpack-plugin');
const CopyWebpackPlugin = require('copy-webpack-plugin');
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
const PrintTimeWebpackPlugin = require('print-time-webpack');
const HeaderInjectionWebpackPlugin = require('@bndynet/header-injection-webpack-plugin');
// const ReactRefreshWebpackPlugin = require('@pmmmwh/react-refresh-webpack-plugin');
const { BaseHrefWebpackPlugin } = require('base-href-webpack-plugin');
function resolveTsconfigPathsToAlias({
tsconfigPath = './tsconfig.json',
webpackConfigBasePath = './',
} = {}) {
const { paths } = require(tsconfigPath).compilerOptions;
const aliases = {};
Object.keys(paths).forEach(item => {
const key = item.replace('/*', '');
const value = path.resolve(
webpackConfigBasePath,
paths[item][0].replace('/*', ''),
);
aliases[key] = value;
});
return aliases;
}
const isDevelopment = process.env.NODE_ENV !== 'production';
module.exports = env => ({
entry: ['./src/index.tsx'],
performance: {
hints: false,
}, // disable to show warnings about performance
output: {
filename: '[name].[chunkhash].js',
path: path.resolve(__dirname, 'dist'),
},
resolve: {
extensions: ['.ts', '.tsx', '.js', '.jsx', '.scss', 'css'],
alias: {
...resolveTsconfigPathsToAlias(),
react: path.join(__dirname, 'node_modules/react'),
},
},
module: {
rules: [
{
test: /\.(png|jpg|gif)$/,
use: [
{
loader: 'url-loader',
options: {
limit: 8192,
},
},
],
},
{
test: /\.(woff(2)?|ttf|eot|svg)(\?v=\d+\.\d+\.\d+)?$/,
use: [
{
loader: 'url-loader',
},
],
},
{
test: /\.(sa|sc|c)ss$/,
use: [
isDevelopment
? 'style-loader'
: MiniCssExtractPlugin.loader,
MiniCssExtractPlugin.loader,
'css-loader',
'postcss-loader',
'sass-loader',
],
},
{
test: /\.[jt]sx?$/,
exclude: /node_modules/,
use: [
{
loader: 'babel-loader',
// options: {
// plugins: [
// isDevelopment && 'react-refresh/babel',
// ].filter(Boolean),
// },
},
],
},
],
},
plugins: [
new PrintTimeWebpackPlugin(),
new CleanWebpackPlugin(['dist']),
new HtmlWebpackPlugin({
title: app.title,
meta: {
author: app.author,
description: app.description,
},
inject: true,
template: './assets/index.html',
}),
new BaseHrefWebpackPlugin({
baseHref: env && env.baseHref,
}),
// This makes it possible for us to safely use env vars on our code
new webpack.DefinePlugin({
APP_NAME: JSON.stringify(app.name),
APP_VERSION: JSON.stringify(app.version),
APP_BUILD: JSON.stringify(Date.now()),
APP_BASEHREF: JSON.stringify(
env && env.baseHref ? env.baseHref : '/',
),
}),
new webpack.ProvidePlugin({
React: 'react',
ReactDOM: 'react-dom',
}),
new CopyWebpackPlugin([
{
from: './assets',
},
{
from: './README.md',
},
{
from: './README.zh-CN.md',
},
]),
new MiniCssExtractPlugin({
filename: '[name].[hash].css',
chunkFilename: '[id].[hash].css',
}),
new HeaderInjectionWebpackPlugin(),
// react refresh
// isDevelopment && new webpack.HotModuleReplacementPlugin(),
// isDevelopment && new ReactRefreshWebpackPlugin(),
],
// .filter(Boolean)
optimization: {
splitChunks: {
chunks: 'async',
minSize: 30000,
minChunks: 1,
maxAsyncRequests: 5,
maxInitialRequests: 3,
name: true,
cacheGroups: {
default: {
minChunks: 2,
priority: -20,
reuseExistingChunk: true,
},
vendors: {
test: /[\\/]node_modules[\\/]/,
priority: -10,
},
},
},
},
});