-
Notifications
You must be signed in to change notification settings - Fork 233
Expand file tree
/
Copy pathrollup.config.js
More file actions
342 lines (336 loc) · 11.9 KB
/
Copy pathrollup.config.js
File metadata and controls
342 lines (336 loc) · 11.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
import typescript from '@rollup/plugin-typescript';
import resolve from '@rollup/plugin-node-resolve';
import commonjs from '@rollup/plugin-commonjs';
import json from '@rollup/plugin-json';
import alias from '@rollup/plugin-alias';
import inject from '@rollup/plugin-inject';
import replace from '@rollup/plugin-replace';
import terser from '@rollup/plugin-terser';
import { readFileSync } from 'fs';
// Read version from package.json
const packageJson = JSON.parse(readFileSync('./package.json', 'utf-8'));
const version = packageJson.version;
// Helper function to create UMD build configuration
function createUmdConfig(minified = false) {
const filename = minified ? 'dist/index.umd.min.js' : 'dist/index.umd.js';
const plugins = [
replace({
__VERSION__: version,
preventAssignment: true
}),
inject({
process: 'process/browser',
Buffer: ['buffer', 'Buffer'],
preventAssignment: false
}),
alias({
entries: [
{ find: 'events', replacement: 'events' },
{ find: 'assert', replacement: 'assert' },
{ find: 'buffer', replacement: 'buffer' },
{ find: 'util', replacement: 'util' }
]
}),
resolve({
preferBuiltins: false,
browser: true
}),
commonjs(),
json(),
typescript({
tsconfig: './tsconfig.json',
sourceMap: true
})
];
// Add terser for minified version (production: drop console to avoid leaking info)
if (minified) {
plugins.push(terser({
compress: {
drop_console: true,
drop_debugger: true,
pure_funcs: ['console.log', 'console.info', 'console.debug']
},
mangle: {
reserved: ['steem'] // Don't mangle the global name
},
format: {
comments: false // Remove comments
}
}));
}
return {
input: 'src/umd.ts',
output: {
file: filename,
format: 'umd',
name: 'steem',
sourcemap: true,
exports: 'default',
banner: `(function() {
// Provide minimal polyfills for browser
var g = typeof globalThis !== 'undefined' ? globalThis : typeof window !== 'undefined' ? window : typeof global !== 'undefined' ? global : typeof self !== 'undefined' ? self : {};
// Process polyfill
if (typeof g.process === 'undefined') {
g.process = {
browser: true,
env: {},
version: '',
versions: {},
nextTick: function(fn) { setTimeout(fn, 0); },
exit: function() {},
cwd: function() { return '/'; },
platform: 'browser'
};
if (typeof globalThis !== 'undefined') {
globalThis.process = g.process;
}
}
// Buffer will be set in outro after all modules are loaded
})();`,
globals: {}
},
plugins,
external: [],
onwarn(warning, warn) {
if (warning.code === 'EVAL' && warning.id?.includes('bluebird')) {
return;
}
// Filter out "Could not resolve" warnings for @noble packages (they are resolved by commonjs plugin)
if (warning.message?.includes('Could not resolve import') && warning.message?.includes('@noble/')) {
return;
}
// Filter out "Unresolved dependencies" warnings for @noble packages (they are bundled by commonjs plugin)
if (warning.code === 'UNRESOLVED_IMPORT') {
const noblePackages = ['@noble/hashes', '@noble/ciphers'];
if (noblePackages.some(pkg => warning.source?.includes(pkg) || (warning.message && warning.message.includes(pkg)))) {
return;
}
// node:undici is Node built-in; only loaded at runtime when httpsOptions is set (never in browser)
if (warning.message?.includes('node:undici') || warning.id === 'node:undici') {
return;
}
}
// Filter out "Missing global variable names" warnings for @noble packages (they are bundled by commonjs plugin)
if (warning.code === 'MISSING_GLOBAL_NAME' && warning.message?.includes('@noble/')) {
return;
}
// Filter out circular dependency warnings from third-party libraries (not our code issues)
if (warning.code === 'CIRCULAR_DEPENDENCY') {
const thirdPartyLibs = ['readable-stream', 'assert'];
if (thirdPartyLibs.some(lib => warning.message?.includes(lib))) {
return;
}
}
warn(warning);
}
};
}
export default [
// Server-side builds (Node.js)
{
input: 'src/index.ts',
output: [
// ES Module for modern bundlers and Node.js
{
file: 'dist/index.js',
format: 'es',
sourcemap: true,
generatedCode: {
constBindings: true
}
},
// CommonJS for Node.js require()
{
file: 'dist/index.cjs',
format: 'cjs',
sourcemap: true
}
],
plugins: [
replace({
__VERSION__: version,
preventAssignment: true
}),
resolve({
preferBuiltins: true,
browser: false
}),
commonjs({
transformMixedEsModules: true,
strictRequires: true,
esmExternals: true
}),
json(),
typescript({
tsconfig: './tsconfig.json',
sourceMap: true
})
],
external: [],
onwarn(warning, warn) {
if (warning.code === 'EVAL' && warning.id?.includes('bluebird')) {
return;
}
// Filter out "Could not resolve" warnings for @noble packages (they are resolved by commonjs plugin)
if (warning.message?.includes('Could not resolve import') && warning.message?.includes('@noble/')) {
return;
}
// Filter out "Unresolved dependencies" warnings for @noble packages (they are bundled by commonjs plugin)
if (warning.code === 'UNRESOLVED_IMPORT') {
const noblePackages = ['@noble/hashes', '@noble/ciphers'];
if (noblePackages.some(pkg => warning.source?.includes(pkg) || (warning.message && warning.message.includes(pkg)))) {
return;
}
// node:undici is Node built-in; only loaded at runtime when httpsOptions is set
if (warning.message?.includes('node:undici') || warning.id === 'node:undici') {
return;
}
}
// Filter out circular dependency warnings from third-party libraries (not our code issues)
if (warning.code === 'CIRCULAR_DEPENDENCY') {
const thirdPartyLibs = ['readable-stream', 'assert'];
if (thirdPartyLibs.some(lib => warning.message?.includes(lib))) {
return;
}
}
// Filter out "Missing global variable names" warnings for @noble packages (they are bundled, not external)
if (warning.code === 'MISSING_GLOBAL_NAME' && warning.message?.includes('@noble/')) {
return;
}
warn(warning);
}
},
// Browser ES Module build (for modern bundlers like Vite, Webpack 5)
{
input: 'src/index.ts',
output: {
file: 'dist/browser.esm.js',
format: 'es',
sourcemap: true,
generatedCode: {
constBindings: true
},
banner: `// Process polyfill and CommonJS exports for browser
(function() {
var g = typeof globalThis !== 'undefined' ? globalThis : typeof window !== 'undefined' ? window : typeof global !== 'undefined' ? global : typeof self !== 'undefined' ? self : {};
var proc = { browser: true, env: {}, version: '', versions: {}, nextTick: function(fn) { setTimeout(fn, 0); }, exit: function() {}, cwd: function() { return '/'; }, platform: 'browser' };
g.process = proc;
// Define process on globalThis for ES modules
if (typeof globalThis !== 'undefined') {
try {
Object.defineProperty(globalThis, 'process', { value: proc, writable: true, configurable: true });
} catch(e) {
globalThis.process = proc;
}
}
// Define exports for util module that uses top-level exports.*
// This is a fallback for modules that Rollup's commonjs plugin doesn't fully transform
if (typeof exports === 'undefined') {
try {
Object.defineProperty(g, 'exports', { value: {}, writable: true, configurable: true });
} catch(e) {
g.exports = {};
}
}
})();
// Define utilExports in global scope for replace plugin (exports.format -> utilExports.format)
var utilExports = typeof globalThis !== 'undefined' ? (globalThis.utilExports || (globalThis.utilExports = {})) : (typeof window !== 'undefined' ? (window.utilExports || (window.utilExports = {})) : {});
// Buffer will be set in src/index.ts after importing buffer module
// The inject plugin will replace Buffer references with buffer.Buffer in the code
`
},
plugins: [
replace({
__VERSION__: version,
preventAssignment: true
}),
replace({
// Replace require('crypto') calls in browser builds to prevent Vite from externalizing crypto
// This ensures the code path is never executed in browsers
preventAssignment: true,
delimiters: ['', ''],
values: {
"typeof require !== 'undefined' ? require('crypto') : null": 'null',
'typeof require !== "undefined" ? require("crypto") : null': 'null',
"require('crypto')": 'null',
'require("crypto")': 'null'
}
}),
replace({
// Fix util module exports that are not properly transformed by commonjs plugin
// This replaces top-level exports.* references that appear outside of IIFE wrappers
preventAssignment: false,
delimiters: ['\\b', '\\b'],
values: {
'exports.format': 'utilExports.format',
'exports.inspect': 'utilExports.inspect'
}
}),
inject({
process: 'process/browser',
Buffer: ['buffer', 'Buffer'],
preventAssignment: false
}),
alias({
entries: [
{ find: 'events', replacement: 'events' },
{ find: 'assert', replacement: 'assert' },
{ find: 'buffer', replacement: 'buffer' },
{ find: 'util', replacement: 'util' }
]
}),
resolve({
preferBuiltins: false,
browser: true,
}),
commonjs({
transformMixedEsModules: true,
strictRequires: true,
requireReturnsDefault: 'auto',
defaultIsModuleExports: 'auto',
esmExternals: true
}),
json(),
typescript({
tsconfig: './tsconfig.json',
sourceMap: true
})
],
external: [], // Bundle everything for browser (crypto require is replaced with null)
onwarn(warning, warn) {
if (warning.code === 'EVAL' && warning.id?.includes('bluebird')) {
return;
}
// Filter out "Could not resolve" warnings for @noble packages (they are resolved by commonjs plugin)
if (warning.message?.includes('Could not resolve import') && warning.message?.includes('@noble/')) {
return;
}
// Filter out "Unresolved dependencies" warnings for @noble packages (they are bundled by commonjs plugin)
if (warning.code === 'UNRESOLVED_IMPORT') {
const noblePackages = ['@noble/hashes', '@noble/ciphers'];
if (noblePackages.some(pkg => warning.source?.includes(pkg) || (warning.message && warning.message.includes(pkg)))) {
return;
}
// node:undici is Node built-in; only loaded at runtime when httpsOptions is set (never in browser)
if (warning.message?.includes('node:undici') || warning.id === 'node:undici') {
return;
}
}
// Filter out "Missing global variable names" warnings for @noble packages (they are bundled by commonjs plugin)
if (warning.code === 'MISSING_GLOBAL_NAME' && warning.message?.includes('@noble/')) {
return;
}
// Filter out circular dependency warnings from third-party libraries (not our code issues)
if (warning.code === 'CIRCULAR_DEPENDENCY') {
const thirdPartyLibs = ['readable-stream', 'assert'];
if (thirdPartyLibs.some(lib => warning.message?.includes(lib))) {
return;
}
}
warn(warning);
}
},
// Browser builds (UMD)
createUmdConfig(false), // Regular UMD build
createUmdConfig(true) // Minified UMD build
];