forked from alyssaxuu/screenity
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwebpack.config.js
More file actions
executable file
·374 lines (349 loc) · 10.9 KB
/
Copy pathwebpack.config.js
File metadata and controls
executable file
·374 lines (349 loc) · 10.9 KB
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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
const webpack = require("webpack");
const path = require("path");
const fileSystem = require("fs-extra");
const env = require("./utils/env");
const CopyWebpackPlugin = require("copy-webpack-plugin");
const HtmlWebpackPlugin = require("html-webpack-plugin");
const TerserPlugin = require("terser-webpack-plugin");
const isDev = env.NODE_ENV === "development";
// SCREENITY_BS_BUILD=1: slim build for BrowserStack inline-CRX size limit.
// drops post-recording UI + WASM. recording + camera paths preserved.
const isBsBuild = process.env.SCREENITY_BS_BUILD === "1";
const BS_DROPPED_ENTRIES = new Set([
"cloudrecorder",
"download",
"waveform",
"setup",
"playground",
]);
const ASSET_PATH = process.env.ASSET_PATH || "/";
if (process.env.SCREENITY_SKIP_ENV) {
// open-source release build, no dotenv
} else if (isDev || process.env.SCREENITY_USE_LOCAL_ENV === "1") {
// SCREENITY_USE_LOCAL_ENV=1 lets you do a NODE_ENV=production
// (minified, fast) build that still points at localhost; useful
// for testing login/auth flows against a local dev server while
// keeping the small prod-style bundle.
require("dotenv").config({ path: ".env.local" });
} else {
require("dotenv").config({ path: ".env.production" });
}
// Entry points for the different pages
const entryPoints = {
background: path.join(__dirname, "src", "pages", "Background", "index.js"),
contentScript: path.join(__dirname, "src", "pages", "Content", "index.jsx"),
recorder: path.join(__dirname, "src", "pages", "Recorder", "index.jsx"),
recorderkeepalive: path.join(
__dirname,
"src",
"pages",
"Recorder",
"recorderKeepalive.js"
),
cloudrecorder: path.join(
__dirname,
"src",
"pages",
"CloudRecorder",
"index.jsx"
),
offscreenrecorder: path.join(
__dirname,
"src",
"pages",
"OffscreenRecorder",
"index.jsx"
),
camera: path.join(__dirname, "src", "pages", "Camera", "index.jsx"),
waveform: path.join(__dirname, "src", "pages", "Waveform", "index.jsx"),
permissions: path.join(__dirname, "src", "pages", "Permissions", "index.jsx"),
setup: path.join(__dirname, "src", "pages", "Setup", "index.jsx"),
playground: path.join(__dirname, "src", "pages", "Playground", "index.jsx"),
region: path.join(__dirname, "src", "pages", "Region", "index.jsx"),
download: path.join(__dirname, "src", "pages", "Download", "index.jsx"),
editor: path.join(__dirname, "src", "pages", "Editor", "index.jsx"),
remuxoffscreen: path.join(
__dirname,
"src",
"pages",
"RemuxOffscreen",
"index.js"
),
remuxworker: path.join(
__dirname,
"src",
"pages",
"RemuxOffscreen",
"worker.js"
),
recorderopfsworker: path.join(
__dirname,
"src",
"pages",
"Recorder",
"recorderStorage",
"opfs",
"writerWorker.js"
),
};
if (isBsBuild) {
for (const k of Object.keys(entryPoints)) {
if (BS_DROPPED_ENTRIES.has(k)) delete entryPoints[k];
}
console.log(
`[webpack] SCREENITY_BS_BUILD: dropped entries [${[...BS_DROPPED_ENTRIES].join(",")}]`,
);
}
const htmlPlugins = Object.keys(entryPoints)
.map((entryName) => {
// Skip background script and worker bundles; they have no HTML page.
if (
entryName === "background" ||
entryName === "contentScript" ||
entryName === "remuxworker" ||
entryName === "recorderopfsworker" ||
entryName === "recorderkeepalive"
) {
return null;
}
// Map entry names to folder names (for multi-word entries)
const folderNameMap = {
cloudrecorder: "CloudRecorder",
offscreenrecorder: "OffscreenRecorder",
remuxoffscreen: "RemuxOffscreen",
};
const folderName =
folderNameMap[entryName] ||
entryName.charAt(0).toUpperCase() + entryName.slice(1);
const templatePath = path.join(
__dirname,
"src",
"pages",
folderName,
"index.html"
);
// Inject keepalive before the main bundle so audio/locks/mediaSession
// signals are live before heavy parse; otherwise hidden-tab throttling
// drops encoders to ~5fps for the first 15s. Manual sort because auto
// sort flips order based on the chunk graph.
const needsKeepalive =
entryName === "recorder" || entryName === "cloudrecorder";
const chunks = needsKeepalive
? ["recorderkeepalive", entryName]
: [entryName];
const options = {
template: templatePath,
filename: `${entryName}.html`,
chunks,
cache: true,
...(needsKeepalive ? { chunksSortMode: "manual" } : {}),
};
options.favicon = path.join(__dirname, "src", "assets", "favicon.png");
return new HtmlWebpackPlugin(options);
})
.filter(Boolean); // Filter out null values
const fileExtensions = [
"jpg",
"jpeg",
"png",
"gif",
"eot",
"otf",
"svg",
"ttf",
"woff",
"woff2",
];
const secretsPath = path.join(__dirname, `secrets.${env.NODE_ENV}.js`);
const alias = { "react-dom": "@hot-loader/react-dom" };
if (fileSystem.existsSync(secretsPath)) {
alias["secrets"] = secretsPath;
}
const config = {
mode: process.env.NODE_ENV || "production",
performance: { hints: false },
entry: entryPoints,
// Persistent filesystem cache for fast rebuilds
cache: {
type: "filesystem",
buildDependencies: {
config: [__filename],
},
},
output: {
filename: "[name].bundle.js",
// chrome rejects extension files starting with "_". force a "chunk."
// prefix so webpack's default _f608.bundle.js etc. don't trip it.
chunkFilename: "chunk.[name].[contenthash:8].bundle.js",
path: path.resolve(__dirname, "build"),
clean: !isDev, // Only wipe build dir in production; dev keeps it to avoid re-copying 40MB of assets
publicPath: ASSET_PATH,
},
module: {
rules: [
{
test: /\.(css|scss)$/,
use: [
{ loader: "style-loader" },
{ loader: "css-loader" },
{
loader: "sass-loader",
options: { sourceMap: true },
},
],
},
{
test: new RegExp(`.(${fileExtensions.join("|")})$`),
type: "asset/resource",
exclude: /node_modules/,
},
{
test: /\.html$/,
loader: "html-loader",
exclude: /node_modules/,
},
{
test: /\.(ts|tsx)$/,
loader: "ts-loader",
exclude: /node_modules/,
options: {
transpileOnly: isDev,
},
},
{
test: /\.(js|jsx)$/,
use: isDev
? [{ loader: "babel-loader" }]
: [{ loader: "source-map-loader" }, { loader: "babel-loader" }],
exclude: /node_modules/,
},
],
},
resolve: {
alias: {
react: path.resolve("./node_modules/react"),
"react-dom": path.resolve("./node_modules/react-dom"),
"react/jsx-runtime": path.resolve("./node_modules/react/jsx-runtime"),
},
// Code extensions first; image/font extensions are only needed for explicit imports with extensions
extensions: [".js", ".jsx", ".ts", ".tsx", ".css"],
},
plugins: [
new webpack.ProgressPlugin(),
new webpack.DefinePlugin({
"process.env.SCREENITY_APP_BASE": JSON.stringify(
process.env.SCREENITY_APP_BASE
),
"process.env.SCREENITY_WEBSITE_BASE": JSON.stringify(
process.env.SCREENITY_WEBSITE_BASE
),
"process.env.SCREENITY_API_BASE_URL": JSON.stringify(
process.env.SCREENITY_API_BASE_URL
),
"process.env.SCREENITY_ENABLE_CLOUD_FEATURES": JSON.stringify(
process.env.SCREENITY_ENABLE_CLOUD_FEATURES
),
"process.env.MAX_RECORDING_DURATION": JSON.stringify(
process.env.MAX_RECORDING_DURATION || 3600 // Default to 1 hour
),
"process.env.RECORDING_WARNING_THRESHOLD": JSON.stringify(
process.env.RECORDING_WARNING_THRESHOLD || 60 // Default to 1 minute
),
"process.env.SCREENITY_DEV_MODE": JSON.stringify(
process.env.SCREENITY_DEV_MODE || ""
),
}),
// Copy manifest and transform with package info
new CopyWebpackPlugin({
patterns: [
{
from: "src/manifest.json",
to: path.join(__dirname, "build"),
force: true,
transform: (content) => {
const manifest = {
description: process.env.npm_package_description,
version: process.env.npm_package_version,
...JSON.parse(content.toString()),
};
// Strip dev-only origins from prod builds. The
// SCREENITY_USE_LOCAL_ENV escape hatch keeps them in for
// build:local (prod-optimized bundle on localhost APIs).
if (
!isDev &&
process.env.SCREENITY_USE_LOCAL_ENV !== "1" &&
manifest.externally_connectable?.matches
) {
manifest.externally_connectable.matches =
manifest.externally_connectable.matches.filter(
(m) =>
!m.includes("localhost") && !m.includes("127.0.0.1"),
);
}
return Buffer.from(JSON.stringify(manifest));
},
},
{
from: "src/schema.json",
to: path.join(__dirname, "build/schema.json"),
force: true,
},
{
from: "src/assets/",
to: path.join(__dirname, "build/assets"),
force: true,
filter: isBsBuild
? (resourcePath) =>
!/vision_wasm.*\.wasm$/.test(resourcePath) &&
!/\/videos\//.test(resourcePath) &&
!/pin\.gif$/.test(resourcePath)
: undefined,
},
{
from: "src/_locales/",
to: path.join(__dirname, "build/_locales"),
force: true,
},
],
}),
...htmlPlugins,
],
};
if (isDev) {
config.devtool = "cheap-module-source-map";
} else {
config.optimization = {
minimize: true,
minimizer: [
new TerserPlugin({
extractComments: false,
// Parallelize across CPU cores. Default is os.cpus().length-1
// but being explicit makes the intent clear.
parallel: true,
terserOptions: {
ecma: 2020,
compress: {
ecma: 2020,
// Two compress passes catches more dead code than one
// (DCE during pass 1 enables further inlining in pass 2).
// Adds ~10-20% to build time, shaves 3-7% off bundle size.
passes: 2,
// strip log/debug/info in prod, keep warn/error for support.
// Array form catches calls inside callbacks too.
drop_console: ["log", "debug", "info"],
pure_funcs: ["console.log", "console.debug", "console.info"],
},
mangle: {
// Off; would mangle _-prefixed properties only. Measure
// before flipping.
},
format: {
// Drop all comments, including @preserve from deps.
comments: false,
},
},
}),
],
};
}
module.exports = config;