-
Notifications
You must be signed in to change notification settings - Fork 63
/
Copy pathwebpack.config.ts
276 lines (231 loc) · 7.96 KB
/
webpack.config.ts
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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
/*
Reference for this config:
https://github.com/facebook/create-react-app/blob/main/packages/react-scripts/config/webpack.config.js
*/
import SaveRemoteFilePlugin from '@temple-wallet/save-remote-file-webpack-plugin';
import { CleanWebpackPlugin } from 'clean-webpack-plugin';
import CopyWebpackPlugin from 'copy-webpack-plugin';
import CreateFileWebpack from 'create-file-webpack';
import CssMinimizerPlugin from 'css-minimizer-webpack-plugin';
import HtmlWebpackPlugin from 'html-webpack-plugin';
import MiniCssExtractPlugin from 'mini-css-extract-plugin';
import * as Path from 'path';
import WebPack from 'webpack';
import ExtensionReloaderMV3BadlyTyped, {
ExtensionReloader as ExtensionReloaderMV3Type
} from 'webpack-ext-reloader-mv3';
import WebExtensionTargetPlugin from 'webpack-target-webextension';
import WebpackBar from 'webpackbar';
import { buildBaseConfig } from './webpack/base.config';
import {
DEVELOPMENT_ENV,
PRODUCTION_ENV,
TARGET_BROWSER,
MANIFEST_VERSION,
BACKGROUND_IS_WORKER,
RELOADER_PORTS,
MAX_JS_CHUNK_SIZE_IN_BYTES
} from './webpack/env';
import usePagesLiveReload from './webpack/live-reload';
import { buildManifest } from './webpack/manifest';
import { PATHS, IFRAMES } from './webpack/paths';
import { isTruthy } from './webpack/utils';
const ExtensionReloaderMV3 = ExtensionReloaderMV3BadlyTyped as ExtensionReloaderMV3Type;
const PAGES_NAMES = ['popup', 'fullpage', 'confirm', 'options'];
const HTML_TEMPLATES = PAGES_NAMES.map(name => {
const filename = `${name}.html`;
const path = Path.join(PATHS.PUBLIC, filename);
return { name, filename, path };
}).concat(
Object.keys(IFRAMES).map(name => {
const filename = `${name}.html`;
const path = Path.join(PATHS.PUBLIC, `iframes/${filename}`);
return { name, filename: `iframes/${filename}`, path };
})
);
const IS_CORE_BUILD = process.env.CORE_BUILD === 'true';
const CONTENT_SCRIPTS = ['contentScript', !IS_CORE_BUILD && 'replaceAds'].filter(isTruthy);
if (BACKGROUND_IS_WORKER) CONTENT_SCRIPTS.push('keepBackgroundWorkerAlive');
const mainConfig = (() => {
const config = buildBaseConfig();
/* Page reloading in development mode */
const liveReload = DEVELOPMENT_ENV && usePagesLiveReload(RELOADER_PORTS.PAGES);
config.entry = {
...Object.fromEntries(PAGES_NAMES.map(name => [name, Path.join(PATHS.SOURCE, `${name}.tsx`)])),
...IFRAMES
};
if (liveReload) config.entry.live_reload = liveReload.client_entry;
config.output = {
...config.output,
filename: 'pages/[name].js',
chunkFilename: 'pages/[id].bundle-chunk.js'
};
config.plugins!.push(
...[
new WebpackBar({
name: 'Temple Wallet | Main',
color: '#ed8936'
}),
new CleanWebpackPlugin({
cleanOnceBeforeBuildPatterns: [`**/*`, `!scripts/**`, `!background/**`, PATHS.OUTPUT_PACKED],
cleanStaleWebpackAssets: false,
verbose: false
}),
new MiniCssExtractPlugin({
filename: 'styles/[name].css',
chunkFilename: 'styles/[name].chunk.css'
}),
...HTML_TEMPLATES.map(
({ name, filename, path }) =>
new HtmlWebpackPlugin({
template: path,
filename,
chunks: liveReload ? [name, 'live_reload'] : [name],
inject: 'body',
...(PRODUCTION_ENV
? {
minify: {
removeComments: true,
collapseWhitespace: true,
removeRedundantAttributes: true,
useShortDoctype: true,
removeEmptyAttributes: true,
removeStyleLinkTypeAttributes: true,
keepClosingSlash: true,
minifyJS: true,
minifyCSS: true,
minifyURLs: true
}
}
: {})
})
),
new CopyWebpackPlugin({
patterns: [
{
from: PATHS.PUBLIC,
to: PATHS.OUTPUT,
globOptions: {
/*
- HTML files are taken care of by the `html-webpack-plugin`. Copying them here leads to:
`ERROR in Conflict: Multiple assets emit different content to the same filename [name].html`
- Manifest file is copied next, along with transformation of it.
*/
ignore: ['**/*.html']
}
},
/*
Using `asset/resource` rule type with `webworker` target isn't working.
See: https://github.com/vercel/next.js/issues/22581
*/
{ from: PATHS.LIBTHEMIS_WASM_FILE, to: PATHS.OUTPUT_WASM }
]
}),
new SaveRemoteFilePlugin([
{ url: 'https://api.hypelab.com/v1/scripts/hp-sdk.js?v=0', filepath: 'scripts/hypelab.embed.js', hash: false }
]),
new CreateFileWebpack({
path: PATHS.OUTPUT,
fileName: 'manifest.json',
content: (() => {
const manifest = buildManifest(TARGET_BROWSER);
return JSON.stringify(manifest, null, 2);
})()
}),
...(liveReload ? liveReload.plugins : [])
].filter(isTruthy)
);
config.optimization!.splitChunks = {
chunks: 'all',
maxSize: MAX_JS_CHUNK_SIZE_IN_BYTES,
enforceSizeThreshold: MAX_JS_CHUNK_SIZE_IN_BYTES,
name: (_: any, chunks: any) => chunks.map((chunk: any) => chunk.name).join('-') + '.split-chunk'
};
config.optimization!.minimizer!.push(
// This is only used in production mode
new CssMinimizerPlugin()
);
return config;
})();
const scriptsConfig = (() => {
const config = buildBaseConfig();
// Required for dynamic imports `import()`
config.output!.chunkFormat = 'module';
config.entry = {
contentScript: Path.join(PATHS.SOURCE, 'contentScript.ts')
};
if (!IS_CORE_BUILD) {
config.entry.replaceAds = Path.join(PATHS.SOURCE, 'replaceAds.ts');
}
if (BACKGROUND_IS_WORKER)
config.entry.keepBackgroundWorkerAlive = Path.join(PATHS.SOURCE, 'keepBackgroundWorkerAlive.ts');
config.output = {
...config.output,
filename: 'scripts/[name].js',
chunkFilename: 'scripts/[name].chunk.js'
};
config.plugins!.push(
...[
new WebpackBar({
name: 'Temple Wallet | Scripts',
color: '#ed8936'
}),
new CleanWebpackPlugin({
cleanOnceBeforeBuildPatterns: ['scripts/**'],
cleanStaleWebpackAssets: false,
verbose: false
})
].filter(isTruthy)
);
return config;
})();
const backgroundConfig = (() => {
const config = buildBaseConfig();
if (BACKGROUND_IS_WORKER) config.target = 'webworker';
config.entry = {
background: Path.join(PATHS.SOURCE, 'background.ts')
};
config.output = {
...config.output,
filename: 'background/index.js',
chunkFilename: 'background/[name].chunk.js'
};
config.plugins!.push(
...[
new WebpackBar({
name: 'Temple Wallet | Background',
color: '#ed8936'
}),
new WebPack.NormalModuleReplacementPlugin(/^react$/, () => {
throw new Error('React is not allowed in BG script');
}),
new CleanWebpackPlugin({
cleanOnceBeforeBuildPatterns: ['background/**'],
cleanStaleWebpackAssets: false,
verbose: false
}),
/*
Handling dynamic imports, converted to ServiceWorker's `importScripts`, and failing
over being called deferred, not at the root of the script.
*/
BACKGROUND_IS_WORKER &&
new WebExtensionTargetPlugin({
background: {
entry: 'background',
manifest: MANIFEST_VERSION
}
}),
/* Page reloading in development mode */
DEVELOPMENT_ENV &&
new ExtensionReloaderMV3({
port: RELOADER_PORTS.BACKGROUND,
reloadPage: true,
entries: { background: 'background', contentScript: [] }
})
].filter(isTruthy)
);
return config;
})();
const configurations = [mainConfig, scriptsConfig, backgroundConfig];
export default configurations;
export const parallelism = 3;