rolldown 0.10.5 → 0.11.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,1507 @@
1
+ const { __commonJSMin, __toESM } = require("./chunk-IY9XHjk1.cjs");
2
+ const { default: nodePath, default: path, default: path$1 } = __toESM(require("node:path"));
3
+ const { z } = __toESM(require("zod"));
4
+ const { Worker } = __toESM(require("node:worker_threads"));
5
+ const { availableParallelism } = __toESM(require("node:os"));
6
+ const { isRegExp } = __toESM(require("node:util/types"));
7
+
8
+ //#region src/utils/async-flatten.ts
9
+ async function asyncFlatten(array) {
10
+ do {
11
+ array = (await Promise.all(array)).flat(Infinity);
12
+ } while (array.some((v) => v?.then));
13
+ return array;
14
+ }
15
+
16
+ //#endregion
17
+ //#region src/utils/asset-source.ts
18
+ function transformAssetSource(bindingAssetSource$1) {
19
+ return bindingAssetSource$1.inner;
20
+ }
21
+ function bindingAssetSource(source) {
22
+ return {inner: source};
23
+ }
24
+
25
+ //#endregion
26
+ //#region src/utils/transform-to-rollup-output.ts
27
+ function transformToRollupOutputChunk(chunk) {
28
+ return {
29
+ type: 'chunk',
30
+ get code() {
31
+ return chunk.code;
32
+ },
33
+ set code(code) {
34
+ chunk.code = code;
35
+ },
36
+ fileName: chunk.fileName,
37
+ name: chunk.name,
38
+ get modules() {
39
+ return Object.fromEntries(Object.entries(chunk.modules).map(([key, _]) => [key, {}]));
40
+ },
41
+ get imports() {
42
+ return chunk.imports;
43
+ },
44
+ set imports(imports) {
45
+ chunk.imports = imports;
46
+ },
47
+ get dynamicImports() {
48
+ return chunk.dynamicImports;
49
+ },
50
+ exports: chunk.exports,
51
+ isEntry: chunk.isEntry,
52
+ facadeModuleId: chunk.facadeModuleId || null,
53
+ isDynamicEntry: chunk.isDynamicEntry,
54
+ get moduleIds() {
55
+ return chunk.moduleIds;
56
+ },
57
+ get map() {
58
+ return chunk.map ? JSON.parse(chunk.map) : null;
59
+ },
60
+ set map(map) {
61
+ chunk.map = JSON.stringify(map);
62
+ },
63
+ sourcemapFileName: chunk.sourcemapFileName || null,
64
+ preliminaryFileName: chunk.preliminaryFileName
65
+ };
66
+ }
67
+ function transformToRollupOutputAsset(asset) {
68
+ return {
69
+ type: 'asset',
70
+ fileName: asset.fileName,
71
+ get source() {
72
+ return transformAssetSource(asset.source);
73
+ },
74
+ set source(source) {
75
+ asset.source = bindingAssetSource(source);
76
+ }
77
+ };
78
+ }
79
+ function transformToRollupOutput(output) {
80
+ const { chunks, assets } = output;
81
+ const [firstChunk, ...restChunks] = chunks;
82
+ return {output: [transformToRollupOutputChunk(firstChunk), ...restChunks.map(transformToRollupOutputChunk), ...assets.map(transformToRollupOutputAsset),]};
83
+ }
84
+ function transformToOutputBundle(output) {
85
+ const bundle = Object.fromEntries(transformToRollupOutput(output).output.map((item) => [item.fileName, item]));
86
+ return new Proxy(bundle, {deleteProperty(target, property) {
87
+ if (typeof property === 'string') {
88
+ output.delete(property);
89
+ }
90
+ return true;
91
+ }});
92
+ }
93
+
94
+ //#endregion
95
+ //#region src/utils/normalize-plugin-option.ts
96
+ const normalizePluginOption = async (plugins) => (await asyncFlatten([plugins])).filter(Boolean);
97
+
98
+ //#endregion
99
+ //#region src/binding.js
100
+ var require_binding = __commonJSMin((exports, module) => {
101
+ const { readFileSync } = require('node:fs');
102
+ let nativeBinding = null;
103
+ const loadErrors = [];
104
+ const isMusl = () => {
105
+ let musl = false;
106
+ if (process.platform === 'linux') {
107
+ musl = isMuslFromFilesystem();
108
+ if (musl === null) {
109
+ musl = isMuslFromReport();
110
+ }
111
+ if (musl === null) {
112
+ musl = isMuslFromChildProcess();
113
+ }
114
+ }
115
+ return musl;
116
+ };
117
+ const isFileMusl = (f) => f.includes('libc.musl-') || f.includes('ld-musl-');
118
+ const isMuslFromFilesystem = () => {
119
+ try {
120
+ return readFileSync('/usr/bin/ldd', 'utf-8').includes('musl');
121
+ } catch {
122
+ return null;
123
+ }
124
+ };
125
+ const isMuslFromReport = () => {
126
+ const report = typeof process.report.getReport === 'function' ? process.report.getReport() : null;
127
+ if (!report) {
128
+ return null;
129
+ }
130
+ if (report.header && report.header.glibcVersionRuntime) {
131
+ return false;
132
+ }
133
+ if (Array.isArray(report.sharedObjects)) {
134
+ if (report.sharedObjects.some(isFileMusl)) {
135
+ return true;
136
+ }
137
+ }
138
+ return false;
139
+ };
140
+ const isMuslFromChildProcess = () => {
141
+ try {
142
+ return require('node:child_process').execSync('ldd --version', {encoding: 'utf8'}).includes('musl');
143
+ } catch (e) {
144
+ return false;
145
+ }
146
+ };
147
+ function requireNative() {
148
+ if (process.platform === 'android') {
149
+ if (process.arch === 'arm64') {
150
+ try {
151
+ return require('./rolldown-binding.android-arm64.node');
152
+ } catch (e) {
153
+ loadErrors.push(e);
154
+ }
155
+ try {
156
+ return require('@rolldown/binding-android-arm64');
157
+ } catch (e) {
158
+ loadErrors.push(e);
159
+ }
160
+ } else if (process.arch === 'arm') {
161
+ try {
162
+ return require('./rolldown-binding.android-arm-eabi.node');
163
+ } catch (e) {
164
+ loadErrors.push(e);
165
+ }
166
+ try {
167
+ return require('@rolldown/binding-android-arm-eabi');
168
+ } catch (e) {
169
+ loadErrors.push(e);
170
+ }
171
+ } else {
172
+ loadErrors.push(new Error(`Unsupported architecture on Android ${process.arch}`));
173
+ }
174
+ } else if (process.platform === 'win32') {
175
+ if (process.arch === 'x64') {
176
+ try {
177
+ return require('./rolldown-binding.win32-x64-msvc.node');
178
+ } catch (e) {
179
+ loadErrors.push(e);
180
+ }
181
+ try {
182
+ return require('@rolldown/binding-win32-x64-msvc');
183
+ } catch (e) {
184
+ loadErrors.push(e);
185
+ }
186
+ } else if (process.arch === 'ia32') {
187
+ try {
188
+ return require('./rolldown-binding.win32-ia32-msvc.node');
189
+ } catch (e) {
190
+ loadErrors.push(e);
191
+ }
192
+ try {
193
+ return require('@rolldown/binding-win32-ia32-msvc');
194
+ } catch (e) {
195
+ loadErrors.push(e);
196
+ }
197
+ } else if (process.arch === 'arm64') {
198
+ try {
199
+ return require('./rolldown-binding.win32-arm64-msvc.node');
200
+ } catch (e) {
201
+ loadErrors.push(e);
202
+ }
203
+ try {
204
+ return require('@rolldown/binding-win32-arm64-msvc');
205
+ } catch (e) {
206
+ loadErrors.push(e);
207
+ }
208
+ } else {
209
+ loadErrors.push(new Error(`Unsupported architecture on Windows: ${process.arch}`));
210
+ }
211
+ } else if (process.platform === 'darwin') {
212
+ try {
213
+ return require('./rolldown-binding.darwin-universal.node');
214
+ } catch (e) {
215
+ loadErrors.push(e);
216
+ }
217
+ try {
218
+ return require('@rolldown/binding-darwin-universal');
219
+ } catch (e) {
220
+ loadErrors.push(e);
221
+ }
222
+ if (process.arch === 'x64') {
223
+ try {
224
+ return require('./rolldown-binding.darwin-x64.node');
225
+ } catch (e) {
226
+ loadErrors.push(e);
227
+ }
228
+ try {
229
+ return require('@rolldown/binding-darwin-x64');
230
+ } catch (e) {
231
+ loadErrors.push(e);
232
+ }
233
+ } else if (process.arch === 'arm64') {
234
+ try {
235
+ return require('./rolldown-binding.darwin-arm64.node');
236
+ } catch (e) {
237
+ loadErrors.push(e);
238
+ }
239
+ try {
240
+ return require('@rolldown/binding-darwin-arm64');
241
+ } catch (e) {
242
+ loadErrors.push(e);
243
+ }
244
+ } else {
245
+ loadErrors.push(new Error(`Unsupported architecture on macOS: ${process.arch}`));
246
+ }
247
+ } else if (process.platform === 'freebsd') {
248
+ if (process.arch === 'x64') {
249
+ try {
250
+ return require('./rolldown-binding.freebsd-x64.node');
251
+ } catch (e) {
252
+ loadErrors.push(e);
253
+ }
254
+ try {
255
+ return require('@rolldown/binding-freebsd-x64');
256
+ } catch (e) {
257
+ loadErrors.push(e);
258
+ }
259
+ } else if (process.arch === 'arm64') {
260
+ try {
261
+ return require('./rolldown-binding.freebsd-arm64.node');
262
+ } catch (e) {
263
+ loadErrors.push(e);
264
+ }
265
+ try {
266
+ return require('@rolldown/binding-freebsd-arm64');
267
+ } catch (e) {
268
+ loadErrors.push(e);
269
+ }
270
+ } else {
271
+ loadErrors.push(new Error(`Unsupported architecture on FreeBSD: ${process.arch}`));
272
+ }
273
+ } else if (process.platform === 'linux') {
274
+ if (process.arch === 'x64') {
275
+ if (isMusl()) {
276
+ try {
277
+ return require('./rolldown-binding.linux-x64-musl.node');
278
+ } catch (e) {
279
+ loadErrors.push(e);
280
+ }
281
+ try {
282
+ return require('@rolldown/binding-linux-x64-musl');
283
+ } catch (e) {
284
+ loadErrors.push(e);
285
+ }
286
+ } else {
287
+ try {
288
+ return require('./rolldown-binding.linux-x64-gnu.node');
289
+ } catch (e) {
290
+ loadErrors.push(e);
291
+ }
292
+ try {
293
+ return require('@rolldown/binding-linux-x64-gnu');
294
+ } catch (e) {
295
+ loadErrors.push(e);
296
+ }
297
+ }
298
+ } else if (process.arch === 'arm64') {
299
+ if (isMusl()) {
300
+ try {
301
+ return require('./rolldown-binding.linux-arm64-musl.node');
302
+ } catch (e) {
303
+ loadErrors.push(e);
304
+ }
305
+ try {
306
+ return require('@rolldown/binding-linux-arm64-musl');
307
+ } catch (e) {
308
+ loadErrors.push(e);
309
+ }
310
+ } else {
311
+ try {
312
+ return require('./rolldown-binding.linux-arm64-gnu.node');
313
+ } catch (e) {
314
+ loadErrors.push(e);
315
+ }
316
+ try {
317
+ return require('@rolldown/binding-linux-arm64-gnu');
318
+ } catch (e) {
319
+ loadErrors.push(e);
320
+ }
321
+ }
322
+ } else if (process.arch === 'arm') {
323
+ if (isMusl()) {
324
+ try {
325
+ return require('./rolldown-binding.linux-arm-musleabihf.node');
326
+ } catch (e) {
327
+ loadErrors.push(e);
328
+ }
329
+ try {
330
+ return require('@rolldown/binding-linux-arm-musleabihf');
331
+ } catch (e) {
332
+ loadErrors.push(e);
333
+ }
334
+ } else {
335
+ try {
336
+ return require('./rolldown-binding.linux-arm-gnueabihf.node');
337
+ } catch (e) {
338
+ loadErrors.push(e);
339
+ }
340
+ try {
341
+ return require('@rolldown/binding-linux-arm-gnueabihf');
342
+ } catch (e) {
343
+ loadErrors.push(e);
344
+ }
345
+ }
346
+ } else if (process.arch === 'riscv64') {
347
+ if (isMusl()) {
348
+ try {
349
+ return require('./rolldown-binding.linux-riscv64-musl.node');
350
+ } catch (e) {
351
+ loadErrors.push(e);
352
+ }
353
+ try {
354
+ return require('@rolldown/binding-linux-riscv64-musl');
355
+ } catch (e) {
356
+ loadErrors.push(e);
357
+ }
358
+ } else {
359
+ try {
360
+ return require('./rolldown-binding.linux-riscv64-gnu.node');
361
+ } catch (e) {
362
+ loadErrors.push(e);
363
+ }
364
+ try {
365
+ return require('@rolldown/binding-linux-riscv64-gnu');
366
+ } catch (e) {
367
+ loadErrors.push(e);
368
+ }
369
+ }
370
+ } else if (process.arch === 'ppc64') {
371
+ try {
372
+ return require('./rolldown-binding.linux-ppc64-gnu.node');
373
+ } catch (e) {
374
+ loadErrors.push(e);
375
+ }
376
+ try {
377
+ return require('@rolldown/binding-linux-ppc64-gnu');
378
+ } catch (e) {
379
+ loadErrors.push(e);
380
+ }
381
+ } else if (process.arch === 's390x') {
382
+ try {
383
+ return require('./rolldown-binding.linux-s390x-gnu.node');
384
+ } catch (e) {
385
+ loadErrors.push(e);
386
+ }
387
+ try {
388
+ return require('@rolldown/binding-linux-s390x-gnu');
389
+ } catch (e) {
390
+ loadErrors.push(e);
391
+ }
392
+ } else {
393
+ loadErrors.push(new Error(`Unsupported architecture on Linux: ${process.arch}`));
394
+ }
395
+ } else {
396
+ loadErrors.push(new Error(`Unsupported OS: ${process.platform}, architecture: ${process.arch}`));
397
+ }
398
+ }
399
+ nativeBinding = requireNative();
400
+ if (!nativeBinding || process.env.NAPI_RS_FORCE_WASI) {
401
+ try {
402
+ nativeBinding = require('./rolldown-binding.wasi.cjs');
403
+ } catch (err) {
404
+ if (process.env.NAPI_RS_FORCE_WASI) {
405
+ console.error(err);
406
+ }
407
+ }
408
+ if (!nativeBinding) {
409
+ try {
410
+ nativeBinding = require('@rolldown/binding-wasm32-wasi');
411
+ } catch (err) {
412
+ if (process.env.NAPI_RS_FORCE_WASI) {
413
+ console.error(err);
414
+ }
415
+ }
416
+ }
417
+ }
418
+ if (!nativeBinding) {
419
+ if (loadErrors.length > 0) {
420
+ throw new Error('Failed to load native binding', {cause: loadErrors});
421
+ }
422
+ throw new Error(`Failed to load native binding`);
423
+ }
424
+ module.exports.BindingLog = nativeBinding.BindingLog;
425
+ module.exports.BindingModuleInfo = nativeBinding.BindingModuleInfo;
426
+ module.exports.BindingOutputAsset = nativeBinding.BindingOutputAsset;
427
+ module.exports.BindingOutputChunk = nativeBinding.BindingOutputChunk;
428
+ module.exports.BindingOutputs = nativeBinding.BindingOutputs;
429
+ module.exports.BindingPluginContext = nativeBinding.BindingPluginContext;
430
+ module.exports.BindingTransformPluginContext = nativeBinding.BindingTransformPluginContext;
431
+ module.exports.Bundler = nativeBinding.Bundler;
432
+ module.exports.FinalBindingOutputs = nativeBinding.FinalBindingOutputs;
433
+ module.exports.ParallelJsPluginRegistry = nativeBinding.ParallelJsPluginRegistry;
434
+ module.exports.BindingBuiltinPluginName = nativeBinding.BindingBuiltinPluginName;
435
+ module.exports.BindingHookSideEffects = nativeBinding.BindingHookSideEffects;
436
+ module.exports.BindingLogLevel = nativeBinding.BindingLogLevel;
437
+ module.exports.isolatedDeclaration = nativeBinding.isolatedDeclaration;
438
+ module.exports.registerPlugins = nativeBinding.registerPlugins;
439
+ module.exports.transform = nativeBinding.transform;
440
+ });
441
+
442
+ //#endregion
443
+ //#region src/utils/normalize-hook.ts
444
+ function normalizeHook(hook) {
445
+ if (typeof hook === 'function') {
446
+ return [hook, {}];
447
+ }
448
+ const { handler,...options } = hook;
449
+ return [handler, options];
450
+ }
451
+
452
+ //#endregion
453
+ //#region src/utils/transform-sourcemap.ts
454
+ function isEmptySourcemapFiled(array) {
455
+ if (!array) {
456
+ return true;
457
+ }
458
+ if (array.length === 0 || !array[0]) {
459
+ return true;
460
+ }
461
+ return false;
462
+ }
463
+
464
+ //#endregion
465
+ //#region src/utils/misc.ts
466
+ function arraify(value) {
467
+ return Array.isArray(value) ? value : [value];
468
+ }
469
+ function unimplemented(info) {
470
+ if (info) {
471
+ throw new Error(`unimplemented: ${info}`);
472
+ }
473
+ throw new Error('unimplemented');
474
+ }
475
+ function unsupported(info) {
476
+ return () => {
477
+ throw new Error(`UNSUPPORTED: ${info}`);
478
+ };
479
+ }
480
+ function noop(..._args) {}
481
+
482
+ //#endregion
483
+ //#region src/utils/transform-module-info.ts
484
+ function transformModuleInfo(info) {
485
+ return {
486
+ get ast() {
487
+ return unsupported('ModuleInfo#ast');
488
+ },
489
+ get code() {
490
+ return info.code;
491
+ },
492
+ id: info.id,
493
+ importers: info.importers,
494
+ dynamicImporters: info.dynamicImporters,
495
+ importedIds: info.importedIds,
496
+ dynamicallyImportedIds: info.dynamicallyImportedIds,
497
+ isEntry: info.isEntry
498
+ };
499
+ }
500
+
501
+ //#endregion
502
+ //#region src/types/sourcemap.ts
503
+ function bindingifySourcemap$1(map) {
504
+ if (map == null) return;
505
+ return {inner: typeof map === 'string' ? map : {
506
+ file: map.file ?? undefined,
507
+ mappings: map.mappings,
508
+ sourceRoot: map.sourceRoot,
509
+ sources: map.sources?.map((s) => s ?? undefined),
510
+ sourcesContent: map.sourcesContent?.map((s) => s ?? undefined),
511
+ names: map.names
512
+ }};
513
+ }
514
+
515
+ //#endregion
516
+ //#region src/log/logging.ts
517
+ const LogLevelSchema = z.literal('info').or(z.literal('debug')).or(z.literal('warn'));
518
+ const LogLevelWithErrorSchema = LogLevelSchema.or(z.literal('error'));
519
+ const LogLevelOptionSchema = LogLevelSchema.or(z.literal('silent'));
520
+ const LOG_LEVEL_SILENT = 'silent';
521
+ const LOG_LEVEL_ERROR = 'error';
522
+ const LOG_LEVEL_WARN = 'warn';
523
+ const LOG_LEVEL_INFO = 'info';
524
+ const LOG_LEVEL_DEBUG = 'debug';
525
+ const logLevelPriority = {
526
+ [LOG_LEVEL_DEBUG]: 0,
527
+ [LOG_LEVEL_INFO]: 1,
528
+ [LOG_LEVEL_WARN]: 2,
529
+ [LOG_LEVEL_SILENT]: 3
530
+ };
531
+ const RollupLogSchema = z.any();
532
+ const RollupLogWithStringSchema = RollupLogSchema.or(z.string());
533
+
534
+ //#endregion
535
+ //#region src/utils/code-frame.ts
536
+ function spaces(index) {
537
+ let result = '';
538
+ while (index--) result += ' ';
539
+ return result;
540
+ }
541
+ function tabsToSpaces(value) {
542
+ return value.replace(/^\t+/, (match) => match.split(' ').join(' '));
543
+ }
544
+ const LINE_TRUNCATE_LENGTH = 120;
545
+ const MIN_CHARACTERS_SHOWN_AFTER_LOCATION = 10;
546
+ const ELLIPSIS = '...';
547
+ function getCodeFrame(source, line, column) {
548
+ let lines = source.split('\n');
549
+ if (line > lines.length) return '';
550
+ const maxLineLength = Math.max(tabsToSpaces(lines[line - 1].slice(0, column)).length + MIN_CHARACTERS_SHOWN_AFTER_LOCATION + ELLIPSIS.length, LINE_TRUNCATE_LENGTH);
551
+ const frameStart = Math.max(0, line - 3);
552
+ let frameEnd = Math.min(line + 2, lines.length);
553
+ lines = lines.slice(frameStart, frameEnd);
554
+ while (!/\S/.test(lines[lines.length - 1])) {
555
+ lines.pop();
556
+ frameEnd -= 1;
557
+ }
558
+ const digits = String(frameEnd).length;
559
+ return lines.map((sourceLine, index) => {
560
+ const isErrorLine = frameStart + index + 1 === line;
561
+ let lineNumber = String(index + frameStart + 1);
562
+ while (lineNumber.length < digits) lineNumber = ` ${lineNumber}`;
563
+ let displayedLine = tabsToSpaces(sourceLine);
564
+ if (displayedLine.length > maxLineLength) {
565
+ displayedLine = `${displayedLine.slice(0, maxLineLength - ELLIPSIS.length)}${ELLIPSIS}`;
566
+ }
567
+ if (isErrorLine) {
568
+ const indicator = spaces(digits + 2 + tabsToSpaces(sourceLine.slice(0, column)).length) + '^';
569
+ return `${lineNumber}: ${displayedLine}\n${indicator}`;
570
+ }
571
+ return `${lineNumber}: ${displayedLine}`;
572
+ }).join('\n');
573
+ }
574
+
575
+ //#endregion
576
+ //#region ../../node_modules/.pnpm/locate-character@3.0.0/node_modules/locate-character/src/index.js
577
+ function rangeContains(range, index) {
578
+ return range.start <= index && index < range.end;
579
+ }
580
+ function getLocator(source, options = {}) {
581
+ const { offsetLine = 0, offsetColumn = 0 } = options;
582
+ let start = 0;
583
+ const ranges = source.split('\n').map((line, i$1) => {
584
+ const end = start + line.length + 1;
585
+ const range = {
586
+ start,
587
+ end,
588
+ line: i$1
589
+ };
590
+ start = end;
591
+ return range;
592
+ });
593
+ let i = 0;
594
+ function locator(search, index) {
595
+ if (typeof search === 'string') {
596
+ search = source.indexOf(search, index ?? 0);
597
+ }
598
+ if (search === -1) return undefined;
599
+ let range = ranges[i];
600
+ const d = search >= range.end ? 1 : -1;
601
+ while (range) {
602
+ if (rangeContains(range, search)) {
603
+ return {
604
+ line: offsetLine + range.line,
605
+ column: offsetColumn + search - range.start,
606
+ character: search
607
+ };
608
+ }
609
+ i += d;
610
+ range = ranges[i];
611
+ }
612
+ }
613
+ return locator;
614
+ }
615
+ function locate(source, search, options) {
616
+ return getLocator(source, options)(search, options && options.startIndex);
617
+ }
618
+
619
+ //#endregion
620
+ //#region src/log/logs.ts
621
+ const INVALID_LOG_POSITION = 'INVALID_LOG_POSITION', PLUGIN_ERROR = 'PLUGIN_ERROR';
622
+ function logInvalidLogPosition(pluginName) {
623
+ return {
624
+ code: INVALID_LOG_POSITION,
625
+ message: `Plugin "${pluginName}" tried to add a file position to a log or warning. This is only supported in the "transform" hook at the moment and will be ignored.`
626
+ };
627
+ }
628
+ function logPluginError(error$1, plugin, { hook, id } = {}) {
629
+ const code = error$1.code;
630
+ if (!error$1.pluginCode && code != null && (typeof code !== 'string' || !code.startsWith('PLUGIN_'))) {
631
+ error$1.pluginCode = code;
632
+ }
633
+ error$1.code = PLUGIN_ERROR;
634
+ error$1.plugin = plugin;
635
+ if (hook) {
636
+ error$1.hook = hook;
637
+ }
638
+ if (id) {
639
+ error$1.id = id;
640
+ }
641
+ return error$1;
642
+ }
643
+ function error(base) {
644
+ if (!(base instanceof Error)) {
645
+ base = Object.assign(new Error(base.message), base);
646
+ Object.defineProperty(base, 'name', {
647
+ value: 'RollupError',
648
+ writable: true
649
+ });
650
+ }
651
+ throw base;
652
+ }
653
+ function augmentCodeLocation(properties, pos, source, id) {
654
+ if (typeof pos === 'object') {
655
+ const { line, column } = pos;
656
+ properties.loc = {
657
+ column,
658
+ file: id,
659
+ line
660
+ };
661
+ } else {
662
+ properties.pos = pos;
663
+ const location = locate(source, pos, {offsetLine: 1});
664
+ if (!location) {
665
+ return;
666
+ }
667
+ const { line, column } = location;
668
+ properties.loc = {
669
+ column,
670
+ file: id,
671
+ line
672
+ };
673
+ }
674
+ if (properties.frame === undefined) {
675
+ const { line, column } = properties.loc;
676
+ properties.frame = getCodeFrame(source, line, column);
677
+ }
678
+ }
679
+
680
+ //#endregion
681
+ //#region src/log/logHandler.ts
682
+ const normalizeLog = (log) => typeof log === 'string' ? {message: log} : typeof log === 'function' ? normalizeLog(log()) : log;
683
+ function getLogHandler(level, code, logger, pluginName, logLevel) {
684
+ if (logLevelPriority[level] < logLevelPriority[logLevel]) {
685
+ return noop;
686
+ }
687
+ return (log, pos) => {
688
+ if (pos != null) {
689
+ logger(LOG_LEVEL_WARN, logInvalidLogPosition(pluginName));
690
+ }
691
+ log = normalizeLog(log);
692
+ if (log.code && !log.pluginCode) {
693
+ log.pluginCode = log.code;
694
+ }
695
+ log.code = code;
696
+ log.plugin = pluginName;
697
+ logger(level, log);
698
+ };
699
+ }
700
+
701
+ //#endregion
702
+ //#region src/plugin/plugin-context.ts
703
+ class PluginContext {
704
+ debug;
705
+ info;
706
+ warn;
707
+ error;
708
+ resolve;
709
+ emitFile;
710
+ getFileName;
711
+ getModuleInfo;
712
+ getModuleIds;
713
+ parse;
714
+ modules = new Map();
715
+ moduleIds = null;
716
+ constructor(options, context, plugin) {
717
+ const onLog = options.onLog;
718
+ const pluginName = plugin.name || 'unknown';
719
+ const logLevel = options.logLevel;
720
+ this.debug = getLogHandler(LOG_LEVEL_DEBUG, 'PLUGIN_LOG', onLog, pluginName, logLevel);
721
+ this.warn = getLogHandler(LOG_LEVEL_WARN, 'PLUGIN_WARNING', onLog, pluginName, logLevel);
722
+ this.info = getLogHandler(LOG_LEVEL_INFO, 'PLUGIN_LOG', onLog, pluginName, logLevel);
723
+ this.error = (e) => {
724
+ return error(logPluginError(normalizeLog(e), pluginName));
725
+ };
726
+ this.resolve = context.resolve.bind(context);
727
+ this.emitFile = (file) => {
728
+ if (file.type !== 'asset') {
729
+ return unimplemented('PluginContext.emitFile: only asset type is supported');
730
+ }
731
+ return context.emitFile({
732
+ ...file,
733
+ source: bindingAssetSource(file.source)
734
+ });
735
+ };
736
+ this.getFileName = context.getFileName.bind(context);
737
+ this.getModuleInfo = (id) => {
738
+ if (this.modules.has(id)) {
739
+ return this.modules.get(id) ?? null;
740
+ }
741
+ const bindingInfo = context.getModuleInfo(id);
742
+ if (bindingInfo) {
743
+ const info = transformModuleInfo(bindingInfo);
744
+ this.modules.set(id, info);
745
+ return info;
746
+ }
747
+ return null;
748
+ };
749
+ this.getModuleIds = () => {
750
+ if (this.moduleIds) {
751
+ return this.moduleIds.values();
752
+ }
753
+ const moduleIds = context.getModuleIds();
754
+ if (moduleIds) {
755
+ this.moduleIds = moduleIds;
756
+ return moduleIds.values();
757
+ }
758
+ return [].values();
759
+ };
760
+ this.parse = unsupported('`PluginContext#parse` is not supported by rolldown.');
761
+ }
762
+ }
763
+
764
+ //#endregion
765
+ //#region src/plugin/transfrom-plugin-context.ts
766
+ class TransformPluginContext {
767
+ debug;
768
+ info;
769
+ warn;
770
+ error;
771
+ resolve;
772
+ emitFile;
773
+ getFileName;
774
+ parse;
775
+ constructor(inner, context, moduleId, moduleSource) {
776
+ const getLogHandler$1 = (handler) => (log, pos) => {
777
+ log = normalizeLog(log);
778
+ if (pos) augmentCodeLocation(log, pos, moduleSource, moduleId);
779
+ log.id = moduleId;
780
+ log.hook = 'transform';
781
+ handler(log);
782
+ };
783
+ this.debug = getLogHandler$1(context.debug);
784
+ this.warn = getLogHandler$1(context.warn);
785
+ this.info = getLogHandler$1(context.info);
786
+ this.error = (error$1, pos) => {
787
+ if (typeof error$1 === 'string') error$1 = {message: error$1};
788
+ if (pos) augmentCodeLocation(error$1, pos, moduleSource, moduleId);
789
+ error$1.id = moduleId;
790
+ error$1.hook = 'transform';
791
+ return context.error(error$1);
792
+ };
793
+ this.resolve = context.resolve;
794
+ this.parse = context.parse;
795
+ this.emitFile = context.emitFile;
796
+ this.getFileName = context.getFileName;
797
+ }
798
+ }
799
+
800
+ //#endregion
801
+ //#region src/utils/transform-side-effects.ts
802
+ var import_binding$4 = __toESM(require_binding());
803
+ function bindingifySideEffects(sideEffects) {
804
+ switch (sideEffects) {
805
+ case true: return import_binding$4.BindingHookSideEffects.True;
806
+ case false: return import_binding$4.BindingHookSideEffects.False;
807
+ case 'no-treeshake': return import_binding$4.BindingHookSideEffects.NoTreeshake;
808
+ case null:
809
+ case undefined: return undefined;
810
+ default: throw new Error(`Unexpected side effects: ${sideEffects}`);
811
+ }
812
+ }
813
+
814
+ //#endregion
815
+ //#region src/plugin/bindingify-build-hooks.ts
816
+ function bindingifyBuildStart(plugin, options) {
817
+ const hook = plugin.buildStart;
818
+ if (!hook) {
819
+ return undefined;
820
+ }
821
+ const [handler, _optionsIgnoredSofar] = normalizeHook(hook);
822
+ return async (ctx) => {
823
+ await handler.call(new PluginContext(options, ctx, plugin), options);
824
+ };
825
+ }
826
+ function bindingifyBuildEnd(plugin, options) {
827
+ const hook = plugin.buildEnd;
828
+ if (!hook) {
829
+ return undefined;
830
+ }
831
+ const [handler, _optionsIgnoredSofar] = normalizeHook(hook);
832
+ return async (ctx, err) => {
833
+ await handler.call(new PluginContext(options, ctx, plugin), err ? new Error(err) : undefined);
834
+ };
835
+ }
836
+ function bindingifyResolveId(plugin, options) {
837
+ const hook = plugin.resolveId;
838
+ if (!hook) {
839
+ return undefined;
840
+ }
841
+ const [handler, _optionsIgnoredSofar] = normalizeHook(hook);
842
+ return async (ctx, specifier, importer, extraOptions) => {
843
+ const ret = await handler.call(new PluginContext(options, ctx, plugin), specifier, importer ?? undefined, extraOptions);
844
+ if (ret == false || ret == null) {
845
+ return;
846
+ }
847
+ if (typeof ret === 'string') {
848
+ return {id: ret};
849
+ }
850
+ const result = {
851
+ id: ret.id,
852
+ external: ret.external
853
+ };
854
+ if (ret.moduleSideEffects !== null) {
855
+ result.sideEffects = bindingifySideEffects(ret.moduleSideEffects);
856
+ }
857
+ return result;
858
+ };
859
+ }
860
+ function bindingifyResolveDynamicImport(plugin, options) {
861
+ const hook = plugin.resolveDynamicImport;
862
+ if (!hook) {
863
+ return undefined;
864
+ }
865
+ const [handler, _optionsIgnoredSofar] = normalizeHook(hook);
866
+ return async (ctx, specifier, importer) => {
867
+ const ret = await handler.call(new PluginContext(options, ctx, plugin), specifier, importer ?? undefined);
868
+ if (ret == false || ret == null) {
869
+ return;
870
+ }
871
+ if (typeof ret === 'string') {
872
+ return {id: ret};
873
+ }
874
+ const result = {
875
+ id: ret.id,
876
+ external: ret.external
877
+ };
878
+ if (ret.moduleSideEffects !== null) {
879
+ result.sideEffects = bindingifySideEffects(ret.moduleSideEffects);
880
+ }
881
+ return result;
882
+ };
883
+ }
884
+ function bindingifyTransform(plugin, options) {
885
+ const hook = plugin.transform;
886
+ if (!hook) {
887
+ return undefined;
888
+ }
889
+ const [handler, _optionsIgnoredSofar] = normalizeHook(hook);
890
+ return async (ctx, code, id) => {
891
+ const ret = await handler.call(new TransformPluginContext(ctx, new PluginContext(options, ctx.inner(), plugin), id, code), code, id);
892
+ if (ret == null) {
893
+ return undefined;
894
+ }
895
+ if (typeof ret === 'string') {
896
+ return {code: ret};
897
+ }
898
+ return {
899
+ code: ret.code,
900
+ map: bindingifySourcemap$1(ret.map),
901
+ sideEffects: bindingifySideEffects(ret.moduleSideEffects)
902
+ };
903
+ };
904
+ }
905
+ function bindingifyLoad(plugin, options) {
906
+ const hook = plugin.load;
907
+ if (!hook) {
908
+ return undefined;
909
+ }
910
+ const [handler, _optionsIgnoredSofar] = normalizeHook(hook);
911
+ return async (ctx, id) => {
912
+ const ret = await handler.call(new PluginContext(options, ctx, plugin), id);
913
+ if (ret == null) {
914
+ return;
915
+ }
916
+ if (typeof ret === 'string') {
917
+ return {code: ret};
918
+ }
919
+ if (!ret.map) {
920
+ return {code: ret.code};
921
+ }
922
+ let map = typeof ret.map === 'object' ? ret.map : JSON.parse(ret.map);
923
+ if (!isEmptySourcemapFiled(map.sources)) {
924
+ const directory = path$1.dirname(id) || '.';
925
+ const sourceRoot = map.sourceRoot || '.';
926
+ map.sources = map.sources.map((source) => path$1.resolve(directory, sourceRoot, source));
927
+ }
928
+ const result = {
929
+ code: ret.code,
930
+ map: bindingifySourcemap$1(map)
931
+ };
932
+ if (ret.moduleSideEffects !== null) {
933
+ result.sideEffects = bindingifySideEffects(ret.moduleSideEffects);
934
+ }
935
+ return result;
936
+ };
937
+ }
938
+ function bindingifyModuleParsed(plugin, options) {
939
+ const hook = plugin.moduleParsed;
940
+ if (!hook) {
941
+ return undefined;
942
+ }
943
+ const [handler, _optionsIgnoredSofar] = normalizeHook(hook);
944
+ return async (ctx, moduleInfo) => {
945
+ await handler.call(new PluginContext(options, ctx, plugin), transformModuleInfo(moduleInfo));
946
+ };
947
+ }
948
+
949
+ //#endregion
950
+ //#region src/plugin/bindingify-output-hooks.ts
951
+ function bindingifyRenderStart(plugin, options, outputOptions) {
952
+ const hook = plugin.renderStart;
953
+ if (!hook) {
954
+ return undefined;
955
+ }
956
+ const [handler, _optionsIgnoredSofar] = normalizeHook(hook);
957
+ return async (ctx) => {
958
+ handler.call(new PluginContext(options, ctx, plugin), outputOptions, options);
959
+ };
960
+ }
961
+ function bindingifyRenderChunk(plugin, options, outputOptions) {
962
+ const hook = plugin.renderChunk;
963
+ if (!hook) {
964
+ return undefined;
965
+ }
966
+ const [handler, _optionsIgnoredSofar] = normalizeHook(hook);
967
+ return async (ctx, code, chunk) => {
968
+ const ret = await handler.call(new PluginContext(options, ctx, plugin), code, chunk, outputOptions);
969
+ if (ret == null) {
970
+ return;
971
+ }
972
+ if (typeof ret === 'string') {
973
+ return {code: ret};
974
+ }
975
+ if (!ret.map) {
976
+ return {code: ret.code};
977
+ }
978
+ return {
979
+ code: ret.code,
980
+ map: bindingifySourcemap$1(ret.map)
981
+ };
982
+ };
983
+ }
984
+ function bindingifyAugmentChunkHash(plugin, options) {
985
+ const hook = plugin.augmentChunkHash;
986
+ if (!hook) {
987
+ return undefined;
988
+ }
989
+ const [handler, _optionsIgnoredSofar] = normalizeHook(hook);
990
+ return async (ctx, chunk) => {
991
+ return await handler.call(new PluginContext(options, ctx, plugin), chunk);
992
+ };
993
+ }
994
+ function bindingifyRenderError(plugin, options) {
995
+ const hook = plugin.renderError;
996
+ if (!hook) {
997
+ return undefined;
998
+ }
999
+ const [handler, _optionsIgnoredSofar] = normalizeHook(hook);
1000
+ return async (ctx, err) => {
1001
+ handler.call(new PluginContext(options, ctx, plugin), new Error(err));
1002
+ };
1003
+ }
1004
+ function bindingifyGenerateBundle(plugin, options, outputOptions) {
1005
+ const hook = plugin.generateBundle;
1006
+ if (!hook) {
1007
+ return undefined;
1008
+ }
1009
+ const [handler, _optionsIgnoredSofar] = normalizeHook(hook);
1010
+ return async (ctx, bundle, isWrite) => {
1011
+ handler.call(new PluginContext(options, ctx, plugin), outputOptions, transformToOutputBundle(bundle), isWrite);
1012
+ };
1013
+ }
1014
+ function bindingifyWriteBundle(plugin, options, outputOptions) {
1015
+ const hook = plugin.writeBundle;
1016
+ if (!hook) {
1017
+ return undefined;
1018
+ }
1019
+ const [handler, _optionsIgnoredSofar] = normalizeHook(hook);
1020
+ return async (ctx, bundle) => {
1021
+ handler.call(new PluginContext(options, ctx, plugin), outputOptions, transformToOutputBundle(bundle));
1022
+ };
1023
+ }
1024
+
1025
+ //#endregion
1026
+ //#region src/plugin/bindingify-plugin.ts
1027
+ function bindingifyPlugin(plugin, options, outputOptions) {
1028
+ return {
1029
+ name: plugin.name ?? 'unknown',
1030
+ buildStart: bindingifyBuildStart(plugin, options),
1031
+ resolveId: bindingifyResolveId(plugin, options),
1032
+ resolveDynamicImport: bindingifyResolveDynamicImport(plugin, options),
1033
+ buildEnd: bindingifyBuildEnd(plugin, options),
1034
+ transform: bindingifyTransform(plugin, options),
1035
+ moduleParsed: bindingifyModuleParsed(plugin, options),
1036
+ load: bindingifyLoad(plugin, options),
1037
+ renderChunk: bindingifyRenderChunk(plugin, options, outputOptions),
1038
+ augmentChunkHash: bindingifyAugmentChunkHash(plugin, options),
1039
+ renderStart: bindingifyRenderStart(plugin, options, outputOptions),
1040
+ renderError: bindingifyRenderError(plugin, options),
1041
+ generateBundle: bindingifyGenerateBundle(plugin, options, outputOptions),
1042
+ writeBundle: bindingifyWriteBundle(plugin, options, outputOptions)
1043
+ };
1044
+ }
1045
+
1046
+ //#endregion
1047
+ //#region src/plugin/bindingify-builtin-plugin.ts
1048
+ var import_binding$3 = __toESM(require_binding());
1049
+ class BuiltinPlugin {
1050
+ constructor(name, options) {
1051
+ this.name = name;
1052
+ this.options = options;
1053
+ this.name = name;
1054
+ this.options = options;
1055
+ }
1056
+ }
1057
+ class BuiltinWasmPlugin extends BuiltinPlugin {
1058
+ constructor(options) {
1059
+ super(import_binding$3.BindingBuiltinPluginName.WasmPlugin, options);
1060
+ }
1061
+ }
1062
+ class BuiltinGlobImportPlugin extends BuiltinPlugin {
1063
+ constructor(options) {
1064
+ super(import_binding$3.BindingBuiltinPluginName.GlobImportPlugin, options);
1065
+ }
1066
+ }
1067
+ function bindingifyBuiltInPlugin(plugin) {
1068
+ return {
1069
+ name: plugin.name,
1070
+ options: plugin.options
1071
+ };
1072
+ }
1073
+
1074
+ //#endregion
1075
+ //#region src/options/bindingify-input-options.ts
1076
+ var import_binding$2 = __toESM(require_binding());
1077
+ function bindingifyInputOptions(options, outputOptions) {
1078
+ return {
1079
+ input: bindingifyInput(options.input),
1080
+ plugins: options.plugins.map((plugin) => {
1081
+ if ('_parallel'in plugin) {
1082
+ return undefined;
1083
+ }
1084
+ if (plugin instanceof BuiltinPlugin) {
1085
+ return bindingifyBuiltInPlugin(plugin);
1086
+ }
1087
+ return bindingifyPlugin(plugin, options, outputOptions);
1088
+ }),
1089
+ cwd: options.cwd ?? process.cwd(),
1090
+ external: options.external ? function bindingifyExternal() {
1091
+ const external = options.external;
1092
+ if (typeof external === 'function') {
1093
+ return (id, importer, isResolved) => {
1094
+ if (id.startsWith('\0')) return false;
1095
+ return external(id, importer, isResolved) ?? false;
1096
+ };
1097
+ }
1098
+ const externalArr = arraify(external);
1099
+ return (id, _importer, _isResolved) => {
1100
+ return externalArr.some((pat) => {
1101
+ if (pat instanceof RegExp) {
1102
+ return pat.test(id);
1103
+ }
1104
+ return id === pat;
1105
+ });
1106
+ };
1107
+ }() : undefined,
1108
+ resolve: options.resolve ? function bindingifyResolve() {
1109
+ const { alias,...rest } = options.resolve;
1110
+ return {
1111
+ alias: alias ? Object.entries(alias).map(([name, replacement]) => ({
1112
+ find: name,
1113
+ replacements: [replacement]
1114
+ })) : undefined,
1115
+ ...rest
1116
+ };
1117
+ }() : undefined,
1118
+ platform: options.platform,
1119
+ shimMissingExports: options.shimMissingExports,
1120
+ logLevel: bindingifyLogLevel(options.logLevel),
1121
+ onLog: (level, log) => {
1122
+ options.onLog(level, {
1123
+ code: log.code,
1124
+ message: log.message
1125
+ });
1126
+ },
1127
+ treeshake: options.treeshake,
1128
+ moduleTypes: options.moduleTypes
1129
+ };
1130
+ }
1131
+ function bindingifyLogLevel(logLevel) {
1132
+ switch (logLevel) {
1133
+ case 'silent': return import_binding$2.BindingLogLevel.Silent;
1134
+ case 'warn': return import_binding$2.BindingLogLevel.Warn;
1135
+ case 'info': return import_binding$2.BindingLogLevel.Info;
1136
+ case 'debug': return import_binding$2.BindingLogLevel.Debug;
1137
+ default: throw new Error(`Unexpected log level: ${logLevel}`);
1138
+ }
1139
+ }
1140
+ function bindingifyInput(input) {
1141
+ if (Array.isArray(input)) {
1142
+ return input.map((src) => {
1143
+ const name = nodePath.parse(src).name;
1144
+ return {
1145
+ name,
1146
+ import: src
1147
+ };
1148
+ });
1149
+ } else {
1150
+ return Object.entries(input).map((value) => {
1151
+ return {
1152
+ name: value[0],
1153
+ import: value[1]
1154
+ };
1155
+ });
1156
+ }
1157
+ }
1158
+
1159
+ //#endregion
1160
+ //#region src/utils/initialize-parallel-plugins.ts
1161
+ var import_binding$1 = __toESM(require_binding());
1162
+ async function initializeParallelPlugins(plugins) {
1163
+ const pluginInfos = [];
1164
+ for (const [index, plugin] of plugins.entries()) {
1165
+ if ('_parallel'in plugin) {
1166
+ const { fileUrl, options } = plugin._parallel;
1167
+ pluginInfos.push({
1168
+ index,
1169
+ fileUrl,
1170
+ options
1171
+ });
1172
+ }
1173
+ }
1174
+ if (pluginInfos.length <= 0) {
1175
+ return undefined;
1176
+ }
1177
+ const count = Math.min(availableParallelism(), 8);
1178
+ const parallelJsPluginRegistry = new import_binding$1.ParallelJsPluginRegistry(count);
1179
+ const registryId = parallelJsPluginRegistry.id;
1180
+ const workers = await initializeWorkers(registryId, count, pluginInfos);
1181
+ const stopWorkers = async () => {
1182
+ await Promise.all(workers.map((worker) => worker.terminate()));
1183
+ };
1184
+ return {
1185
+ registry: parallelJsPluginRegistry,
1186
+ stopWorkers
1187
+ };
1188
+ }
1189
+ function initializeWorkers(registryId, count, pluginInfos) {
1190
+ return Promise.all(Array.from({length: count}, (_, i) => initializeWorker(registryId, pluginInfos, i)));
1191
+ }
1192
+ async function initializeWorker(registryId, pluginInfos, threadNumber) {
1193
+ const urlString = undefined('#parallel-plugin-worker');
1194
+ const workerData = {
1195
+ registryId,
1196
+ pluginInfos,
1197
+ threadNumber
1198
+ };
1199
+ let worker;
1200
+ try {
1201
+ worker = new Worker(new URL(urlString), {workerData});
1202
+ worker.unref();
1203
+ await new Promise((resolve, reject) => {
1204
+ worker.once('message', async (message) => {
1205
+ if (message.type === 'error') {
1206
+ reject(message.error);
1207
+ } else {
1208
+ resolve();
1209
+ }
1210
+ });
1211
+ });
1212
+ return worker;
1213
+ } catch (e) {
1214
+ worker?.terminate();
1215
+ throw e;
1216
+ }
1217
+ }
1218
+
1219
+ //#endregion
1220
+ //#region src/log/logger.ts
1221
+ function getLogger(plugins, onLog, logLevel) {
1222
+ const minimalPriority = logLevelPriority[logLevel];
1223
+ const logger = (level, log, skipped = new Set()) => {
1224
+ const logPriority = logLevelPriority[level];
1225
+ if (logPriority < minimalPriority) {
1226
+ return;
1227
+ }
1228
+ for (const plugin of plugins) {
1229
+ if (skipped.has(plugin)) continue;
1230
+ const { onLog: pluginOnLog } = plugin;
1231
+ if (pluginOnLog) {
1232
+ const getLogHandler$1 = (level$1) => {
1233
+ if (logLevelPriority[level$1] < minimalPriority) {
1234
+ return () => {};
1235
+ }
1236
+ return (log$1) => logger(level$1, normalizeLog(log$1), new Set(skipped).add(plugin));
1237
+ };
1238
+ const handler = 'handler'in pluginOnLog ? pluginOnLog.handler : pluginOnLog;
1239
+ if (handler.call({
1240
+ debug: getLogHandler$1(LOG_LEVEL_DEBUG),
1241
+ error: (log$1) => error(normalizeLog(log$1)),
1242
+ info: getLogHandler$1(LOG_LEVEL_INFO),
1243
+ warn: getLogHandler$1(LOG_LEVEL_WARN)
1244
+ }, level, log) === false) {
1245
+ return;
1246
+ }
1247
+ }
1248
+ }
1249
+ onLog(level, log);
1250
+ };
1251
+ return logger;
1252
+ }
1253
+ const getOnLog = (config, logLevel, printLog = defaultPrintLog) => {
1254
+ const { onwarn, onLog } = config;
1255
+ const defaultOnLog = getDefaultOnLog(printLog, onwarn);
1256
+ if (onLog) {
1257
+ const minimalPriority = logLevelPriority[logLevel];
1258
+ return (level, log) => onLog(level, addLogToString(log), (level$1, handledLog) => {
1259
+ if (level$1 === LOG_LEVEL_ERROR) {
1260
+ return error(normalizeLog(handledLog));
1261
+ }
1262
+ if (logLevelPriority[level$1] >= minimalPriority) {
1263
+ defaultOnLog(level$1, normalizeLog(handledLog));
1264
+ }
1265
+ });
1266
+ }
1267
+ return defaultOnLog;
1268
+ };
1269
+ const getDefaultOnLog = (printLog, onwarn) => onwarn ? (level, log) => {
1270
+ if (level === LOG_LEVEL_WARN) {
1271
+ onwarn(addLogToString(log), (warning) => printLog(LOG_LEVEL_WARN, normalizeLog(warning)));
1272
+ } else {
1273
+ printLog(level, log);
1274
+ }
1275
+ } : printLog;
1276
+ const addLogToString = (log) => {
1277
+ Object.defineProperty(log, 'toString', {
1278
+ value: () => getExtendedLogMessage(log),
1279
+ writable: true
1280
+ });
1281
+ return log;
1282
+ };
1283
+ const defaultPrintLog = (level, log) => {
1284
+ const message = getExtendedLogMessage(log);
1285
+ switch (level) {
1286
+ case LOG_LEVEL_WARN: {
1287
+ return console.warn(message);
1288
+ }
1289
+ case LOG_LEVEL_DEBUG: {
1290
+ return console.debug(message);
1291
+ }
1292
+ default: {
1293
+ return console.info(message);
1294
+ }
1295
+ }
1296
+ };
1297
+ const getExtendedLogMessage = (log) => {
1298
+ let prefix = '';
1299
+ if (log.plugin) {
1300
+ prefix += `(${log.plugin} plugin) `;
1301
+ }
1302
+ if (log.loc) {
1303
+ prefix += `${relativeId(log.loc.file)} (${log.loc.line}:${log.loc.column}) `;
1304
+ }
1305
+ return prefix + log.message;
1306
+ };
1307
+ function relativeId(id) {
1308
+ if (!path.isAbsolute(id)) return id;
1309
+ return path.relative(path.resolve(), id);
1310
+ }
1311
+
1312
+ //#endregion
1313
+ //#region src/plugin/plugin-driver.ts
1314
+ class PluginDriver {
1315
+ async callOptionsHook(inputOptions) {
1316
+ const logLevel = inputOptions.logLevel || LOG_LEVEL_INFO;
1317
+ const plugins = getObjectPlugins(inputOptions.plugins ?? []);
1318
+ const logger = getLogger(plugins, getOnLog(inputOptions, logLevel), logLevel);
1319
+ for (const plugin of plugins) {
1320
+ const name = plugin.name || 'unknown';
1321
+ const options = plugin.options;
1322
+ if (options) {
1323
+ const [handler, _optionsIgnoredSofar] = normalizeHook(options);
1324
+ const result = await handler.call({
1325
+ debug: getLogHandler(LOG_LEVEL_DEBUG, 'PLUGIN_LOG', logger, name, logLevel),
1326
+ error: (e) => error(logPluginError(normalizeLog(e), name, {hook: 'onLog'})),
1327
+ info: getLogHandler(LOG_LEVEL_INFO, 'PLUGIN_LOG', logger, name, logLevel),
1328
+ warn: getLogHandler(LOG_LEVEL_WARN, 'PLUGIN_WARNING', logger, name, logLevel)
1329
+ }, inputOptions);
1330
+ if (result) {
1331
+ inputOptions = result;
1332
+ }
1333
+ }
1334
+ }
1335
+ return inputOptions;
1336
+ }
1337
+ async callOutputOptionsHook(inputOptions, outputOptions) {
1338
+ const plugins = getObjectPlugins(inputOptions.plugins);
1339
+ for (const plugin of plugins) {
1340
+ const options = plugin.outputOptions;
1341
+ if (options) {
1342
+ const [handler, _optionsIgnoredSofar] = normalizeHook(options);
1343
+ const result = await handler.call(null, outputOptions);
1344
+ if (result) {
1345
+ outputOptions = result;
1346
+ }
1347
+ }
1348
+ }
1349
+ return outputOptions;
1350
+ }
1351
+ }
1352
+ function getObjectPlugins(plugins) {
1353
+ return plugins.filter((plugin) => {
1354
+ if ('_parallel'in plugin) {
1355
+ return undefined;
1356
+ }
1357
+ if (plugin instanceof BuiltinPlugin) {
1358
+ return undefined;
1359
+ }
1360
+ return plugin;
1361
+ });
1362
+ }
1363
+
1364
+ //#endregion
1365
+ //#region src/utils/normalize-tree-shake.ts
1366
+ function normalizeTreeshakeOptions(config) {
1367
+ if (config === false) {
1368
+ return undefined;
1369
+ }
1370
+ if (config === true || config === undefined) {
1371
+ return {moduleSideEffects: 'true'};
1372
+ }
1373
+ if (config.moduleSideEffects === undefined) {
1374
+ config.moduleSideEffects = 'true';
1375
+ } else if (isRegExp(config.moduleSideEffects)) {
1376
+ config.moduleSideEffects = config.moduleSideEffects.source;
1377
+ } else {
1378
+ config.moduleSideEffects = config.moduleSideEffects.toString();
1379
+ }
1380
+ return config;
1381
+ }
1382
+
1383
+ //#endregion
1384
+ //#region src/utils/normalize-input-options.ts
1385
+ async function normalizeInputOptions(config) {
1386
+ const { input,...rest } = config;
1387
+ const plugins = await normalizePluginOption(config.plugins);
1388
+ const treeshake = normalizeTreeshakeOptions(config.treeshake);
1389
+ const logLevel = config.logLevel || LOG_LEVEL_INFO;
1390
+ const onLog = getLogger(getObjectPlugins(plugins), getOnLog(config, logLevel), logLevel);
1391
+ return {
1392
+ ...rest,
1393
+ input: input ? typeof input === 'string' ? [input] : input : [],
1394
+ plugins,
1395
+ logLevel,
1396
+ onLog,
1397
+ treeshake
1398
+ };
1399
+ }
1400
+
1401
+ //#endregion
1402
+ //#region src/utils/normalize-output-options.ts
1403
+ function normalizeOutputOptions(opts) {
1404
+ const { dir, format, exports, sourcemap, sourcemapIgnoreList, sourcemapPathTransform, entryFileNames, chunkFileNames, assetFileNames } = opts;
1405
+ return {
1406
+ dir: dir,
1407
+ format: getFormat(format),
1408
+ exports: exports ?? 'named',
1409
+ sourcemap: sourcemap ?? false,
1410
+ sourcemapIgnoreList: typeof sourcemapIgnoreList === 'function' ? sourcemapIgnoreList : sourcemapIgnoreList === false ? () => false : (relativeSourcePath, sourcemapPath) => relativeSourcePath.includes('node_modules'),
1411
+ sourcemapPathTransform,
1412
+ banner: getAddon(opts, 'banner'),
1413
+ footer: getAddon(opts, 'footer'),
1414
+ entryFileNames: entryFileNames ?? '[name].js',
1415
+ chunkFileNames: chunkFileNames ?? '[name]-[hash].js',
1416
+ assetFileNames: assetFileNames ?? 'assets/[name]-[hash][extname]',
1417
+ plugins: []
1418
+ };
1419
+ }
1420
+ function getFormat(format) {
1421
+ switch (format) {
1422
+ case undefined:
1423
+ case 'es':
1424
+ case 'esm':
1425
+ case 'module': {
1426
+ return 'es';
1427
+ }
1428
+ case 'cjs':
1429
+ case 'commonjs': {
1430
+ return 'cjs';
1431
+ }
1432
+ default: unimplemented(`output.format: ${format}`);
1433
+ }
1434
+ }
1435
+ const getAddon = (config, name) => {
1436
+ const configAddon = config[name];
1437
+ if (typeof configAddon === 'function') {
1438
+ return configAddon;
1439
+ }
1440
+ return async () => configAddon || '';
1441
+ };
1442
+
1443
+ //#endregion
1444
+ //#region src/options/bindingify-output-options.ts
1445
+ function bindingifyOutputOptions(outputOptions) {
1446
+ const { dir, format, exports, sourcemap, sourcemapIgnoreList, sourcemapPathTransform, entryFileNames, chunkFileNames, assetFileNames, banner, footer } = outputOptions;
1447
+ return {
1448
+ dir,
1449
+ format: function() {
1450
+ switch (format) {
1451
+ case 'es': return 'es';
1452
+ case 'cjs': return 'cjs';
1453
+ }
1454
+ }(),
1455
+ exports,
1456
+ sourcemap: bindingifySourcemap(sourcemap),
1457
+ sourcemapIgnoreList,
1458
+ sourcemapPathTransform,
1459
+ banner,
1460
+ footer,
1461
+ entryFileNames,
1462
+ chunkFileNames,
1463
+ assetFileNames,
1464
+ plugins: []
1465
+ };
1466
+ }
1467
+ function bindingifySourcemap(sourcemap) {
1468
+ switch (sourcemap) {
1469
+ case true: return 'file';
1470
+ case 'inline': return 'inline';
1471
+ case false:
1472
+ case undefined:
1473
+ case 'hidden': return 'hidden';
1474
+ default: throw new Error(`unknown sourcemap: ${sourcemap}`);
1475
+ }
1476
+ }
1477
+
1478
+ //#endregion
1479
+ //#region src/utils/create-bundler.ts
1480
+ var import_binding = __toESM(require_binding());
1481
+ async function createBundler(inputOptions, outputOptions) {
1482
+ const pluginDriver = new PluginDriver();
1483
+ inputOptions = await pluginDriver.callOptionsHook(inputOptions);
1484
+ const normalizedInputOptions = await normalizeInputOptions(inputOptions);
1485
+ const parallelPluginInitResult = await initializeParallelPlugins(normalizedInputOptions.plugins);
1486
+ try {
1487
+ outputOptions = await pluginDriver.callOutputOptionsHook(normalizedInputOptions, outputOptions);
1488
+ const normalizedOutputOptions = normalizeOutputOptions(outputOptions);
1489
+ const bindingInputOptions = bindingifyInputOptions(normalizedInputOptions, normalizedOutputOptions);
1490
+ return {
1491
+ bundler: new import_binding.Bundler(bindingInputOptions, bindingifyOutputOptions(normalizedOutputOptions), parallelPluginInitResult?.registry),
1492
+ stopWorkers: parallelPluginInitResult?.stopWorkers
1493
+ };
1494
+ } catch (e) {
1495
+ await parallelPluginInitResult?.stopWorkers();
1496
+ throw e;
1497
+ }
1498
+ }
1499
+
1500
+ //#endregion
1501
+ exports.BuiltinGlobImportPlugin = BuiltinGlobImportPlugin;
1502
+ exports.BuiltinWasmPlugin = BuiltinWasmPlugin;
1503
+ exports.arraify = arraify;
1504
+ exports.bindingifyPlugin = bindingifyPlugin;
1505
+ exports.createBundler = createBundler;
1506
+ exports.require_binding = require_binding;
1507
+ exports.transformToRollupOutput = transformToRollupOutput;