-
Notifications
You must be signed in to change notification settings - Fork 1
/
webpack.config.js
206 lines (202 loc) · 6.52 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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
const fs = require('fs');
const path = require('path');
const glob = require('glob');
const webpack = require('webpack');
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
const CssMinimizerPlugin = require('css-minimizer-webpack-plugin');
const { CleanWebpackPlugin } = require('clean-webpack-plugin');
const CopyPlugin = require('copy-webpack-plugin');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const PreloadWebpackPlugin = require('@vue/preload-webpack-plugin');
const ESLintPlugin = require('eslint-webpack-plugin');
const ForkTsCheckerWebpackPlugin = require('fork-ts-checker-webpack-plugin');
const { SubresourceIntegrityPlugin } = require('webpack-subresource-integrity');
const CircularDependencyPlugin = require('circular-dependency-plugin');
const { BundleAnalyzerPlugin } = require('webpack-bundle-analyzer');
const StatoscopeWebpackPlugin = require('@statoscope/webpack-plugin').default;
const DEV_ENV = '.env.dev';
if (fs.existsSync(DEV_ENV)) require('dotenv').config({ path: DEV_ENV });
module.exports = (env, options) => {
const isProd = options.mode === 'production';
return {
entry: './src/index',
output: {
filename: '[name].[contenthash:8].js',
path: path.join(__dirname, 'dist'),
publicPath: '/',
crossOriginLoading: 'anonymous',
},
optimization: {
runtimeChunk: true,
splitChunks: {
chunks: 'all',
cacheGroups: {
commons: {
name: 'commons',
test: /_common\/components/,
reuseExistingChunk: true,
minChunks: 2,
},
},
},
minimizer: [`...`, new CssMinimizerPlugin()],
},
devtool: !isProd ? 'eval-source-map' : false,
devServer: {
static: {
directory: path.join(__dirname, 'public'),
},
client: {
overlay: false,
},
compress: true,
server: 'spdy',
historyApiFallback: true,
proxy: {
'/api': {
target: process.env.DEV_API || '',
changeOrigin: true,
logLevel: 'debug',
pathRewrite: process.env.USE_API_REWRITE ? { '^/api': '' } : undefined,
},
},
},
resolve: {
modules: ['node_modules', path.join(__dirname, 'src')],
extensions: ['.tsx', '.ts', '.js', '.json'],
},
module: {
rules: [
{
test: /pdf\.worker\.min\.js$/,
type: 'asset/resource',
generator: {
filename: 'workers/pdf.worker.[hash][ext]',
},
},
{
test: /\.(ts)x?$/,
exclude: /node_modules/,
use: [
{
loader: 'babel-loader',
options: {
exclude: [/node_modules[\\/]core-js/],
presets: ['solid'],
plugins: [['polyfill-corejs3', { method: 'usage-global' }]],
},
},
{
loader: 'ts-loader',
options: {
transpileOnly: true,
onlyCompileBundledFiles: true,
},
},
],
},
{
test: /\.css$/,
exclude: /node_modules/,
use: [
isProd ? MiniCssExtractPlugin.loader : 'style-loader',
{
loader: 'css-loader',
options: {
importLoaders: 1,
modules: {
mode: 'local',
exportLocalsConvention: 'dashesOnly',
localIdentName: isProd ? '[hash:base64:5]' : '[name]__[local]__[hash:base64:5]',
},
},
},
{
loader: 'postcss-loader',
options: {
postcssOptions: {
plugins: {
'postcss-nesting': {},
'postcss-custom-media': { importFrom: 'src/media.css' },
...(isProd && { autoprefixer: { flexbox: 'no-2009' } }),
},
},
},
},
],
},
{
test: /\.woff2?$/,
type: 'asset/resource',
generator: {
filename: 'fonts/[name].[hash:8][ext]',
},
},
{
test: /icons\/[a-z-]+\.svg$/,
type: 'asset/source',
},
{
test: /\.(svg|png)$/,
exclude: /icons\/[a-z-]+\.svg$/,
type: 'asset/resource',
generator: {
filename: 'static/[hash:8][ext]',
},
},
],
},
plugins: [
new CleanWebpackPlugin(),
isProd &&
new CopyPlugin({
patterns: [
{ from: 'public/favicons', to: 'favicons' },
{ from: 'public/browserconfig.xml' },
{ from: 'public/site.webmanifest' },
{ from: 'public/stripe-reveal.html' },
],
}),
new webpack.ids.HashedModuleIdsPlugin(),
new webpack.DefinePlugin({
'process.env.NODE_ENV': JSON.stringify(options.mode),
'process.env.STRIPE_PUB_KEY': JSON.stringify(process.env.STRIPE_PUB_KEY),
'process.env.STRIPE_ACCOUNT': JSON.stringify(process.env.STRIPE_ACCOUNT),
'process.env.MIXPANEL_PROJECT_TOKEN': JSON.stringify(process.env.MIXPANEL_PROJECT_TOKEN),
'process.env.LAUNCHDARKLY_CLIENT_ID': JSON.stringify(process.env.LAUNCHDARKLY_CLIENT_ID),
}),
new HtmlWebpackPlugin({ template: './public/index.html' }),
new PreloadWebpackPlugin({
rel: 'preload',
include: 'initial',
fileWhitelist: [
/PPNeueMontreal-Regular\..+?\.woff2$/,
/PPNeueMontreal-Bold\..+?\.woff2$/,
/pptelegraf-light-webfont\..+?\.woff2$/,
/pptelegraf-regular-webfont\..+?\.woff2$/,
],
}),
new ESLintPlugin({
files: './src/**/*.{ts,tsx}',
failOnError: isProd,
failOnWarning: isProd,
}),
new ForkTsCheckerWebpackPlugin(),
new MiniCssExtractPlugin({
filename: '[name].[contenthash:8].css',
experimentalUseImportModule: true,
ignoreOrder: true,
}),
new SubresourceIntegrityPlugin(),
env.analyzer === 'default' && new CircularDependencyPlugin({ exclude: /node_modules/ }),
env.analyzer === 'default' && new BundleAnalyzerPlugin(),
env.analyzer === 'statoscope' &&
new StatoscopeWebpackPlugin({
name: 'capital',
saveTo: '.stats/[name]-[hash].html',
saveStatsTo: '.stats/[name]-[hash].json',
additionalStats: glob.sync('.stats/*.json'),
}),
].filter(Boolean),
};
};