-
-
Notifications
You must be signed in to change notification settings - Fork 87
Expand file tree
/
Copy pathrollup.config.node.mjs
More file actions
282 lines (263 loc) · 9.79 KB
/
rollup.config.node.mjs
File metadata and controls
282 lines (263 loc) · 9.79 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
import fs from 'fs';
import path from 'path';
import terser from '@rollup/plugin-terser';
import resolve from '@rollup/plugin-node-resolve';
import commonjs from '@rollup/plugin-commonjs';
import json from '@rollup/plugin-json';
import replace from '@rollup/plugin-replace';
import copy from 'rollup-plugin-copy';
import { getBabelOutputPlugin } from '@rollup/plugin-babel';
import gcPlugin from './scripts/rollup-gc-plugin.mjs';
// import config Babel via import ESM
import babelConfig from './babel.config.json' with { type: 'json' };
import babelNodeOutput from './babel.node-output.json' with { type: 'json' };
// determines environment
const baseResolveOpts = { browser: false, exportConditions: ['node', 'svelte'], preferBuiltins: true };
// common plugins
const replaceOpts = {
preventAssignment: false,
delimiters: ['', ''],
values: {
'__BUILD_MODE__': JSON.stringify(process.env.NODE_ENV || 'development'),
'fs/promises")': 'fs").promises',
"fs/promises')": "fs').promises",
'getFilename()': '__filename',
'getDirname()': '__dirname'
}
}
// Check if this is a production build
const isProduction = process.env.NODE_ENV === 'production' || process.argv.includes('--production')
// Babel config import
// bundles main, preload e workers
const baseBabelOpts = babelConfig;
const nodeBabelOpts = babelNodeOutput;
const watchOpts = { buildDelay: 3000, exclude: 'node_modules/**' };
const external = [
'electron',
/.+\.(node|native)$/,
/premium\./,
'bytenode' // Must be external - modifies Node.js module system at runtime
];
const outputs = [];
// Plugin to resolve node:sqlite to a mock module (prevents external dependency warning)
function sqliteResolvePlugin() {
return {
name: 'sqlite-resolve',
resolveId(source) {
// Intercept node:sqlite and return virtual module ID
if (source === 'node:sqlite') {
return '\0node:sqlite'; // Virtual module ID (null-byte prefix)
}
return null;
},
load(id) {
// Return mock module when virtual ID is loaded
// This replaces require('node:sqlite') with a mock implementation
if (id === '\0node:sqlite') {
return `
// Mock module for node:sqlite (not available on Android)
class DatabaseSync {
constructor() {}
close() {}
prepare() {
return {
run: () => {},
get: () => null,
all: () => []
};
}
}
class StatementSync {}
function openSync() {
throw new Error('SQLite not available on Android');
}
export { DatabaseSync, StatementSync, openSync };
export default { DatabaseSync, StatementSync, openSync };
`;
}
return null;
}
};
}
// helper for Node.js bundles
function makeNodeBundle({ input, output, babelOpts, extraPlugins = [], externals = null, isLargeFile = false, isMainProcess = false }) {
// Specific settings for main process
const mainProcessConfig = isMainProcess ? {
maxWorkers: 1, // Reduce workers to save memory
keep_classnames: false, // Disable to save memory
keep_fnames: false, // Disable to save memory
compress: {
drop_console: isProduction, // Remove console logs in production
drop_debugger: true,
passes: 1,
unsafe: true, // More aggressive optimizations
unsafe_comps: true,
unsafe_math: true
}
} : {
maxWorkers: 2,
keep_classnames: true,
keep_fnames: true,
compress: {
drop_console: isProduction, // Keep console logs in development
drop_debugger: false,
passes: 1
}
};
const plugins = [
sqliteResolvePlugin(), // Resolve node:sqlite to mock module (must be before resolve)
resolve({
...baseResolveOpts,
// Specific settings for main process
...(isMainProcess ? { dedupe: [] } : {})
}),
commonjs({
sourcemap: isMainProcess ? false : true, // Disable sourcemap for main process
dynamicRequireTargets: [
'node_modules/node-ssdp/lib/server.js',
'node_modules/*/lib/server.js',
'node_modules/**/lib/server.js',
'www/nodejs/modules/premium/modules/cast/node_modules/node-ssdp/lib/server.js',
'www/nodejs/modules/premium/modules/cast/node_modules/node-ssdp/lib/client.js',
'www/nodejs/modules/premium/modules/cast/node_modules/node-ssdp/lib/index.js'
],
ignoreDynamicRequires: true,
...(isMainProcess ? { requireReturnsDefault: 'auto' } : {})
}),
json({ compact: true }),
getBabelOutputPlugin({
...babelOpts,
// Memory optimizations for main process
...(isMainProcess ? { compact: true, minified: true } : {})
}),
replace(replaceOpts),
...extraPlugins,
// Enable terser for production builds with BigInt-safe settings
...(isProduction && !isLargeFile ? [terser({
ecma: 2020, // Use ECMAScript 2020 for better BigInt compatibility
maxWorkers: 0, // Disable workers to prevent hanging
keep_classnames: true, // Keep class names to avoid BigInt issues
keep_fnames: true, // Keep function names to avoid BigInt issues
output: { comments: false },
compress: {
drop_console: isProduction, // Remove console logs in production
drop_debugger: true,
passes: 1, // Reduce passes to avoid BigInt issues
// ✅ ONLY essential disables for BigInt
reduce_vars: false, // ← Main: avoids rewriting vars with BigInt
evaluate: false, // ← Main: avoids evaluating BigInt expressions
// Keep other optimizations
unsafe: false,
unsafe_comps: false,
unsafe_math: false,
unsafe_proto: false,
unsafe_regexp: false
}
})] : []),
gcPlugin() // Add GC plugin at the end to run after bundle is written
];
outputs.push({
input,
output,
plugins,
external: Array.isArray(externals) ? externals : external,
watch: watchOpts,
maxParallelFileOps: isLargeFile || isMainProcess ? 1 : undefined, // Force 1 for main process
treeshake: isLargeFile ? false : (isMainProcess ? {
// Tree shaking enabled for main.js with conservative settings
moduleSideEffects: (id) => {
// Preserve modules that may have important side effects
if (id.includes('analytics.js') ||
id.includes('crashlog.js') ||
id.includes('node-cleanup') ||
id.includes('onexit')) {
return true; // Preserve side effects
}
// For own modules, assume they may have side effects (conservative)
if (id.includes('www/nodejs/modules/') && !id.includes('node_modules')) {
return true; // Preserve own modules for safety
}
// For node_modules, try tree shaking (safer)
return false; // Allow tree shaking in node_modules
},
propertyReadSideEffects: false, // Properties can be tree-shaken
tryCatchDeoptimization: false // Don't deoptimize try-catch
} : undefined),
// Specific settings for main process
...(isMainProcess ? {
preserveEntrySignatures: 'allow-extension',
onwarn(warning, warn) {
// Suppress circular dependency warnings for main process
if (warning.code === 'CIRCULAR_DEPENDENCY') return;
warn(warning);
}
} : {})
});
}
// Node.js bundles only
makeNodeBundle({
input: 'www/nodejs/main.mjs',
output: {
format: 'cjs',
file: 'www/nodejs/dist/main.js',
inlineDynamicImports: true,
sourcemap: false,
// Polyfill Web APIs for Node.js < 20
banner: `
// Polyfill Web APIs for undici (node-fetch wrapper)
if (typeof globalThis.File === 'undefined' || typeof globalThis.Blob === 'undefined') {
try {
const { File: FileClass, Blob: BlobClass } = require('buffer');
if (typeof globalThis.File === 'undefined') globalThis.File = FileClass;
if (typeof globalThis.Blob === 'undefined') globalThis.Blob = BlobClass;
} catch (e) {
console.warn('Failed to apply Web API polyfills:', e.message);
}
}
`.trim()
}, // Disable sourcemap for main process
babelOpts: nodeBabelOpts,
isMainProcess: true, // Enable main-process specific optimizations
extraPlugins: [
copy({ targets: [
{ src: 'node_modules/dayjs/locale/*.js', dest: 'www/nodejs/dist/dayjs-locale' },
{ src: 'node_modules/create-desktop-shortcuts/src/windows.vbs', dest: 'www/nodejs/dist' },
{ src: 'node_modules/hls.js/dist/hls.min.js', dest: 'www/nodejs/renderer/dist' },
{ src: 'node_modules/mpegts.js/dist/mpegts.js', dest: 'www/nodejs/renderer/dist' },
{ src: 'node_modules/bytenode/**/*', dest: 'www/nodejs/dist/node_modules/bytenode' },
{ src: 'node_modules/@edenware/tv-channels-by-country/channels/*.json', dest: 'www/nodejs/channels' },
] })
]
});
makeNodeBundle({
input: 'www/nodejs/electron.mjs',
output: { format: 'cjs', file: 'www/nodejs/dist/electron.js', inlineDynamicImports: true, sourcemap: true },
babelOpts: nodeBabelOpts,
externals: ['electron', /.+\.(node|native)$/]
});
[
'preload.mjs',
'modules/lists/updater-worker.js',
'modules/epg/worker/EPGManager.js',
'modules/streamer/utils/mpegts-processor-worker.js',
'modules/multi-worker/worker.mjs'
].forEach(file => {
const outputFile = path.basename(file).replace('.mjs','.js');
const isLargeFile = file.includes('EPGManager.js');
makeNodeBundle({
input: `www/nodejs/${file}`,
output: { format: 'cjs', file: `www/nodejs/dist/${outputFile}`, inlineDynamicImports: true, sourcemap: true },
babelOpts: nodeBabelOpts,
externals: ['electron', /.+\.(node|native)$/],
isLargeFile
});
});
if (fs.existsSync('www/nodejs/modules/premium/premium.js')) {
makeNodeBundle({
input: 'www/nodejs/modules/premium/premium.js',
output: { format: 'cjs', file: 'www/nodejs/dist/premium.js', inlineDynamicImports: true, sourcemap: true },
babelOpts: nodeBabelOpts,
externals: ['electron', /.+\.(node|native)$/]
});
}
export default outputs;