-
Notifications
You must be signed in to change notification settings - Fork 531
/
plugins.js
220 lines (205 loc) · 8.66 KB
/
plugins.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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
const fs = require('fs');
const path = require('path');
const webpack = require('webpack');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const CopyWebpackPlugin = require('copy-webpack-plugin');
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
const FaviconsWebpackPlugin = require('favicons-webpack-plugin');
const ReactRefreshWebpackPlugin = require('@pmmmwh/react-refresh-webpack-plugin');
const ESLintPlugin = require('eslint-webpack-plugin');
const ForkTsCheckerWebpackPlugin = require('fork-ts-checker-webpack-plugin');
const { SubresourceIntegrityPlugin } = require('webpack-subresource-integrity');
const { RetryChunkLoadPlugin } = require('webpack-retry-chunk-load-plugin');
const SentryCliPlugin = require('@sentry/webpack-plugin');
const PostCssLogicalWebpackPlugin = require('./postcss-logical-webpack-plugin').default;
const WriteWebpackPlugin = require('./write-webpack-plugin').default;
const HtmlEditWebpackPlugin = require('./html-edit-webpack-plugin').default;
const defaultFaviconConfig = require('./favicon.config');
const faviconConfig = require(path.resolve('./favicon.config.js'));
const { getIndexChunks } = require('../webpack/entries');
module.exports = ({
isProduction,
isRelease,
publicPath,
appMode,
buildData,
featureFlags,
writeSRI,
warningLogs,
errorLogs,
logical,
cssName,
}) => {
return [
...(isProduction
? []
: [
new ReactRefreshWebpackPlugin({
overlay: false,
}),
(warningLogs || errorLogs) &&
new ESLintPlugin({
extensions: ['js', 'ts', 'tsx'],
eslintPath: require.resolve('eslint'),
context: path.resolve('.'),
emitWarning: warningLogs,
emitError: errorLogs,
// ESLint class options
resolvePluginsRelativeTo: __dirname,
cwd: path.resolve('.'),
cache: true,
}),
(warningLogs || errorLogs) &&
new ForkTsCheckerWebpackPlugin({
async: true,
formatter: 'basic',
issue: {
include: (issue) => {
if (warningLogs && issue.severity === 'warning') {
return true;
}
if (errorLogs && issue.severity === 'error') {
return true;
}
return false;
},
},
}),
]),
/*
* Sentry webpack plugin is only run on tag creation (IS_RELEASE_BUNDLE)
* Needed values for source-maps upload
* project: defined in sentry.properties of each app
* org: defined in SENTRY_ORG (gitlab env)
* url: defined in SENTRY_URL (gitlab env)
* authToken: defined in SENTRY_AUTH_TOKEN (gitlab env)
* */
isRelease &&
new SentryCliPlugin({
include: './dist',
ignore: ['node_modules', 'webpack.config.js'],
configFile: 'sentry.properties',
// This prevents build to fail if any issue happened
errorHandler: (err, invokeErr, compilation) => {
compilation.warnings.push('Sentry CLI Plugin: ' + err.message);
},
release: buildData.version,
}),
new CopyWebpackPlugin({
patterns: [
{
from: `${path.dirname(require.resolve('push.js'))}/serviceWorker.min.js`,
to: 'assets/serviceWorker.min.js',
},
],
}),
new WriteWebpackPlugin([
{
name: 'assets/version.json',
data: Buffer.from(JSON.stringify(buildData, null, 2)),
},
]),
new WriteWebpackPlugin([
{
name: 'assets/host.png',
data: Buffer.from('R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7', 'base64'),
},
]),
new CopyWebpackPlugin({
patterns: [
{ from: 'src/.htaccess' },
// Fix max file limit if the folder does not exist
fs.existsSync('public') && { from: 'public', noErrorOnMissing: true },
].filter(Boolean),
}),
new MiniCssExtractPlugin({
filename: cssName,
chunkFilename: cssName,
}),
new HtmlWebpackPlugin({
template: path.resolve('./src/app.ejs'),
templateParameters: {
appName: faviconConfig.favicons.appName,
title: faviconConfig.favicons.appName,
description: faviconConfig.favicons.appDescription,
url: faviconConfig.url,
locales: faviconConfig.locales,
ogImage: faviconConfig.ogImage,
lang: 'en-US',
},
inject: 'body',
scriptLoading: 'defer',
chunks: getIndexChunks('index'),
minify: isProduction && {
removeComments: true,
collapseWhitespace: true,
removeRedundantAttributes: true,
useShortDoctype: true,
removeEmptyAttributes: true,
removeStyleLinkTypeAttributes: true,
keepClosingSlash: true,
minifyJS: true,
minifyCSS: true,
minifyURLs: true,
},
}),
new FaviconsWebpackPlugin({
logo: path.resolve(faviconConfig.logo),
logoMaskable: path.resolve(faviconConfig.logoMaskable),
cache: path.resolve('./node_modules/.cache'),
favicons: {
version: buildData.version,
...defaultFaviconConfig,
...faviconConfig.favicons,
},
}),
...(writeSRI
? [
new SubresourceIntegrityPlugin(),
new HtmlEditWebpackPlugin((tag) => {
const src = tag.attributes.href || tag.attributes.src;
// Remove the integrity and crossorigin attributes for these files because we don't
// want to validate them since we may override the server response on these assets
// for certain scenarios.
if (/\.(css|png|svg|ico|json)(?:\?.+)?$/.test(src)) {
if (tag.attributes.integrity || tag.attributes.crossorigin) {
delete tag.attributes.integrity;
delete tag.attributes.crossorigin;
}
}
return tag;
}),
]
: []),
new HtmlEditWebpackPlugin((tag) => {
// Remove the favicon.ico tag that the FaviconsWebpackPlugin generates because:
// 1) We want it to be listed before the .svg icon that we manually inject
// 2) We want it to have the sizes="any" attribute because of this chrome bug
// https://twitter.com/subzey/status/1417099064949235712
if (tag.tagName === 'link' && tag.attributes.href.endsWith('favicon.ico')) {
return null;
}
// With enabling the loadManifestWithCredentials option in the FaviconsWebpackPlugin the
// crossorigin attribute for the manifest.json link is added correctly, however the SRI
// plugin removes it and replaces it with SRI attributes. This plugin adds back the use-credentials
// crossorigin attribute for the manifest link.
if (tag.tagName === 'link' && tag.attributes.href.endsWith('manifest.webmanifest')) {
tag.attributes.crossorigin = 'use-credentials';
}
return tag;
}),
new RetryChunkLoadPlugin({
cacheBust: `function() {
return Date.now();
}`,
retryDelay: 5000,
maxRetries: 3,
}),
new webpack.DefinePlugin({
WEBPACK_APP_MODE: JSON.stringify(appMode),
WEBPACK_PUBLIC_PATH: JSON.stringify(publicPath),
WEBPACK_FEATURE_FLAGS: JSON.stringify(featureFlags),
}),
logical && new PostCssLogicalWebpackPlugin(),
].filter(Boolean);
};