-
Notifications
You must be signed in to change notification settings - Fork 6
/
webpack.config.js
160 lines (153 loc) · 4.99 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
const path = require('path');
const webpack = require('webpack');
const CopyWebpackPlugin = require('copy-webpack-plugin');
const ESLintPlugin = require('eslint-webpack-plugin');
const FaviconsWebpackPlugin = require('favicons-webpack-plugin');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const StylelintPlugin = require('stylelint-webpack-plugin');
const API_ORIGIN = process.env.API_ORIGIN || '';
const devServerPort = 3000;
const publicPath = '/dist/';
const config = {
entry: './src/app/main.js',
output: {
path: path.resolve(__dirname, 'dist'),
publicPath
},
module: {
rules: [
{
test: /\.jsx?$/,
exclude: /node_modules/,
loader: 'esbuild-loader',
options: {loader: 'jsx'}
},
{
test: /\.d\.ts$/,
loader: 'ignore-loader'
},
{
test: /\.tsx?$/,
exclude: /node_modules|\.d\.ts$/,
use: 'ts-loader'
},
{
test: /\.s?css$/,
use: [
'style-loader',
'fast-css-loader',
{
loader: 'fast-sass-loader',
options: {
implementation: require('node-sass'),
includePaths: ['./src/styles', './node_modules']
}
}
]
}
]
},
optimization: {
runtimeChunk: 'single',
splitChunks: {
chunks: 'all',
maxInitialRequests: Infinity,
minSize: 0,
cacheGroups: {
vendor: {
test: /[\\/]node_modules[\\/]/,
name(module) {
// get the name. E.g. node_modules/packageName/not/this/part.js
// or node_modules/packageName
const packageName = module.context.match(
/[\\/]node_modules[\\/](.*?)([\\/]|$)/
)[1];
// npm package names are URL-safe, but some servers don't like @ symbols
return `npm.${packageName.replace('@', '')}`;
}
}
}
}
},
plugins: [
new CopyWebpackPlugin({
patterns: [{from: 'src/images', to: 'images'}]
}),
new webpack.EnvironmentPlugin({API_ORIGIN}),
new ESLintPlugin({fix: true}),
new FaviconsWebpackPlugin('./src/images/favicon.svg'),
new HtmlWebpackPlugin({
template: './src/index.html'
}),
new StylelintPlugin({
files: './src/**/*.scss'
})
],
performance: {
maxEntrypointSize: 2.5 * 1000000, // 1MB
maxAssetSize: 2.1 * 1000000
},
resolve: {
alias: {
react: 'preact/compat',
'react-dom/test-utils': 'preact/test-utils',
'react-dom': 'preact/compat',
'react/jsx-runtime': 'preact/jsx-runtime',
'~': path.resolve(__dirname, 'src/app')
},
modules: [path.resolve(__dirname, 'src/app'), 'node_modules'],
extensions: ['.js', '.jsx', '.tsx', '.ts']
},
watchOptions: {
aggregateTimeout: 500,
poll: 1000
},
devServer: {
client: {
logging: 'warn'
},
devMiddleware: {
index: false,
publicPath,
stats: 'errors-only'
},
headers: {
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Methods':
'GET, POST, PUT, DELETE, PATCH, OPTIONS',
'Access-Control-Allow-Headers':
'X-Requested-With, content-type, Authorization'
},
historyApiFallback: {index: `${publicPath}/index.html`},
hot: true,
liveReload: true,
open: true,
port: devServerPort,
// fonts are always loaded with CORS but the CMS doesn't set CORS headers for them
// to get around this, we proxy them instead
proxy: {
context: (p) => p.startsWith('/cms/assets/fonts'),
target: API_ORIGIN,
changeOrigin: true
},
static: {
directory: path.resolve(__dirname, './src'),
publicPath
}
}
};
module.exports = (env, argv) => {
config.mode = argv.mode || 'development';
console.log('Building', config.mode);
if (config.mode === 'production') {
config.output.filename = '[name]-[contenthash].min.js';
config.devtool = 'source-map';
config.optimization.splitChunks.maxInitialRequests = 5;
config.output.chunkFilename = 'chunk-[chunkhash].js';
} else {
config.output.filename = '[name].js';
config.devtool = 'inline-source-map';
config.optimization.splitChunks.maxInitialRequests = 1;
}
return config;
};