-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwebpack.config.ts
More file actions
180 lines (156 loc) · 4.72 KB
/
Copy pathwebpack.config.ts
File metadata and controls
180 lines (156 loc) · 4.72 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
173
174
175
176
177
178
179
180
import path from 'path';
import ReactRefreshWebpackPlugin from '@pmmmwh/react-refresh-webpack-plugin';
import CopyWebpackPlugin from 'copy-webpack-plugin';
import DotenvWebpackPlugin from 'dotenv-webpack';
import ForkTsCheckerWebpackPlugin from 'fork-ts-checker-webpack-plugin';
import HtmlWebpackPlugin from 'html-webpack-plugin';
import MiniCssExtractPlugin from 'mini-css-extract-plugin';
import { TsconfigPathsPlugin } from 'tsconfig-paths-webpack-plugin';
import Webpack from 'webpack';
import DevServer from 'webpack-dev-server';
const ROOT = path.resolve(__dirname);
const SRC = path.join(ROOT, 'src');
const DIST = path.join(ROOT, 'dist');
const PUBLIC = path.join(ROOT, 'public');
type Falsy = false | null | undefined | 0 | '';
type TruthyPredicate = <T>(x: T | Falsy) => x is T;
const styleExt = '(css|sass|scss)';
const extMap = {
script: /\.(tsx?|jsx?)$/i,
style: new RegExp(`\\.${styleExt}$`, 'i'),
styleModule: new RegExp(`\\.module\\.${styleExt}$`, 'i'),
};
const assetExtMap = {
font: /\.(woff2?|eot|ttf|otf)(\?.*)?$/i,
image: /\.(ico|jpe?g|a?png|gif|webp)(\?.*)?$/i,
media: /\.(mp4|webm|wav|mp3|m4a|aac|oga)(\?.*)?$/i,
};
export const getStylesLoaders = (
isDev: boolean,
withModules = false
): Webpack.RuleSetUseItem[] => {
const injectLoader = isDev ? 'style-loader' : MiniCssExtractPlugin.loader;
const localIdentName = isDev ? '[path][name]__[local]' : '[hash:base64]';
const cssLoader = withModules
? {
loader: 'css-loader',
options: { modules: { localIdentName } },
}
: 'css-loader';
const postcssLoader = {
loader: 'postcss-loader',
options: { postcssOptions: { plugins: ['autoprefixer'] } },
};
return [injectLoader, cssLoader, postcssLoader, 'sass-loader'];
};
const getConfig = (env: Record<string, string>): Webpack.Configuration => {
const isDev = Boolean(env.WEBPACK_SERVE);
const isProd = Boolean(env.WEBPACK_BUILD);
const mode = isDev ? 'development' : 'production';
process.env.NODE_ENV = mode;
const extensions = ['.ts', '.tsx', '.js', '.jsx', '.css', '.scss', '.sass'];
const devServer: DevServer.Configuration = {
host: 'localhost',
port: 7007,
static: {
directory: PUBLIC,
},
hot: true,
open: true,
historyApiFallback: true,
};
return {
mode,
devServer,
entry: {
index: path.join(SRC, 'index.tsx'),
},
output: {
path: DIST,
filename: 'static/script/bundle-[contenthash].js',
publicPath: '/',
clean: true,
},
optimization: {
chunkIds: 'named',
splitChunks: {
chunks: 'all',
cacheGroups: {
vendors: {
test: /[\\/]node_modules[\\/]/,
priority: -10,
reuseExistingChunk: true,
filename: 'static/script/vendor/[name].min.js',
name(
module: Webpack.Module
// chunks: Webpack.Chunk[],
// cacheGroupKey: string
) {
const moduleFileName = module
.identifier()
.replace(/.*node_modules\/([^/]*).*/, '$1');
return moduleFileName;
},
},
},
},
},
target: 'browserslist',
devtool: isDev ? 'eval-source-map' : 'hidden-source-map',
module: {
rules: [
{
test: extMap.script,
exclude: /node_modules/,
use: 'babel-loader',
},
{
test: extMap.styleModule,
use: getStylesLoaders(isDev, true),
},
{
test: extMap.style,
exclude: extMap.styleModule,
use: getStylesLoaders(isDev, false),
sideEffects: true,
},
{
test: /\.svg$/i,
issuer: extMap.script,
use: '@svgr/webpack',
},
...Object.entries(assetExtMap).map(([type, ext]) => ({
test: ext,
type: 'asset',
parser: { dataUrlCondition: { maxSize: 1024 } },
generator: { filename: `static/${type}/[path][name][ext]` },
})),
],
},
resolve: {
extensions,
plugins: [new TsconfigPathsPlugin({ extensions })],
},
plugins: [
isProd &&
new CopyWebpackPlugin({
patterns: [{ from: PUBLIC, to: DIST }],
}),
new DotenvWebpackPlugin({
path: './.env.local',
systemvars: true,
}),
new HtmlWebpackPlugin({
template: path.join(SRC, 'index.html'),
inject: 'body',
}),
isProd &&
new MiniCssExtractPlugin({
filename: 'static/style/bundle-[fullhash].css',
}),
isDev && new ReactRefreshWebpackPlugin(),
new ForkTsCheckerWebpackPlugin(),
].filter(Boolean as unknown as TruthyPredicate),
};
};
export default getConfig;