-
-
Notifications
You must be signed in to change notification settings - Fork 142
/
webpack.config.js
181 lines (177 loc) · 5.12 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
const path = require('path');
const webpack = require('webpack');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const CspHtmlWebpackPlugin = require('csp-html-webpack-plugin');
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
const { BundleAnalyzerPlugin } = require('webpack-bundle-analyzer');
// Page title is set in HtmlWebpackPlugin(), and `name` in `package.json`
// requires all letters to be lower case
// For consistency with an app that has the first letter of the name
// as upper case, we will normalize the name here
const { name } = require('./package.json');
const title = name.charAt(0).toUpperCase() + name.slice(1);
module.exports = {
target: 'web',
resolve: {
fallback: {
buffer: require.resolve('buffer'),
fs: false,
tls: false,
net: false,
path: false,
zlib: false,
http: false,
https: false,
stream: false,
crypto: false,
},
},
entry: ['./src/index.js'],
output: {
path: path.resolve(__dirname, 'dist'),
filename: 'bundle.js',
publicPath: '/',
},
module: {
rules: [
{
test: /\.(ts|js|mjs)x?$/,
include: [path.resolve(__dirname, 'src')],
use: {
loader: 'babel-loader',
options: {
presets: [
'@babel/preset-env',
'@babel/preset-react',
'@babel/typescript',
],
},
},
resolve: {
extensions: ['.ts', '.tsx', '.js', '.jsx', '.mjs', '.json'],
},
},
{
test: /\.scss$/,
include: [path.resolve(__dirname, 'src')],
use: [MiniCssExtractPlugin.loader, 'css-loader', 'sass-loader'],
resolve: {
extensions: ['.scss'],
},
},
{
test: /\.css$/,
include: [
path.resolve(__dirname, 'src'),
path.resolve(__dirname, 'node_modules'),
],
use: [MiniCssExtractPlugin.loader, 'css-loader'],
resolve: {
extensions: ['.css'],
},
},
{
test: /\.(eot|woff|woff2|ttf|svg|png|jpg|gif|avif)$/,
use: 'url-loader',
},
],
},
plugins: [
new MiniCssExtractPlugin({
// Potentially adding ?%%NONCE%% for replacement
filename: 'main.css',
}),
new HtmlWebpackPlugin({
template: './index-csp.html',
filename: 'index.html',
title: title,
cspPlugin: {
enabled: true,
policy: {
'base-uri': "'self'",
'object-src': "'none'",
'script-src': ["'self'"],
'style-src': ["'unsafe-inline'", "'self'", "'unsafe-eval'"],
},
hashEnabled: {
'script-src': false,
'style-src': false,
},
nonceEnabled: {
'script-src': true,
'style-src': false, // * Disabled for dynamic styling from Material UI
},
},
/** templateParameters:
*Using templateParameters
* https://webpack.js.org/guides/csp/#enabling-csp
* https://github.com/jantimon/html-webpack-plugin?tab=readme-ov-file#options
*/
// Injects nonce value into template parameter, which can be accessed in HTML template
templateParameters: (compilation, assets, assetTags, options) => {
return {
compilation,
webpackConfig: compilation.options,
htmlWebpackPlugin: {
tags: assetTags,
files: assets,
options,
},
nonce: CspHtmlWebpackPlugin.nonce,
};
},
}),
new CspHtmlWebpackPlugin({
'base-uri': ["'self'"],
'default-src': [
"'self'",
'http://localhost:3000',
'ws://localhost:3000',
'https://api.github.com',
"'unsafe-inline'",
"'unsafe-eval'",
'*',
'blob:',
'data:',
'gap:',
],
'img-src': ["'self'", 'data:', 'https://avatars.githubusercontent.com/'],
'child-src': ["'none'"],
'object-src': ["'none'"],
'script-src': [
"'self'",
"'unsafe-eval'",
(_, nonce) => `'nonce-${nonce}'`,
],
'style-src': [
"'unsafe-inline'",
"'self'",
"'unsafe-eval'",
(_, nonce) => `'nonce-${nonce}'`,
],
}),
// options here: https://github.com/webpack-contrib/webpack-bundle-analyzer
// set to true to display bundle breakdown
new BundleAnalyzerPlugin({
openAnalyzer: false,
analyzerMode: 'static',
}),
new webpack.ProvidePlugin({
process: 'node:buffer',
Buffer: ['buffer', 'Buffer'],
}),
new webpack.NormalModuleReplacementPlugin(/node:/, (resource) => {
const mod = resource.request.replace(/^node:/, '');
switch (mod) {
case 'buffer':
resource.request = 'buffer';
break;
case 'stream':
resource.request = 'readable-stream';
break;
default:
throw new Error(`Not found ${mod}`);
}
}),
],
};