@coderline/alphatab 1.2.3 → 1.3.0-alpha.1006

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,2044 @@
1
+ /**
2
+ * alphaTab v1.3.0-alpha.1006 (develop, build 1006)
3
+ *
4
+ * Copyright © 2024, Daniel Kuschny and Contributors, All rights reserved.
5
+ *
6
+ * This Source Code Form is subject to the terms of the Mozilla Public
7
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
8
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9
+ *
10
+ * SoundFont loading and Audio Synthesis based on TinySoundFont (licensed under MIT)
11
+ * Copyright (C) 2017, 2018 Bernhard Schelling (https://github.com/schellingb/TinySoundFont)
12
+ *
13
+ * TinySoundFont is based on SFZero (licensed under MIT)
14
+ * Copyright (C) 2012 Steve Folta (https://github.com/stevefolta/SFZero)
15
+ */
16
+
17
+ import * as path from 'path';
18
+ import path__default from 'path';
19
+ import * as fs from 'fs';
20
+ import { createHash } from 'node:crypto';
21
+ import { normalizePath } from 'vite';
22
+
23
+ const comma = ','.charCodeAt(0);
24
+ const semicolon = ';'.charCodeAt(0);
25
+ const chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/';
26
+ const intToChar = new Uint8Array(64); // 64 possible chars.
27
+ const charToInt = new Uint8Array(128); // z is 122 in ASCII
28
+ for (let i = 0; i < chars.length; i++) {
29
+ const c = chars.charCodeAt(i);
30
+ intToChar[i] = c;
31
+ charToInt[c] = i;
32
+ }
33
+ // Provide a fallback for older environments.
34
+ const td = typeof TextDecoder !== 'undefined'
35
+ ? /* #__PURE__ */ new TextDecoder()
36
+ : typeof Buffer !== 'undefined'
37
+ ? {
38
+ decode(buf) {
39
+ const out = Buffer.from(buf.buffer, buf.byteOffset, buf.byteLength);
40
+ return out.toString();
41
+ },
42
+ }
43
+ : {
44
+ decode(buf) {
45
+ let out = '';
46
+ for (let i = 0; i < buf.length; i++) {
47
+ out += String.fromCharCode(buf[i]);
48
+ }
49
+ return out;
50
+ },
51
+ };
52
+ function encode(decoded) {
53
+ const state = new Int32Array(5);
54
+ const bufLength = 1024 * 16;
55
+ const subLength = bufLength - 36;
56
+ const buf = new Uint8Array(bufLength);
57
+ const sub = buf.subarray(0, subLength);
58
+ let pos = 0;
59
+ let out = '';
60
+ for (let i = 0; i < decoded.length; i++) {
61
+ const line = decoded[i];
62
+ if (i > 0) {
63
+ if (pos === bufLength) {
64
+ out += td.decode(buf);
65
+ pos = 0;
66
+ }
67
+ buf[pos++] = semicolon;
68
+ }
69
+ if (line.length === 0)
70
+ continue;
71
+ state[0] = 0;
72
+ for (let j = 0; j < line.length; j++) {
73
+ const segment = line[j];
74
+ // We can push up to 5 ints, each int can take at most 7 chars, and we
75
+ // may push a comma.
76
+ if (pos > subLength) {
77
+ out += td.decode(sub);
78
+ buf.copyWithin(0, subLength, pos);
79
+ pos -= subLength;
80
+ }
81
+ if (j > 0)
82
+ buf[pos++] = comma;
83
+ pos = encodeInteger(buf, pos, state, segment, 0); // genColumn
84
+ if (segment.length === 1)
85
+ continue;
86
+ pos = encodeInteger(buf, pos, state, segment, 1); // sourcesIndex
87
+ pos = encodeInteger(buf, pos, state, segment, 2); // sourceLine
88
+ pos = encodeInteger(buf, pos, state, segment, 3); // sourceColumn
89
+ if (segment.length === 4)
90
+ continue;
91
+ pos = encodeInteger(buf, pos, state, segment, 4); // namesIndex
92
+ }
93
+ }
94
+ return out + td.decode(buf.subarray(0, pos));
95
+ }
96
+ function encodeInteger(buf, pos, state, segment, j) {
97
+ const next = segment[j];
98
+ let num = next - state[j];
99
+ state[j] = next;
100
+ num = num < 0 ? (-num << 1) | 1 : num << 1;
101
+ do {
102
+ let clamped = num & 0b011111;
103
+ num >>>= 5;
104
+ if (num > 0)
105
+ clamped |= 0b100000;
106
+ buf[pos++] = intToChar[clamped];
107
+ } while (num > 0);
108
+ return pos;
109
+ }
110
+
111
+ class BitSet {
112
+ constructor(arg) {
113
+ this.bits = arg instanceof BitSet ? arg.bits.slice() : [];
114
+ }
115
+
116
+ add(n) {
117
+ this.bits[n >> 5] |= 1 << (n & 31);
118
+ }
119
+
120
+ has(n) {
121
+ return !!(this.bits[n >> 5] & (1 << (n & 31)));
122
+ }
123
+ }
124
+
125
+ class Chunk {
126
+ constructor(start, end, content) {
127
+ this.start = start;
128
+ this.end = end;
129
+ this.original = content;
130
+
131
+ this.intro = '';
132
+ this.outro = '';
133
+
134
+ this.content = content;
135
+ this.storeName = false;
136
+ this.edited = false;
137
+
138
+ {
139
+ this.previous = null;
140
+ this.next = null;
141
+ }
142
+ }
143
+
144
+ appendLeft(content) {
145
+ this.outro += content;
146
+ }
147
+
148
+ appendRight(content) {
149
+ this.intro = this.intro + content;
150
+ }
151
+
152
+ clone() {
153
+ const chunk = new Chunk(this.start, this.end, this.original);
154
+
155
+ chunk.intro = this.intro;
156
+ chunk.outro = this.outro;
157
+ chunk.content = this.content;
158
+ chunk.storeName = this.storeName;
159
+ chunk.edited = this.edited;
160
+
161
+ return chunk;
162
+ }
163
+
164
+ contains(index) {
165
+ return this.start < index && index < this.end;
166
+ }
167
+
168
+ eachNext(fn) {
169
+ let chunk = this;
170
+ while (chunk) {
171
+ fn(chunk);
172
+ chunk = chunk.next;
173
+ }
174
+ }
175
+
176
+ eachPrevious(fn) {
177
+ let chunk = this;
178
+ while (chunk) {
179
+ fn(chunk);
180
+ chunk = chunk.previous;
181
+ }
182
+ }
183
+
184
+ edit(content, storeName, contentOnly) {
185
+ this.content = content;
186
+ if (!contentOnly) {
187
+ this.intro = '';
188
+ this.outro = '';
189
+ }
190
+ this.storeName = storeName;
191
+
192
+ this.edited = true;
193
+
194
+ return this;
195
+ }
196
+
197
+ prependLeft(content) {
198
+ this.outro = content + this.outro;
199
+ }
200
+
201
+ prependRight(content) {
202
+ this.intro = content + this.intro;
203
+ }
204
+
205
+ split(index) {
206
+ const sliceIndex = index - this.start;
207
+
208
+ const originalBefore = this.original.slice(0, sliceIndex);
209
+ const originalAfter = this.original.slice(sliceIndex);
210
+
211
+ this.original = originalBefore;
212
+
213
+ const newChunk = new Chunk(index, this.end, originalAfter);
214
+ newChunk.outro = this.outro;
215
+ this.outro = '';
216
+
217
+ this.end = index;
218
+
219
+ if (this.edited) {
220
+ // after split we should save the edit content record into the correct chunk
221
+ // to make sure sourcemap correct
222
+ // For example:
223
+ // ' test'.trim()
224
+ // split -> ' ' + 'test'
225
+ // ✔️ edit -> '' + 'test'
226
+ // ✖️ edit -> 'test' + ''
227
+ // TODO is this block necessary?...
228
+ newChunk.edit('', false);
229
+ this.content = '';
230
+ } else {
231
+ this.content = originalBefore;
232
+ }
233
+
234
+ newChunk.next = this.next;
235
+ if (newChunk.next) newChunk.next.previous = newChunk;
236
+ newChunk.previous = this;
237
+ this.next = newChunk;
238
+
239
+ return newChunk;
240
+ }
241
+
242
+ toString() {
243
+ return this.intro + this.content + this.outro;
244
+ }
245
+
246
+ trimEnd(rx) {
247
+ this.outro = this.outro.replace(rx, '');
248
+ if (this.outro.length) return true;
249
+
250
+ const trimmed = this.content.replace(rx, '');
251
+
252
+ if (trimmed.length) {
253
+ if (trimmed !== this.content) {
254
+ this.split(this.start + trimmed.length).edit('', undefined, true);
255
+ if (this.edited) {
256
+ // save the change, if it has been edited
257
+ this.edit(trimmed, this.storeName, true);
258
+ }
259
+ }
260
+ return true;
261
+ } else {
262
+ this.edit('', undefined, true);
263
+
264
+ this.intro = this.intro.replace(rx, '');
265
+ if (this.intro.length) return true;
266
+ }
267
+ }
268
+
269
+ trimStart(rx) {
270
+ this.intro = this.intro.replace(rx, '');
271
+ if (this.intro.length) return true;
272
+
273
+ const trimmed = this.content.replace(rx, '');
274
+
275
+ if (trimmed.length) {
276
+ if (trimmed !== this.content) {
277
+ const newChunk = this.split(this.end - trimmed.length);
278
+ if (this.edited) {
279
+ // save the change, if it has been edited
280
+ newChunk.edit(trimmed, this.storeName, true);
281
+ }
282
+ this.edit('', undefined, true);
283
+ }
284
+ return true;
285
+ } else {
286
+ this.edit('', undefined, true);
287
+
288
+ this.outro = this.outro.replace(rx, '');
289
+ if (this.outro.length) return true;
290
+ }
291
+ }
292
+ }
293
+
294
+ function getBtoa() {
295
+ if (typeof window !== 'undefined' && typeof window.btoa === 'function') {
296
+ return (str) => window.btoa(unescape(encodeURIComponent(str)));
297
+ } else if (typeof Buffer === 'function') {
298
+ return (str) => Buffer.from(str, 'utf-8').toString('base64');
299
+ } else {
300
+ return () => {
301
+ throw new Error('Unsupported environment: `window.btoa` or `Buffer` should be supported.');
302
+ };
303
+ }
304
+ }
305
+
306
+ const btoa = /*#__PURE__*/ getBtoa();
307
+
308
+ class SourceMap {
309
+ constructor(properties) {
310
+ this.version = 3;
311
+ this.file = properties.file;
312
+ this.sources = properties.sources;
313
+ this.sourcesContent = properties.sourcesContent;
314
+ this.names = properties.names;
315
+ this.mappings = encode(properties.mappings);
316
+ if (typeof properties.x_google_ignoreList !== 'undefined') {
317
+ this.x_google_ignoreList = properties.x_google_ignoreList;
318
+ }
319
+ }
320
+
321
+ toString() {
322
+ return JSON.stringify(this);
323
+ }
324
+
325
+ toUrl() {
326
+ return 'data:application/json;charset=utf-8;base64,' + btoa(this.toString());
327
+ }
328
+ }
329
+
330
+ function guessIndent(code) {
331
+ const lines = code.split('\n');
332
+
333
+ const tabbed = lines.filter((line) => /^\t+/.test(line));
334
+ const spaced = lines.filter((line) => /^ {2,}/.test(line));
335
+
336
+ if (tabbed.length === 0 && spaced.length === 0) {
337
+ return null;
338
+ }
339
+
340
+ // More lines tabbed than spaced? Assume tabs, and
341
+ // default to tabs in the case of a tie (or nothing
342
+ // to go on)
343
+ if (tabbed.length >= spaced.length) {
344
+ return '\t';
345
+ }
346
+
347
+ // Otherwise, we need to guess the multiple
348
+ const min = spaced.reduce((previous, current) => {
349
+ const numSpaces = /^ +/.exec(current)[0].length;
350
+ return Math.min(numSpaces, previous);
351
+ }, Infinity);
352
+
353
+ return new Array(min + 1).join(' ');
354
+ }
355
+
356
+ function getRelativePath(from, to) {
357
+ const fromParts = from.split(/[/\\]/);
358
+ const toParts = to.split(/[/\\]/);
359
+
360
+ fromParts.pop(); // get dirname
361
+
362
+ while (fromParts[0] === toParts[0]) {
363
+ fromParts.shift();
364
+ toParts.shift();
365
+ }
366
+
367
+ if (fromParts.length) {
368
+ let i = fromParts.length;
369
+ while (i--) fromParts[i] = '..';
370
+ }
371
+
372
+ return fromParts.concat(toParts).join('/');
373
+ }
374
+
375
+ const toString = Object.prototype.toString;
376
+
377
+ function isObject(thing) {
378
+ return toString.call(thing) === '[object Object]';
379
+ }
380
+
381
+ function getLocator(source) {
382
+ const originalLines = source.split('\n');
383
+ const lineOffsets = [];
384
+
385
+ for (let i = 0, pos = 0; i < originalLines.length; i++) {
386
+ lineOffsets.push(pos);
387
+ pos += originalLines[i].length + 1;
388
+ }
389
+
390
+ return function locate(index) {
391
+ let i = 0;
392
+ let j = lineOffsets.length;
393
+ while (i < j) {
394
+ const m = (i + j) >> 1;
395
+ if (index < lineOffsets[m]) {
396
+ j = m;
397
+ } else {
398
+ i = m + 1;
399
+ }
400
+ }
401
+ const line = i - 1;
402
+ const column = index - lineOffsets[line];
403
+ return { line, column };
404
+ };
405
+ }
406
+
407
+ const wordRegex = /\w/;
408
+
409
+ class Mappings {
410
+ constructor(hires) {
411
+ this.hires = hires;
412
+ this.generatedCodeLine = 0;
413
+ this.generatedCodeColumn = 0;
414
+ this.raw = [];
415
+ this.rawSegments = this.raw[this.generatedCodeLine] = [];
416
+ this.pending = null;
417
+ }
418
+
419
+ addEdit(sourceIndex, content, loc, nameIndex) {
420
+ if (content.length) {
421
+ let contentLineEnd = content.indexOf('\n', 0);
422
+ let previousContentLineEnd = -1;
423
+ while (contentLineEnd >= 0) {
424
+ const segment = [this.generatedCodeColumn, sourceIndex, loc.line, loc.column];
425
+ if (nameIndex >= 0) {
426
+ segment.push(nameIndex);
427
+ }
428
+ this.rawSegments.push(segment);
429
+
430
+ this.generatedCodeLine += 1;
431
+ this.raw[this.generatedCodeLine] = this.rawSegments = [];
432
+ this.generatedCodeColumn = 0;
433
+
434
+ previousContentLineEnd = contentLineEnd;
435
+ contentLineEnd = content.indexOf('\n', contentLineEnd + 1);
436
+ }
437
+
438
+ const segment = [this.generatedCodeColumn, sourceIndex, loc.line, loc.column];
439
+ if (nameIndex >= 0) {
440
+ segment.push(nameIndex);
441
+ }
442
+ this.rawSegments.push(segment);
443
+
444
+ this.advance(content.slice(previousContentLineEnd + 1));
445
+ } else if (this.pending) {
446
+ this.rawSegments.push(this.pending);
447
+ this.advance(content);
448
+ }
449
+
450
+ this.pending = null;
451
+ }
452
+
453
+ addUneditedChunk(sourceIndex, chunk, original, loc, sourcemapLocations) {
454
+ let originalCharIndex = chunk.start;
455
+ let first = true;
456
+ // when iterating each char, check if it's in a word boundary
457
+ let charInHiresBoundary = false;
458
+
459
+ while (originalCharIndex < chunk.end) {
460
+ if (this.hires || first || sourcemapLocations.has(originalCharIndex)) {
461
+ const segment = [this.generatedCodeColumn, sourceIndex, loc.line, loc.column];
462
+
463
+ if (this.hires === 'boundary') {
464
+ // in hires "boundary", group segments per word boundary than per char
465
+ if (wordRegex.test(original[originalCharIndex])) {
466
+ // for first char in the boundary found, start the boundary by pushing a segment
467
+ if (!charInHiresBoundary) {
468
+ this.rawSegments.push(segment);
469
+ charInHiresBoundary = true;
470
+ }
471
+ } else {
472
+ // for non-word char, end the boundary by pushing a segment
473
+ this.rawSegments.push(segment);
474
+ charInHiresBoundary = false;
475
+ }
476
+ } else {
477
+ this.rawSegments.push(segment);
478
+ }
479
+ }
480
+
481
+ if (original[originalCharIndex] === '\n') {
482
+ loc.line += 1;
483
+ loc.column = 0;
484
+ this.generatedCodeLine += 1;
485
+ this.raw[this.generatedCodeLine] = this.rawSegments = [];
486
+ this.generatedCodeColumn = 0;
487
+ first = true;
488
+ } else {
489
+ loc.column += 1;
490
+ this.generatedCodeColumn += 1;
491
+ first = false;
492
+ }
493
+
494
+ originalCharIndex += 1;
495
+ }
496
+
497
+ this.pending = null;
498
+ }
499
+
500
+ advance(str) {
501
+ if (!str) return;
502
+
503
+ const lines = str.split('\n');
504
+
505
+ if (lines.length > 1) {
506
+ for (let i = 0; i < lines.length - 1; i++) {
507
+ this.generatedCodeLine++;
508
+ this.raw[this.generatedCodeLine] = this.rawSegments = [];
509
+ }
510
+ this.generatedCodeColumn = 0;
511
+ }
512
+
513
+ this.generatedCodeColumn += lines[lines.length - 1].length;
514
+ }
515
+ }
516
+
517
+ const n = '\n';
518
+
519
+ const warned = {
520
+ insertLeft: false,
521
+ insertRight: false,
522
+ storeName: false,
523
+ };
524
+
525
+ class MagicString {
526
+ constructor(string, options = {}) {
527
+ const chunk = new Chunk(0, string.length, string);
528
+
529
+ Object.defineProperties(this, {
530
+ original: { writable: true, value: string },
531
+ outro: { writable: true, value: '' },
532
+ intro: { writable: true, value: '' },
533
+ firstChunk: { writable: true, value: chunk },
534
+ lastChunk: { writable: true, value: chunk },
535
+ lastSearchedChunk: { writable: true, value: chunk },
536
+ byStart: { writable: true, value: {} },
537
+ byEnd: { writable: true, value: {} },
538
+ filename: { writable: true, value: options.filename },
539
+ indentExclusionRanges: { writable: true, value: options.indentExclusionRanges },
540
+ sourcemapLocations: { writable: true, value: new BitSet() },
541
+ storedNames: { writable: true, value: {} },
542
+ indentStr: { writable: true, value: undefined },
543
+ ignoreList: { writable: true, value: options.ignoreList },
544
+ });
545
+
546
+ this.byStart[0] = chunk;
547
+ this.byEnd[string.length] = chunk;
548
+ }
549
+
550
+ addSourcemapLocation(char) {
551
+ this.sourcemapLocations.add(char);
552
+ }
553
+
554
+ append(content) {
555
+ if (typeof content !== 'string') throw new TypeError('outro content must be a string');
556
+
557
+ this.outro += content;
558
+ return this;
559
+ }
560
+
561
+ appendLeft(index, content) {
562
+ if (typeof content !== 'string') throw new TypeError('inserted content must be a string');
563
+
564
+ this._split(index);
565
+
566
+ const chunk = this.byEnd[index];
567
+
568
+ if (chunk) {
569
+ chunk.appendLeft(content);
570
+ } else {
571
+ this.intro += content;
572
+ }
573
+ return this;
574
+ }
575
+
576
+ appendRight(index, content) {
577
+ if (typeof content !== 'string') throw new TypeError('inserted content must be a string');
578
+
579
+ this._split(index);
580
+
581
+ const chunk = this.byStart[index];
582
+
583
+ if (chunk) {
584
+ chunk.appendRight(content);
585
+ } else {
586
+ this.outro += content;
587
+ }
588
+ return this;
589
+ }
590
+
591
+ clone() {
592
+ const cloned = new MagicString(this.original, { filename: this.filename });
593
+
594
+ let originalChunk = this.firstChunk;
595
+ let clonedChunk = (cloned.firstChunk = cloned.lastSearchedChunk = originalChunk.clone());
596
+
597
+ while (originalChunk) {
598
+ cloned.byStart[clonedChunk.start] = clonedChunk;
599
+ cloned.byEnd[clonedChunk.end] = clonedChunk;
600
+
601
+ const nextOriginalChunk = originalChunk.next;
602
+ const nextClonedChunk = nextOriginalChunk && nextOriginalChunk.clone();
603
+
604
+ if (nextClonedChunk) {
605
+ clonedChunk.next = nextClonedChunk;
606
+ nextClonedChunk.previous = clonedChunk;
607
+
608
+ clonedChunk = nextClonedChunk;
609
+ }
610
+
611
+ originalChunk = nextOriginalChunk;
612
+ }
613
+
614
+ cloned.lastChunk = clonedChunk;
615
+
616
+ if (this.indentExclusionRanges) {
617
+ cloned.indentExclusionRanges = this.indentExclusionRanges.slice();
618
+ }
619
+
620
+ cloned.sourcemapLocations = new BitSet(this.sourcemapLocations);
621
+
622
+ cloned.intro = this.intro;
623
+ cloned.outro = this.outro;
624
+
625
+ return cloned;
626
+ }
627
+
628
+ generateDecodedMap(options) {
629
+ options = options || {};
630
+
631
+ const sourceIndex = 0;
632
+ const names = Object.keys(this.storedNames);
633
+ const mappings = new Mappings(options.hires);
634
+
635
+ const locate = getLocator(this.original);
636
+
637
+ if (this.intro) {
638
+ mappings.advance(this.intro);
639
+ }
640
+
641
+ this.firstChunk.eachNext((chunk) => {
642
+ const loc = locate(chunk.start);
643
+
644
+ if (chunk.intro.length) mappings.advance(chunk.intro);
645
+
646
+ if (chunk.edited) {
647
+ mappings.addEdit(
648
+ sourceIndex,
649
+ chunk.content,
650
+ loc,
651
+ chunk.storeName ? names.indexOf(chunk.original) : -1,
652
+ );
653
+ } else {
654
+ mappings.addUneditedChunk(sourceIndex, chunk, this.original, loc, this.sourcemapLocations);
655
+ }
656
+
657
+ if (chunk.outro.length) mappings.advance(chunk.outro);
658
+ });
659
+
660
+ return {
661
+ file: options.file ? options.file.split(/[/\\]/).pop() : undefined,
662
+ sources: [
663
+ options.source ? getRelativePath(options.file || '', options.source) : options.file || '',
664
+ ],
665
+ sourcesContent: options.includeContent ? [this.original] : undefined,
666
+ names,
667
+ mappings: mappings.raw,
668
+ x_google_ignoreList: this.ignoreList ? [sourceIndex] : undefined,
669
+ };
670
+ }
671
+
672
+ generateMap(options) {
673
+ return new SourceMap(this.generateDecodedMap(options));
674
+ }
675
+
676
+ _ensureindentStr() {
677
+ if (this.indentStr === undefined) {
678
+ this.indentStr = guessIndent(this.original);
679
+ }
680
+ }
681
+
682
+ _getRawIndentString() {
683
+ this._ensureindentStr();
684
+ return this.indentStr;
685
+ }
686
+
687
+ getIndentString() {
688
+ this._ensureindentStr();
689
+ return this.indentStr === null ? '\t' : this.indentStr;
690
+ }
691
+
692
+ indent(indentStr, options) {
693
+ const pattern = /^[^\r\n]/gm;
694
+
695
+ if (isObject(indentStr)) {
696
+ options = indentStr;
697
+ indentStr = undefined;
698
+ }
699
+
700
+ if (indentStr === undefined) {
701
+ this._ensureindentStr();
702
+ indentStr = this.indentStr || '\t';
703
+ }
704
+
705
+ if (indentStr === '') return this; // noop
706
+
707
+ options = options || {};
708
+
709
+ // Process exclusion ranges
710
+ const isExcluded = {};
711
+
712
+ if (options.exclude) {
713
+ const exclusions =
714
+ typeof options.exclude[0] === 'number' ? [options.exclude] : options.exclude;
715
+ exclusions.forEach((exclusion) => {
716
+ for (let i = exclusion[0]; i < exclusion[1]; i += 1) {
717
+ isExcluded[i] = true;
718
+ }
719
+ });
720
+ }
721
+
722
+ let shouldIndentNextCharacter = options.indentStart !== false;
723
+ const replacer = (match) => {
724
+ if (shouldIndentNextCharacter) return `${indentStr}${match}`;
725
+ shouldIndentNextCharacter = true;
726
+ return match;
727
+ };
728
+
729
+ this.intro = this.intro.replace(pattern, replacer);
730
+
731
+ let charIndex = 0;
732
+ let chunk = this.firstChunk;
733
+
734
+ while (chunk) {
735
+ const end = chunk.end;
736
+
737
+ if (chunk.edited) {
738
+ if (!isExcluded[charIndex]) {
739
+ chunk.content = chunk.content.replace(pattern, replacer);
740
+
741
+ if (chunk.content.length) {
742
+ shouldIndentNextCharacter = chunk.content[chunk.content.length - 1] === '\n';
743
+ }
744
+ }
745
+ } else {
746
+ charIndex = chunk.start;
747
+
748
+ while (charIndex < end) {
749
+ if (!isExcluded[charIndex]) {
750
+ const char = this.original[charIndex];
751
+
752
+ if (char === '\n') {
753
+ shouldIndentNextCharacter = true;
754
+ } else if (char !== '\r' && shouldIndentNextCharacter) {
755
+ shouldIndentNextCharacter = false;
756
+
757
+ if (charIndex === chunk.start) {
758
+ chunk.prependRight(indentStr);
759
+ } else {
760
+ this._splitChunk(chunk, charIndex);
761
+ chunk = chunk.next;
762
+ chunk.prependRight(indentStr);
763
+ }
764
+ }
765
+ }
766
+
767
+ charIndex += 1;
768
+ }
769
+ }
770
+
771
+ charIndex = chunk.end;
772
+ chunk = chunk.next;
773
+ }
774
+
775
+ this.outro = this.outro.replace(pattern, replacer);
776
+
777
+ return this;
778
+ }
779
+
780
+ insert() {
781
+ throw new Error(
782
+ 'magicString.insert(...) is deprecated. Use prependRight(...) or appendLeft(...)',
783
+ );
784
+ }
785
+
786
+ insertLeft(index, content) {
787
+ if (!warned.insertLeft) {
788
+ console.warn(
789
+ 'magicString.insertLeft(...) is deprecated. Use magicString.appendLeft(...) instead',
790
+ ); // eslint-disable-line no-console
791
+ warned.insertLeft = true;
792
+ }
793
+
794
+ return this.appendLeft(index, content);
795
+ }
796
+
797
+ insertRight(index, content) {
798
+ if (!warned.insertRight) {
799
+ console.warn(
800
+ 'magicString.insertRight(...) is deprecated. Use magicString.prependRight(...) instead',
801
+ ); // eslint-disable-line no-console
802
+ warned.insertRight = true;
803
+ }
804
+
805
+ return this.prependRight(index, content);
806
+ }
807
+
808
+ move(start, end, index) {
809
+ if (index >= start && index <= end) throw new Error('Cannot move a selection inside itself');
810
+
811
+ this._split(start);
812
+ this._split(end);
813
+ this._split(index);
814
+
815
+ const first = this.byStart[start];
816
+ const last = this.byEnd[end];
817
+
818
+ const oldLeft = first.previous;
819
+ const oldRight = last.next;
820
+
821
+ const newRight = this.byStart[index];
822
+ if (!newRight && last === this.lastChunk) return this;
823
+ const newLeft = newRight ? newRight.previous : this.lastChunk;
824
+
825
+ if (oldLeft) oldLeft.next = oldRight;
826
+ if (oldRight) oldRight.previous = oldLeft;
827
+
828
+ if (newLeft) newLeft.next = first;
829
+ if (newRight) newRight.previous = last;
830
+
831
+ if (!first.previous) this.firstChunk = last.next;
832
+ if (!last.next) {
833
+ this.lastChunk = first.previous;
834
+ this.lastChunk.next = null;
835
+ }
836
+
837
+ first.previous = newLeft;
838
+ last.next = newRight || null;
839
+
840
+ if (!newLeft) this.firstChunk = first;
841
+ if (!newRight) this.lastChunk = last;
842
+ return this;
843
+ }
844
+
845
+ overwrite(start, end, content, options) {
846
+ options = options || {};
847
+ return this.update(start, end, content, { ...options, overwrite: !options.contentOnly });
848
+ }
849
+
850
+ update(start, end, content, options) {
851
+ if (typeof content !== 'string') throw new TypeError('replacement content must be a string');
852
+
853
+ while (start < 0) start += this.original.length;
854
+ while (end < 0) end += this.original.length;
855
+
856
+ if (end > this.original.length) throw new Error('end is out of bounds');
857
+ if (start === end)
858
+ throw new Error(
859
+ 'Cannot overwrite a zero-length range – use appendLeft or prependRight instead',
860
+ );
861
+
862
+ this._split(start);
863
+ this._split(end);
864
+
865
+ if (options === true) {
866
+ if (!warned.storeName) {
867
+ console.warn(
868
+ 'The final argument to magicString.overwrite(...) should be an options object. See https://github.com/rich-harris/magic-string',
869
+ ); // eslint-disable-line no-console
870
+ warned.storeName = true;
871
+ }
872
+
873
+ options = { storeName: true };
874
+ }
875
+ const storeName = options !== undefined ? options.storeName : false;
876
+ const overwrite = options !== undefined ? options.overwrite : false;
877
+
878
+ if (storeName) {
879
+ const original = this.original.slice(start, end);
880
+ Object.defineProperty(this.storedNames, original, {
881
+ writable: true,
882
+ value: true,
883
+ enumerable: true,
884
+ });
885
+ }
886
+
887
+ const first = this.byStart[start];
888
+ const last = this.byEnd[end];
889
+
890
+ if (first) {
891
+ let chunk = first;
892
+ while (chunk !== last) {
893
+ if (chunk.next !== this.byStart[chunk.end]) {
894
+ throw new Error('Cannot overwrite across a split point');
895
+ }
896
+ chunk = chunk.next;
897
+ chunk.edit('', false);
898
+ }
899
+
900
+ first.edit(content, storeName, !overwrite);
901
+ } else {
902
+ // must be inserting at the end
903
+ const newChunk = new Chunk(start, end, '').edit(content, storeName);
904
+
905
+ // TODO last chunk in the array may not be the last chunk, if it's moved...
906
+ last.next = newChunk;
907
+ newChunk.previous = last;
908
+ }
909
+ return this;
910
+ }
911
+
912
+ prepend(content) {
913
+ if (typeof content !== 'string') throw new TypeError('outro content must be a string');
914
+
915
+ this.intro = content + this.intro;
916
+ return this;
917
+ }
918
+
919
+ prependLeft(index, content) {
920
+ if (typeof content !== 'string') throw new TypeError('inserted content must be a string');
921
+
922
+ this._split(index);
923
+
924
+ const chunk = this.byEnd[index];
925
+
926
+ if (chunk) {
927
+ chunk.prependLeft(content);
928
+ } else {
929
+ this.intro = content + this.intro;
930
+ }
931
+ return this;
932
+ }
933
+
934
+ prependRight(index, content) {
935
+ if (typeof content !== 'string') throw new TypeError('inserted content must be a string');
936
+
937
+ this._split(index);
938
+
939
+ const chunk = this.byStart[index];
940
+
941
+ if (chunk) {
942
+ chunk.prependRight(content);
943
+ } else {
944
+ this.outro = content + this.outro;
945
+ }
946
+ return this;
947
+ }
948
+
949
+ remove(start, end) {
950
+ while (start < 0) start += this.original.length;
951
+ while (end < 0) end += this.original.length;
952
+
953
+ if (start === end) return this;
954
+
955
+ if (start < 0 || end > this.original.length) throw new Error('Character is out of bounds');
956
+ if (start > end) throw new Error('end must be greater than start');
957
+
958
+ this._split(start);
959
+ this._split(end);
960
+
961
+ let chunk = this.byStart[start];
962
+
963
+ while (chunk) {
964
+ chunk.intro = '';
965
+ chunk.outro = '';
966
+ chunk.edit('');
967
+
968
+ chunk = end > chunk.end ? this.byStart[chunk.end] : null;
969
+ }
970
+ return this;
971
+ }
972
+
973
+ lastChar() {
974
+ if (this.outro.length) return this.outro[this.outro.length - 1];
975
+ let chunk = this.lastChunk;
976
+ do {
977
+ if (chunk.outro.length) return chunk.outro[chunk.outro.length - 1];
978
+ if (chunk.content.length) return chunk.content[chunk.content.length - 1];
979
+ if (chunk.intro.length) return chunk.intro[chunk.intro.length - 1];
980
+ } while ((chunk = chunk.previous));
981
+ if (this.intro.length) return this.intro[this.intro.length - 1];
982
+ return '';
983
+ }
984
+
985
+ lastLine() {
986
+ let lineIndex = this.outro.lastIndexOf(n);
987
+ if (lineIndex !== -1) return this.outro.substr(lineIndex + 1);
988
+ let lineStr = this.outro;
989
+ let chunk = this.lastChunk;
990
+ do {
991
+ if (chunk.outro.length > 0) {
992
+ lineIndex = chunk.outro.lastIndexOf(n);
993
+ if (lineIndex !== -1) return chunk.outro.substr(lineIndex + 1) + lineStr;
994
+ lineStr = chunk.outro + lineStr;
995
+ }
996
+
997
+ if (chunk.content.length > 0) {
998
+ lineIndex = chunk.content.lastIndexOf(n);
999
+ if (lineIndex !== -1) return chunk.content.substr(lineIndex + 1) + lineStr;
1000
+ lineStr = chunk.content + lineStr;
1001
+ }
1002
+
1003
+ if (chunk.intro.length > 0) {
1004
+ lineIndex = chunk.intro.lastIndexOf(n);
1005
+ if (lineIndex !== -1) return chunk.intro.substr(lineIndex + 1) + lineStr;
1006
+ lineStr = chunk.intro + lineStr;
1007
+ }
1008
+ } while ((chunk = chunk.previous));
1009
+ lineIndex = this.intro.lastIndexOf(n);
1010
+ if (lineIndex !== -1) return this.intro.substr(lineIndex + 1) + lineStr;
1011
+ return this.intro + lineStr;
1012
+ }
1013
+
1014
+ slice(start = 0, end = this.original.length) {
1015
+ while (start < 0) start += this.original.length;
1016
+ while (end < 0) end += this.original.length;
1017
+
1018
+ let result = '';
1019
+
1020
+ // find start chunk
1021
+ let chunk = this.firstChunk;
1022
+ while (chunk && (chunk.start > start || chunk.end <= start)) {
1023
+ // found end chunk before start
1024
+ if (chunk.start < end && chunk.end >= end) {
1025
+ return result;
1026
+ }
1027
+
1028
+ chunk = chunk.next;
1029
+ }
1030
+
1031
+ if (chunk && chunk.edited && chunk.start !== start)
1032
+ throw new Error(`Cannot use replaced character ${start} as slice start anchor.`);
1033
+
1034
+ const startChunk = chunk;
1035
+ while (chunk) {
1036
+ if (chunk.intro && (startChunk !== chunk || chunk.start === start)) {
1037
+ result += chunk.intro;
1038
+ }
1039
+
1040
+ const containsEnd = chunk.start < end && chunk.end >= end;
1041
+ if (containsEnd && chunk.edited && chunk.end !== end)
1042
+ throw new Error(`Cannot use replaced character ${end} as slice end anchor.`);
1043
+
1044
+ const sliceStart = startChunk === chunk ? start - chunk.start : 0;
1045
+ const sliceEnd = containsEnd ? chunk.content.length + end - chunk.end : chunk.content.length;
1046
+
1047
+ result += chunk.content.slice(sliceStart, sliceEnd);
1048
+
1049
+ if (chunk.outro && (!containsEnd || chunk.end === end)) {
1050
+ result += chunk.outro;
1051
+ }
1052
+
1053
+ if (containsEnd) {
1054
+ break;
1055
+ }
1056
+
1057
+ chunk = chunk.next;
1058
+ }
1059
+
1060
+ return result;
1061
+ }
1062
+
1063
+ // TODO deprecate this? not really very useful
1064
+ snip(start, end) {
1065
+ const clone = this.clone();
1066
+ clone.remove(0, start);
1067
+ clone.remove(end, clone.original.length);
1068
+
1069
+ return clone;
1070
+ }
1071
+
1072
+ _split(index) {
1073
+ if (this.byStart[index] || this.byEnd[index]) return;
1074
+
1075
+ let chunk = this.lastSearchedChunk;
1076
+ const searchForward = index > chunk.end;
1077
+
1078
+ while (chunk) {
1079
+ if (chunk.contains(index)) return this._splitChunk(chunk, index);
1080
+
1081
+ chunk = searchForward ? this.byStart[chunk.end] : this.byEnd[chunk.start];
1082
+ }
1083
+ }
1084
+
1085
+ _splitChunk(chunk, index) {
1086
+ if (chunk.edited && chunk.content.length) {
1087
+ // zero-length edited chunks are a special case (overlapping replacements)
1088
+ const loc = getLocator(this.original)(index);
1089
+ throw new Error(
1090
+ `Cannot split a chunk that has already been edited (${loc.line}:${loc.column} – "${chunk.original}")`,
1091
+ );
1092
+ }
1093
+
1094
+ const newChunk = chunk.split(index);
1095
+
1096
+ this.byEnd[index] = chunk;
1097
+ this.byStart[index] = newChunk;
1098
+ this.byEnd[newChunk.end] = newChunk;
1099
+
1100
+ if (chunk === this.lastChunk) this.lastChunk = newChunk;
1101
+
1102
+ this.lastSearchedChunk = chunk;
1103
+ return true;
1104
+ }
1105
+
1106
+ toString() {
1107
+ let str = this.intro;
1108
+
1109
+ let chunk = this.firstChunk;
1110
+ while (chunk) {
1111
+ str += chunk.toString();
1112
+ chunk = chunk.next;
1113
+ }
1114
+
1115
+ return str + this.outro;
1116
+ }
1117
+
1118
+ isEmpty() {
1119
+ let chunk = this.firstChunk;
1120
+ do {
1121
+ if (
1122
+ (chunk.intro.length && chunk.intro.trim()) ||
1123
+ (chunk.content.length && chunk.content.trim()) ||
1124
+ (chunk.outro.length && chunk.outro.trim())
1125
+ )
1126
+ return false;
1127
+ } while ((chunk = chunk.next));
1128
+ return true;
1129
+ }
1130
+
1131
+ length() {
1132
+ let chunk = this.firstChunk;
1133
+ let length = 0;
1134
+ do {
1135
+ length += chunk.intro.length + chunk.content.length + chunk.outro.length;
1136
+ } while ((chunk = chunk.next));
1137
+ return length;
1138
+ }
1139
+
1140
+ trimLines() {
1141
+ return this.trim('[\\r\\n]');
1142
+ }
1143
+
1144
+ trim(charType) {
1145
+ return this.trimStart(charType).trimEnd(charType);
1146
+ }
1147
+
1148
+ trimEndAborted(charType) {
1149
+ const rx = new RegExp((charType || '\\s') + '+$');
1150
+
1151
+ this.outro = this.outro.replace(rx, '');
1152
+ if (this.outro.length) return true;
1153
+
1154
+ let chunk = this.lastChunk;
1155
+
1156
+ do {
1157
+ const end = chunk.end;
1158
+ const aborted = chunk.trimEnd(rx);
1159
+
1160
+ // if chunk was trimmed, we have a new lastChunk
1161
+ if (chunk.end !== end) {
1162
+ if (this.lastChunk === chunk) {
1163
+ this.lastChunk = chunk.next;
1164
+ }
1165
+
1166
+ this.byEnd[chunk.end] = chunk;
1167
+ this.byStart[chunk.next.start] = chunk.next;
1168
+ this.byEnd[chunk.next.end] = chunk.next;
1169
+ }
1170
+
1171
+ if (aborted) return true;
1172
+ chunk = chunk.previous;
1173
+ } while (chunk);
1174
+
1175
+ return false;
1176
+ }
1177
+
1178
+ trimEnd(charType) {
1179
+ this.trimEndAborted(charType);
1180
+ return this;
1181
+ }
1182
+ trimStartAborted(charType) {
1183
+ const rx = new RegExp('^' + (charType || '\\s') + '+');
1184
+
1185
+ this.intro = this.intro.replace(rx, '');
1186
+ if (this.intro.length) return true;
1187
+
1188
+ let chunk = this.firstChunk;
1189
+
1190
+ do {
1191
+ const end = chunk.end;
1192
+ const aborted = chunk.trimStart(rx);
1193
+
1194
+ if (chunk.end !== end) {
1195
+ // special case...
1196
+ if (chunk === this.lastChunk) this.lastChunk = chunk.next;
1197
+
1198
+ this.byEnd[chunk.end] = chunk;
1199
+ this.byStart[chunk.next.start] = chunk.next;
1200
+ this.byEnd[chunk.next.end] = chunk.next;
1201
+ }
1202
+
1203
+ if (aborted) return true;
1204
+ chunk = chunk.next;
1205
+ } while (chunk);
1206
+
1207
+ return false;
1208
+ }
1209
+
1210
+ trimStart(charType) {
1211
+ this.trimStartAborted(charType);
1212
+ return this;
1213
+ }
1214
+
1215
+ hasChanged() {
1216
+ return this.original !== this.toString();
1217
+ }
1218
+
1219
+ _replaceRegexp(searchValue, replacement) {
1220
+ function getReplacement(match, str) {
1221
+ if (typeof replacement === 'string') {
1222
+ return replacement.replace(/\$(\$|&|\d+)/g, (_, i) => {
1223
+ // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replace#specifying_a_string_as_a_parameter
1224
+ if (i === '$') return '$';
1225
+ if (i === '&') return match[0];
1226
+ const num = +i;
1227
+ if (num < match.length) return match[+i];
1228
+ return `$${i}`;
1229
+ });
1230
+ } else {
1231
+ return replacement(...match, match.index, str, match.groups);
1232
+ }
1233
+ }
1234
+ function matchAll(re, str) {
1235
+ let match;
1236
+ const matches = [];
1237
+ while ((match = re.exec(str))) {
1238
+ matches.push(match);
1239
+ }
1240
+ return matches;
1241
+ }
1242
+ if (searchValue.global) {
1243
+ const matches = matchAll(searchValue, this.original);
1244
+ matches.forEach((match) => {
1245
+ if (match.index != null)
1246
+ this.overwrite(
1247
+ match.index,
1248
+ match.index + match[0].length,
1249
+ getReplacement(match, this.original),
1250
+ );
1251
+ });
1252
+ } else {
1253
+ const match = this.original.match(searchValue);
1254
+ if (match && match.index != null)
1255
+ this.overwrite(
1256
+ match.index,
1257
+ match.index + match[0].length,
1258
+ getReplacement(match, this.original),
1259
+ );
1260
+ }
1261
+ return this;
1262
+ }
1263
+
1264
+ _replaceString(string, replacement) {
1265
+ const { original } = this;
1266
+ const index = original.indexOf(string);
1267
+
1268
+ if (index !== -1) {
1269
+ this.overwrite(index, index + string.length, replacement);
1270
+ }
1271
+
1272
+ return this;
1273
+ }
1274
+
1275
+ replace(searchValue, replacement) {
1276
+ if (typeof searchValue === 'string') {
1277
+ return this._replaceString(searchValue, replacement);
1278
+ }
1279
+
1280
+ return this._replaceRegexp(searchValue, replacement);
1281
+ }
1282
+
1283
+ _replaceAllString(string, replacement) {
1284
+ const { original } = this;
1285
+ const stringLength = string.length;
1286
+ for (
1287
+ let index = original.indexOf(string);
1288
+ index !== -1;
1289
+ index = original.indexOf(string, index + stringLength)
1290
+ ) {
1291
+ this.overwrite(index, index + stringLength, replacement);
1292
+ }
1293
+
1294
+ return this;
1295
+ }
1296
+
1297
+ replaceAll(searchValue, replacement) {
1298
+ if (typeof searchValue === 'string') {
1299
+ return this._replaceAllString(searchValue, replacement);
1300
+ }
1301
+
1302
+ if (!searchValue.global) {
1303
+ throw new TypeError(
1304
+ 'MagicString.prototype.replaceAll called with a non-global RegExp argument',
1305
+ );
1306
+ }
1307
+
1308
+ return this._replaceRegexp(searchValue, replacement);
1309
+ }
1310
+ }
1311
+
1312
+ /**@target web */
1313
+ // index.ts for more details on contents and license of this file
1314
+ // https://github.com/vitejs/vite/blob/b7ddfae5f852c2948fab03e94751ce56f5f31ce0/packages/vite/src/node/utils.ts#L1302
1315
+ function evalValue(rawValue) {
1316
+ const fn = new Function(`
1317
+ var console, exports, global, module, process, require
1318
+ return (\n${rawValue}\n)
1319
+ `);
1320
+ return fn();
1321
+ }
1322
+ // https://github.com/vitejs/vite/blob/b7ddfae5f852c2948fab03e94751ce56f5f31ce0/packages/vite/src/shared/utils.ts#L31-L34
1323
+ const postfixRE = /[?#].*$/;
1324
+ function cleanUrl(url) {
1325
+ return url.replace(postfixRE, '');
1326
+ }
1327
+ // https://github.com/vitejs/vite/blob/b7ddfae5f852c2948fab03e94751ce56f5f31ce0/packages/vite/src/node/utils.ts#L393
1328
+ function tryStatSync(file) {
1329
+ try {
1330
+ // The "throwIfNoEntry" is a performance optimization for cases where the file does not exist
1331
+ return fs.statSync(file, { throwIfNoEntry: false });
1332
+ }
1333
+ catch {
1334
+ // Ignore errors
1335
+ }
1336
+ return;
1337
+ }
1338
+ // https://github.com/vitejs/vite/blob/b7ddfae5f852c2948fab03e94751ce56f5f31ce0/packages/vite/src/node/utils.ts#L1030
1339
+ function getHash(text, length = 8) {
1340
+ const h = createHash('sha256').update(text).digest('hex').substring(0, length);
1341
+ if (length <= 64)
1342
+ return h;
1343
+ return h.padEnd(length, '_');
1344
+ }
1345
+ // https://github.com/vitejs/vite/blob/b7ddfae5f852c2948fab03e94751ce56f5f31ce0/packages/vite/src/shared/utils.ts#L40
1346
+ function withTrailingSlash(path) {
1347
+ if (path[path.length - 1] !== '/') {
1348
+ return `${path}/`;
1349
+ }
1350
+ return path;
1351
+ }
1352
+ // https://github.com/vitejs/vite/blob/b7ddfae5f852c2948fab03e94751ce56f5f31ce0/packages/vite/src/node/utils.ts#L1268
1353
+ function joinUrlSegments(a, b) {
1354
+ if (!a || !b) {
1355
+ return a || b || '';
1356
+ }
1357
+ if (a[a.length - 1] === '/') {
1358
+ a = a.substring(0, a.length - 1);
1359
+ }
1360
+ if (b[0] !== '/') {
1361
+ b = '/' + b;
1362
+ }
1363
+ return a + b;
1364
+ }
1365
+ // https://github.com/vitejs/vite/blob/b7ddfae5f852c2948fab03e94751ce56f5f31ce0/packages/vite/src/node/utils.ts#L1281
1366
+ function removeLeadingSlash(str) {
1367
+ return str[0] === '/' ? str.slice(1) : str;
1368
+ }
1369
+ // https://github.com/vitejs/vite/blob/b7ddfae5f852c2948fab03e94751ce56f5f31ce0/packages/vite/src/node/utils.ts#L319
1370
+ function injectQuery(builtUrl, query) {
1371
+ const queryIndex = builtUrl.indexOf('?');
1372
+ return builtUrl + (queryIndex === -1 ? '?' : '&') + query;
1373
+ }
1374
+ // https://github.com/vitejs/vite/blob/b7ddfae5f852c2948fab03e94751ce56f5f31ce0/packages/vite/src/node/utils.ts#L1435
1375
+ function partialEncodeURIPath(uri) {
1376
+ if (uri.startsWith('data:'))
1377
+ return uri;
1378
+ const filePath = cleanUrl(uri);
1379
+ const postfix = filePath !== uri ? uri.slice(filePath.length) : '';
1380
+ return filePath.replaceAll('%', '%25') + postfix;
1381
+ }
1382
+ // https://github.com/vitejs/vite/blob/b7ddfae5f852c2948fab03e94751ce56f5f31ce0/packages/vite/src/node/utils.ts#L1424
1383
+ function encodeURIPath(uri) {
1384
+ if (uri.startsWith('data:'))
1385
+ return uri;
1386
+ const filePath = cleanUrl(uri);
1387
+ const postfix = filePath !== uri ? uri.slice(filePath.length) : '';
1388
+ return encodeURI(filePath) + postfix;
1389
+ }
1390
+
1391
+ /**@target web */
1392
+ const FS_PREFIX = `/@fs/`;
1393
+ async function fileToUrl(id, config) {
1394
+ let rtn;
1395
+ if (id.startsWith(withTrailingSlash(config.root))) {
1396
+ // in project root, infer short public path
1397
+ rtn = '/' + path.posix.relative(config.root, id);
1398
+ }
1399
+ else {
1400
+ // outside of project root, use absolute fs path
1401
+ // (this is special handled by the serve static middleware
1402
+ rtn = path.posix.join(FS_PREFIX, id);
1403
+ }
1404
+ const base = joinUrlSegments(config.server?.origin ?? '', config.base);
1405
+ return joinUrlSegments(base, removeLeadingSlash(rtn));
1406
+ }
1407
+
1408
+ /**@target web */
1409
+ const needsEscapeRegEx = /[\n\r'\\\u2028\u2029]/;
1410
+ const quoteNewlineRegEx = /([\n\r'\u2028\u2029])/g;
1411
+ const backSlashRegEx = /\\/g;
1412
+ function escapeId(id) {
1413
+ if (!needsEscapeRegEx.test(id))
1414
+ return id;
1415
+ return id.replace(backSlashRegEx, '\\\\').replace(quoteNewlineRegEx, '\\$1');
1416
+ }
1417
+ const getResolveUrl = (path, URL = 'URL') => `new ${URL}(${path}).href`;
1418
+ const getRelativeUrlFromDocument = (relativePath, umd = false) => getResolveUrl(`'${escapeId(partialEncodeURIPath(relativePath))}', ${umd ? `typeof document === 'undefined' ? location.href : ` : ''}document.currentScript && document.currentScript.src || document.baseURI`);
1419
+ const getFileUrlFromFullPath = (path) => `require('u' + 'rl').pathToFileURL(${path}).href`;
1420
+ const getFileUrlFromRelativePath = (path) => getFileUrlFromFullPath(`__dirname + '/${escapeId(path)}'`);
1421
+ const relativeUrlMechanisms = {
1422
+ amd: relativePath => {
1423
+ if (relativePath[0] !== '.')
1424
+ relativePath = './' + relativePath;
1425
+ return getResolveUrl(`require.toUrl('${escapeId(relativePath)}'), document.baseURI`);
1426
+ },
1427
+ cjs: relativePath => `(typeof document === 'undefined' ? ${getFileUrlFromRelativePath(relativePath)} : ${getRelativeUrlFromDocument(relativePath)})`,
1428
+ es: relativePath => getResolveUrl(`'${escapeId(partialEncodeURIPath(relativePath))}', import.meta.url`),
1429
+ iife: relativePath => getRelativeUrlFromDocument(relativePath),
1430
+ // NOTE: make sure rollup generate `module` params
1431
+ system: relativePath => getResolveUrl(`'${escapeId(partialEncodeURIPath(relativePath))}', module.meta.url`),
1432
+ umd: relativePath => `(typeof document === 'undefined' && typeof location === 'undefined' ? ${getFileUrlFromRelativePath(relativePath)} : ${getRelativeUrlFromDocument(relativePath, true)})`
1433
+ };
1434
+ const customRelativeUrlMechanisms = {
1435
+ ...relativeUrlMechanisms,
1436
+ 'worker-iife': relativePath => getResolveUrl(`'${escapeId(partialEncodeURIPath(relativePath))}', self.location.href`)
1437
+ };
1438
+ function createToImportMetaURLBasedRelativeRuntime(format, isWorker) {
1439
+ const formatLong = isWorker && format === 'iife' ? 'worker-iife' : format;
1440
+ const toRelativePath = customRelativeUrlMechanisms[formatLong];
1441
+ return (filename, importer) => ({
1442
+ runtime: toRelativePath(path.posix.relative(path.dirname(importer), filename))
1443
+ });
1444
+ }
1445
+ function toOutputFilePathInJS(filename, type, hostId, hostType, config, toRelative) {
1446
+ const { renderBuiltUrl } = config.experimental;
1447
+ let relative = config.base === '' || config.base === './';
1448
+ if (renderBuiltUrl) {
1449
+ const result = renderBuiltUrl(filename, {
1450
+ hostId,
1451
+ hostType,
1452
+ type,
1453
+ ssr: !!config.build.ssr
1454
+ });
1455
+ if (typeof result === 'object') {
1456
+ if (result.runtime) {
1457
+ return { runtime: result.runtime };
1458
+ }
1459
+ if (typeof result.relative === 'boolean') {
1460
+ relative = result.relative;
1461
+ }
1462
+ }
1463
+ else if (result) {
1464
+ return result;
1465
+ }
1466
+ }
1467
+ if (relative && !config.build.ssr) {
1468
+ return toRelative(filename, hostId);
1469
+ }
1470
+ return joinUrlSegments(config.base, filename);
1471
+ }
1472
+
1473
+ /**@target web */
1474
+ // index.ts for more details on contents and license of this file
1475
+ // https://github.com/vitejs/vite/blob/b7ddfae5f852c2948fab03e94751ce56f5f31ce0/packages/vite/src/node/constants.ts#L143
1476
+ const METADATA_FILENAME = '_metadata.json';
1477
+ // https://github.com/vitejs/vite/blob/b7ddfae5f852c2948fab03e94751ce56f5f31ce0/packages/vite/src/node/constants.ts#L63
1478
+ const ENV_PUBLIC_PATH = `/@vite/env`;
1479
+
1480
+ /**@target web */
1481
+ // index.ts for more details on contents and license of this file
1482
+ // https://github.com/vitejs/vite/blob/b7ddfae5f852c2948fab03e94751ce56f5f31ce0/packages/vite/src/node/fsUtils.ts#L388
1483
+ function tryResolveRealFile(file, preserveSymlinks) {
1484
+ const fileStat = tryStatSync(file);
1485
+ if (fileStat?.isFile()) {
1486
+ return file;
1487
+ }
1488
+ else if (fileStat?.isSymbolicLink()) {
1489
+ return preserveSymlinks ? file : fs.realpathSync(file);
1490
+ }
1491
+ return undefined;
1492
+ }
1493
+
1494
+ /**@target web */
1495
+ // index.ts for more details on contents and license of this file
1496
+ // https://github.com/vitejs/vite/blob/b7ddfae5f852c2948fab03e94751ce56f5f31ce0/packages/vite/src/node/plugins/resolve.ts#L534
1497
+ function splitFileAndPostfix(path) {
1498
+ const file = cleanUrl(path);
1499
+ return { file, postfix: path.slice(file.length) };
1500
+ }
1501
+ // https://github.com/vitejs/vite/blob/b7ddfae5f852c2948fab03e94751ce56f5f31ce0/packages/vite/src/node/plugins/resolve.ts#L566-L574
1502
+ function tryFsResolve(fsPath, preserveSymlinks) {
1503
+ const { file, postfix } = splitFileAndPostfix(fsPath);
1504
+ const res = tryCleanFsResolve(file, preserveSymlinks);
1505
+ if (res)
1506
+ return res + postfix;
1507
+ return;
1508
+ }
1509
+ // https://github.com/vitejs/vite/blob/b7ddfae5f852c2948fab03e94751ce56f5f31ce0/packages/vite/src/node/plugins/resolve.ts#L580
1510
+ function tryCleanFsResolve(file, preserveSymlinks) {
1511
+ if (file.includes('node_modules')) {
1512
+ return tryResolveRealFile(file, preserveSymlinks);
1513
+ }
1514
+ const normalizedResolved = tryResolveRealFile(normalizePath(file));
1515
+ if (!normalizedResolved) {
1516
+ return tryResolveRealFile(file, preserveSymlinks);
1517
+ }
1518
+ return normalizedResolved;
1519
+ }
1520
+
1521
+ /**@target web */
1522
+ // index.ts for more details on contents and license of this file
1523
+ // https://github.com/Danielku15/vite/blob/88b7def341f12d07d7d4f83cbe3dc73cc8c6b7be/packages/vite/src/node/optimizer/index.ts#L1356
1524
+ function tryOptimizedDepResolve(config, ssr, url, depId, preserveSymlinks) {
1525
+ const optimizer = getDepsOptimizer(config, ssr);
1526
+ if (optimizer?.isOptimizedDepFile(depId)) {
1527
+ const depFile = cleanUrl(depId);
1528
+ const info = optimizedDepInfoFromFile(optimizer.metadata, depFile);
1529
+ const depSrc = info?.src;
1530
+ if (depSrc) {
1531
+ const resolvedFile = path.resolve(path.dirname(depSrc), url);
1532
+ return tryFsResolve(resolvedFile, preserveSymlinks);
1533
+ }
1534
+ }
1535
+ return undefined;
1536
+ }
1537
+ // https://github.com/Danielku15/vite/blob/88b7def341f12d07d7d4f83cbe3dc73cc8c6b7be/packages/vite/src/node/optimizer/optimizer.ts#L32-L40
1538
+ const depsOptimizerMap = new WeakMap();
1539
+ const devSsrDepsOptimizerMap = new WeakMap();
1540
+ function getDepsOptimizer(config, ssr) {
1541
+ const map = ssr ? devSsrDepsOptimizerMap : depsOptimizerMap;
1542
+ let optimizer = map.get(config);
1543
+ if (!optimizer) {
1544
+ optimizer = createDepsOptimizer(config);
1545
+ map.set(config, optimizer);
1546
+ }
1547
+ return optimizer;
1548
+ }
1549
+ // https://github.com/vitejs/vite/blob/b7ddfae5f852c2948fab03e94751ce56f5f31ce0/packages/vite/src/node/optimizer/optimizer.ts#L79
1550
+ function createDepsOptimizer(config) {
1551
+ const depsCacheDirPrefix = normalizePath(path.resolve(config.cacheDir, 'deps'));
1552
+ const metadata = parseDepsOptimizerMetadata(fs.readFileSync(path.join(depsCacheDirPrefix, METADATA_FILENAME), 'utf8'), depsCacheDirPrefix);
1553
+ const notImplemented = () => {
1554
+ throw new Error('not implemented');
1555
+ };
1556
+ const depsOptimizer = {
1557
+ metadata,
1558
+ registerMissingImport: notImplemented,
1559
+ run: notImplemented,
1560
+ // https://github.com/vitejs/vite/blob/b7ddfae5f852c2948fab03e94751ce56f5f31ce0/packages/vite/src/node/optimizer/index.ts#L916
1561
+ isOptimizedDepFile: id => id.startsWith(depsCacheDirPrefix),
1562
+ isOptimizedDepUrl: notImplemented,
1563
+ getOptimizedDepId: notImplemented,
1564
+ close: notImplemented,
1565
+ options: {}
1566
+ };
1567
+ return depsOptimizer;
1568
+ }
1569
+ // https://github.com/vitejs/vite/blob/b7ddfae5f852c2948fab03e94751ce56f5f31ce0/packages/vite/src/node/optimizer/index.ts#L944
1570
+ function parseDepsOptimizerMetadata(jsonMetadata, depsCacheDir) {
1571
+ const { hash, lockfileHash, configHash, browserHash, optimized, chunks } = JSON.parse(jsonMetadata, (key, value) => {
1572
+ if (key === 'file' || key === 'src') {
1573
+ return normalizePath(path.resolve(depsCacheDir, value));
1574
+ }
1575
+ return value;
1576
+ });
1577
+ if (!chunks || Object.values(optimized).some(depInfo => !depInfo.fileHash)) {
1578
+ // outdated _metadata.json version, ignore
1579
+ return;
1580
+ }
1581
+ const metadata = {
1582
+ hash,
1583
+ lockfileHash,
1584
+ configHash,
1585
+ browserHash,
1586
+ optimized: {},
1587
+ discovered: {},
1588
+ chunks: {},
1589
+ depInfoList: []
1590
+ };
1591
+ for (const id of Object.keys(optimized)) {
1592
+ addOptimizedDepInfo(metadata, 'optimized', {
1593
+ ...optimized[id],
1594
+ id,
1595
+ browserHash
1596
+ });
1597
+ }
1598
+ for (const id of Object.keys(chunks)) {
1599
+ addOptimizedDepInfo(metadata, 'chunks', {
1600
+ ...chunks[id],
1601
+ id,
1602
+ browserHash,
1603
+ needsInterop: false
1604
+ });
1605
+ }
1606
+ return metadata;
1607
+ }
1608
+ // https://github.com/vitejs/vite/blob/b7ddfae5f852c2948fab03e94751ce56f5f31ce0/packages/vite/src/node/optimizer/index.ts#L322
1609
+ function addOptimizedDepInfo(metadata, type, depInfo) {
1610
+ metadata[type][depInfo.id] = depInfo;
1611
+ metadata.depInfoList.push(depInfo);
1612
+ return depInfo;
1613
+ }
1614
+ // https://github.com/vitejs/vite/blob/b7ddfae5f852c2948fab03e94751ce56f5f31ce0/packages/vite/src/node/optimizer/index.ts#L1248
1615
+ function optimizedDepInfoFromFile(metadata, file) {
1616
+ return metadata.depInfoList.find(depInfo => depInfo.file === file);
1617
+ }
1618
+
1619
+ /**@target web */
1620
+ const workerCache = new WeakMap();
1621
+ const WORKER_FILE_ID = 'alphatab_worker';
1622
+ const WORKER_ASSET_ID = '__ALPHATAB_WORKER_ASSET__';
1623
+ // https://github.com/vitejs/vite/blob/b7ddfae5f852c2948fab03e94751ce56f5f31ce0/packages/vite/src/node/plugins/worker.ts#L47
1624
+ function saveEmitWorkerAsset(config, asset) {
1625
+ const workerMap = workerCache.get(config.mainConfig || config);
1626
+ workerMap.assets.set(asset.fileName, asset);
1627
+ }
1628
+ // https://github.com/vitejs/vite/blob/b7ddfae5f852c2948fab03e94751ce56f5f31ce0/packages/vite/src/node/plugins/worker.ts#L161
1629
+ async function workerFileToUrl(config, id) {
1630
+ const workerMap = workerCache.get(config.mainConfig || config);
1631
+ let fileName = workerMap.bundle.get(id);
1632
+ if (!fileName) {
1633
+ const outputChunk = await bundleWorkerEntry(config, id);
1634
+ fileName = outputChunk.fileName;
1635
+ saveEmitWorkerAsset(config, {
1636
+ fileName,
1637
+ source: outputChunk.code
1638
+ });
1639
+ workerMap.bundle.set(id, fileName);
1640
+ }
1641
+ return encodeWorkerAssetFileName(fileName, workerMap);
1642
+ }
1643
+ // https://github.com/vitejs/vite/blob/b7ddfae5f852c2948fab03e94751ce56f5f31ce0/packages/vite/src/node/plugins/worker.ts#L149
1644
+ function encodeWorkerAssetFileName(fileName, workerCache) {
1645
+ const { fileNameHash } = workerCache;
1646
+ const hash = getHash(fileName);
1647
+ if (!fileNameHash.get(hash)) {
1648
+ fileNameHash.set(hash, fileName);
1649
+ }
1650
+ return `${WORKER_ASSET_ID}${hash}__`;
1651
+ }
1652
+ // https://github.com/vitejs/vite/blob/b7ddfae5f852c2948fab03e94751ce56f5f31ce0/packages/vite/src/node/plugins/worker.ts#L55
1653
+ async function bundleWorkerEntry(config, id) {
1654
+ const input = cleanUrl(id);
1655
+ const bundleChain = config.bundleChain ?? [];
1656
+ const newBundleChain = [...bundleChain, input];
1657
+ if (bundleChain.includes(input)) {
1658
+ throw new Error('Circular worker imports detected. Vite does not support it. ' +
1659
+ `Import chain: ${newBundleChain.join(' -> ')}`);
1660
+ }
1661
+ // bundle the file as entry to support imports
1662
+ const { rollup } = await import('rollup');
1663
+ const { plugins, rollupOptions, format } = config.worker;
1664
+ const bundle = await rollup({
1665
+ ...rollupOptions,
1666
+ input,
1667
+ plugins: await plugins(newBundleChain),
1668
+ preserveEntrySignatures: false
1669
+ });
1670
+ let chunk;
1671
+ try {
1672
+ const workerOutputConfig = config.worker.rollupOptions.output;
1673
+ const workerConfig = workerOutputConfig
1674
+ ? Array.isArray(workerOutputConfig)
1675
+ ? workerOutputConfig[0] || {}
1676
+ : workerOutputConfig
1677
+ : {};
1678
+ const { output: [outputChunk, ...outputChunks] } = await bundle.generate({
1679
+ entryFileNames: path.posix.join(config.build.assetsDir, '[name]-[hash].js'),
1680
+ chunkFileNames: path.posix.join(config.build.assetsDir, '[name]-[hash].js'),
1681
+ assetFileNames: path.posix.join(config.build.assetsDir, '[name]-[hash].[ext]'),
1682
+ ...workerConfig,
1683
+ format,
1684
+ sourcemap: config.build.sourcemap
1685
+ });
1686
+ chunk = outputChunk;
1687
+ outputChunks.forEach(outputChunk => {
1688
+ if (outputChunk.type === 'asset') {
1689
+ saveEmitWorkerAsset(config, outputChunk);
1690
+ }
1691
+ else if (outputChunk.type === 'chunk') {
1692
+ saveEmitWorkerAsset(config, {
1693
+ fileName: outputChunk.fileName,
1694
+ source: outputChunk.code
1695
+ });
1696
+ }
1697
+ });
1698
+ }
1699
+ finally {
1700
+ await bundle.close();
1701
+ }
1702
+ return emitSourcemapForWorkerEntry(config, chunk);
1703
+ }
1704
+ // https://github.com/vitejs/vite/blob/b7ddfae5f852c2948fab03e94751ce56f5f31ce0/packages/vite/src/node/plugins/worker.ts#L124
1705
+ function emitSourcemapForWorkerEntry(config, chunk) {
1706
+ const { map: sourcemap } = chunk;
1707
+ if (sourcemap) {
1708
+ if (config.build.sourcemap === 'hidden' || config.build.sourcemap === true) {
1709
+ const data = sourcemap.toString();
1710
+ const mapFileName = chunk.fileName + '.map';
1711
+ saveEmitWorkerAsset(config, {
1712
+ fileName: mapFileName,
1713
+ source: data
1714
+ });
1715
+ }
1716
+ }
1717
+ return chunk;
1718
+ }
1719
+ // https://github.com/vitejs/vite/blob/b7ddfae5f852c2948fab03e94751ce56f5f31ce0/packages/vite/src/node/plugins/worker.ts#L458
1720
+ function isSameContent(a, b) {
1721
+ if (typeof a === 'string') {
1722
+ if (typeof b === 'string') {
1723
+ return a === b;
1724
+ }
1725
+ return Buffer.from(a).equals(b);
1726
+ }
1727
+ return Buffer.from(b).equals(a);
1728
+ }
1729
+
1730
+ /**@target web */
1731
+ const alphaTabWorkerPatterns = [
1732
+ ['alphaTabWorker', 'new URL', 'import.meta.url'],
1733
+ ['alphaTabWorklet.addModule', 'new URL', 'import.meta.url']
1734
+ ];
1735
+ function includesAlphaTabWorker(code) {
1736
+ for (const pattern of alphaTabWorkerPatterns) {
1737
+ let position = 0;
1738
+ for (const match of pattern) {
1739
+ position = code.indexOf(match, position);
1740
+ if (position === -1) {
1741
+ break;
1742
+ }
1743
+ }
1744
+ if (position !== -1) {
1745
+ return true;
1746
+ }
1747
+ }
1748
+ return false;
1749
+ }
1750
+ function getWorkerType(code, match) {
1751
+ if (match[1].includes('.addModule')) {
1752
+ return "audio_worklet" /* AlphaTabWorkerTypes.AudioWorklet */;
1753
+ }
1754
+ const endOfMatch = match.indices[0][1];
1755
+ const startOfOptions = code.indexOf('{', endOfMatch);
1756
+ if (startOfOptions === -1) {
1757
+ return "worker_classic" /* AlphaTabWorkerTypes.WorkerClassic */;
1758
+ }
1759
+ const endOfOptions = code.indexOf('}', endOfMatch);
1760
+ if (endOfOptions === -1) {
1761
+ return "worker_classic" /* AlphaTabWorkerTypes.WorkerClassic */;
1762
+ }
1763
+ const endOfWorkerCreate = code.indexOf(')', endOfMatch);
1764
+ if (startOfOptions > endOfWorkerCreate || endOfOptions > endOfWorkerCreate) {
1765
+ return "worker_classic" /* AlphaTabWorkerTypes.WorkerClassic */;
1766
+ }
1767
+ let workerOptions = code.slice(startOfOptions, endOfOptions + 1);
1768
+ try {
1769
+ workerOptions = evalValue(workerOptions);
1770
+ }
1771
+ catch (e) {
1772
+ return "worker_classic" /* AlphaTabWorkerTypes.WorkerClassic */;
1773
+ }
1774
+ if (typeof workerOptions === 'object' && workerOptions?.type === 'module') {
1775
+ return "worker_module" /* AlphaTabWorkerTypes.WorkerModule */;
1776
+ }
1777
+ return "worker_classic" /* AlphaTabWorkerTypes.WorkerClassic */;
1778
+ }
1779
+ function importMetaUrlPlugin(options) {
1780
+ let resolvedConfig;
1781
+ let isBuild;
1782
+ let preserveSymlinks;
1783
+ const isWorkerActive = options.webWorkers !== false;
1784
+ const isWorkletActive = options.audioWorklets !== false;
1785
+ const isActive = isWorkerActive || isWorkletActive;
1786
+ return {
1787
+ name: 'vite-plugin-alphatab-url',
1788
+ enforce: 'pre',
1789
+ configResolved(config) {
1790
+ resolvedConfig = config;
1791
+ isBuild = config.command === 'build';
1792
+ preserveSymlinks = config.resolve.preserveSymlinks;
1793
+ },
1794
+ shouldTransformCachedModule({ code }) {
1795
+ if (isActive && isBuild && resolvedConfig.build.watch && includesAlphaTabWorker(code)) {
1796
+ return true;
1797
+ }
1798
+ return;
1799
+ },
1800
+ async transform(code, id, options) {
1801
+ if (!isActive || options?.ssr || !includesAlphaTabWorker(code)) {
1802
+ return;
1803
+ }
1804
+ let s;
1805
+ const alphaTabWorkerPattern = /\b(alphaTabWorker|alphaTabWorklet\.addModule)\s*\(\s*(new\s+URL\s*\(\s*('[^']+'|"[^"]+"|`[^`]+`)\s*,\s*import\.meta\.url\s*\))/dg;
1806
+ let match;
1807
+ while ((match = alphaTabWorkerPattern.exec(code))) {
1808
+ const workerType = getWorkerType(code, match);
1809
+ let typeActive = false;
1810
+ switch (workerType) {
1811
+ case "worker_classic" /* AlphaTabWorkerTypes.WorkerClassic */:
1812
+ case "worker_module" /* AlphaTabWorkerTypes.WorkerModule */:
1813
+ typeActive = isWorkerActive;
1814
+ break;
1815
+ case "audio_worklet" /* AlphaTabWorkerTypes.AudioWorklet */:
1816
+ typeActive = isWorkletActive;
1817
+ break;
1818
+ }
1819
+ if (!typeActive) {
1820
+ continue;
1821
+ }
1822
+ s ?? (s = new MagicString(code));
1823
+ const url = code.slice(match.indices[3][0] + 1, match.indices[3][1] - 1);
1824
+ let file = path__default.resolve(path__default.dirname(id), url);
1825
+ file =
1826
+ tryFsResolve(file, preserveSymlinks) ??
1827
+ tryOptimizedDepResolve(resolvedConfig, options?.ssr === true, url, id, preserveSymlinks) ??
1828
+ file;
1829
+ let builtUrl;
1830
+ if (isBuild) {
1831
+ builtUrl = await workerFileToUrl(resolvedConfig, file);
1832
+ }
1833
+ else {
1834
+ builtUrl = await fileToUrl(cleanUrl(file), resolvedConfig);
1835
+ builtUrl = injectQuery(builtUrl, `${WORKER_FILE_ID}&type=${workerType}`);
1836
+ }
1837
+ s.update(match.indices[3][0], match.indices[3][1],
1838
+ // add `'' +` to skip vite:asset-import-meta-url plugin
1839
+ `new URL('' + ${JSON.stringify(builtUrl)}, import.meta.url)`);
1840
+ }
1841
+ if (s) {
1842
+ return {
1843
+ code: s.toString(),
1844
+ map: resolvedConfig.command === 'build' && resolvedConfig.build.sourcemap
1845
+ ? s.generateMap({ hires: 'boundary', source: id })
1846
+ : null
1847
+ };
1848
+ }
1849
+ return null;
1850
+ }
1851
+ };
1852
+ }
1853
+
1854
+ /**@target web */
1855
+ function copyAssetsPlugin(options) {
1856
+ let resolvedConfig;
1857
+ let output = false;
1858
+ return {
1859
+ name: 'vite-plugin-alphatab-copy',
1860
+ enforce: 'pre',
1861
+ configResolved(config) {
1862
+ resolvedConfig = config;
1863
+ },
1864
+ buildEnd() {
1865
+ // reset for watch mode
1866
+ output = false;
1867
+ },
1868
+ async buildStart() {
1869
+ // run copy only once even if multiple bundles are generated
1870
+ if (output) {
1871
+ return;
1872
+ }
1873
+ output = true;
1874
+ let alphaTabSourceDir = options.alphaTabSourceDir;
1875
+ if (!alphaTabSourceDir) {
1876
+ alphaTabSourceDir = path.join(resolvedConfig.root, 'node_modules/@coderline/alphatab/dist/');
1877
+ }
1878
+ if (!alphaTabSourceDir ||
1879
+ !fs.promises.access(path.join(alphaTabSourceDir, 'alphaTab.mjs'), fs.constants.F_OK)) {
1880
+ resolvedConfig.logger.error('Could not find alphaTab, please ensure it is installed into node_modules or configure alphaTabSourceDir');
1881
+ return;
1882
+ }
1883
+ const outputPath = (options.assetOutputDir ?? resolvedConfig.publicDir);
1884
+ if (!outputPath) {
1885
+ return;
1886
+ }
1887
+ async function copyFiles(subdir) {
1888
+ const fullDir = path.join(alphaTabSourceDir, subdir);
1889
+ const files = await fs.promises.readdir(fullDir, {
1890
+ withFileTypes: true
1891
+ });
1892
+ await fs.promises.mkdir(path.join(outputPath, subdir), {
1893
+ recursive: true
1894
+ });
1895
+ await Promise.all(files
1896
+ .filter(f => f.isFile())
1897
+ .map(async (file) => {
1898
+ const sourceFilename = path.join(file.path, file.name);
1899
+ await fs.promises.copyFile(sourceFilename, path.join(outputPath, subdir, file.name));
1900
+ }));
1901
+ }
1902
+ await Promise.all([copyFiles('font'), copyFiles('soundfont')]);
1903
+ }
1904
+ };
1905
+ }
1906
+
1907
+ /**@target web */
1908
+ const workerFileRE = new RegExp(`(?:\\?|&)${WORKER_FILE_ID}&type=(\\w+)(?:&|$)`);
1909
+ const workerAssetUrlRE = new RegExp(`${WORKER_ASSET_ID}([a-z\\d]{8})__`, 'g');
1910
+ function workerPlugin(options) {
1911
+ let resolvedConfig;
1912
+ let isBuild;
1913
+ let isWorker;
1914
+ const isWorkerActive = options.webWorkers !== false;
1915
+ const isWorkletActive = options.audioWorklets !== false;
1916
+ const isActive = isWorkerActive || isWorkletActive;
1917
+ return {
1918
+ name: 'vite-plugin-alphatab-worker',
1919
+ configResolved(config) {
1920
+ resolvedConfig = config;
1921
+ isBuild = config.command === 'build';
1922
+ isWorker = config.isWorker;
1923
+ },
1924
+ buildStart() {
1925
+ if (!isActive || isWorker) {
1926
+ return;
1927
+ }
1928
+ workerCache.set(resolvedConfig, {
1929
+ assets: new Map(),
1930
+ bundle: new Map(),
1931
+ fileNameHash: new Map()
1932
+ });
1933
+ },
1934
+ load(id) {
1935
+ if (isActive && isBuild && id.includes(WORKER_FILE_ID)) {
1936
+ return '';
1937
+ }
1938
+ return;
1939
+ },
1940
+ shouldTransformCachedModule({ id }) {
1941
+ if (isActive && isBuild && resolvedConfig.build.watch && id.includes(WORKER_FILE_ID)) {
1942
+ return true;
1943
+ }
1944
+ return;
1945
+ },
1946
+ async transform(raw, id) {
1947
+ if (!isActive) {
1948
+ return;
1949
+ }
1950
+ const match = workerFileRE.exec(id);
1951
+ if (!match) {
1952
+ return;
1953
+ }
1954
+ // inject env to worker file, might be needed by imported scripts
1955
+ const envScriptPath = JSON.stringify(path.posix.join(resolvedConfig.base, ENV_PUBLIC_PATH));
1956
+ const workerType = match[1];
1957
+ let injectEnv = '';
1958
+ switch (workerType) {
1959
+ case "worker_classic" /* AlphaTabWorkerTypes.WorkerClassic */:
1960
+ injectEnv = `importScripts(${envScriptPath})\n`;
1961
+ break;
1962
+ case "worker_module" /* AlphaTabWorkerTypes.WorkerModule */:
1963
+ case "audio_worklet" /* AlphaTabWorkerTypes.AudioWorklet */:
1964
+ injectEnv = `import ${envScriptPath}\n`;
1965
+ break;
1966
+ }
1967
+ if (injectEnv) {
1968
+ const s = new MagicString(raw);
1969
+ s.prepend(injectEnv);
1970
+ return {
1971
+ code: s.toString(),
1972
+ map: s.generateMap({ hires: 'boundary' })
1973
+ };
1974
+ }
1975
+ return;
1976
+ },
1977
+ renderChunk(code, chunk, outputOptions) {
1978
+ // when building the worker URLs are replaced with some placeholders
1979
+ // here we replace those placeholders with the final file names respecting chunks
1980
+ let s;
1981
+ const result = () => {
1982
+ return (s && {
1983
+ code: s.toString(),
1984
+ map: resolvedConfig.build.sourcemap ? s.generateMap({ hires: 'boundary' }) : null
1985
+ });
1986
+ };
1987
+ workerAssetUrlRE.lastIndex = 0;
1988
+ if (workerAssetUrlRE.test(code)) {
1989
+ const toRelativeRuntime = createToImportMetaURLBasedRelativeRuntime(outputOptions.format, resolvedConfig.isWorker);
1990
+ let match;
1991
+ s = new MagicString(code);
1992
+ workerAssetUrlRE.lastIndex = 0;
1993
+ // Replace "__VITE_WORKER_ASSET__5aa0ddc0__" using relative paths
1994
+ const workerMap = workerCache.get(resolvedConfig.mainConfig || resolvedConfig);
1995
+ const { fileNameHash } = workerMap;
1996
+ while ((match = workerAssetUrlRE.exec(code))) {
1997
+ const [full, hash] = match;
1998
+ const filename = fileNameHash.get(hash);
1999
+ const replacement = toOutputFilePathInJS(filename, 'asset', chunk.fileName, 'js', resolvedConfig, toRelativeRuntime);
2000
+ const replacementString = typeof replacement === 'string'
2001
+ ? JSON.stringify(encodeURIPath(replacement)).slice(1, -1)
2002
+ : `"+${replacement.runtime}+"`;
2003
+ s.update(match.index, match.index + full.length, replacementString);
2004
+ }
2005
+ }
2006
+ return result();
2007
+ },
2008
+ generateBundle(_, bundle) {
2009
+ if (isWorker) {
2010
+ return;
2011
+ }
2012
+ const workerMap = workerCache.get(resolvedConfig);
2013
+ workerMap.assets.forEach(asset => {
2014
+ const duplicateAsset = bundle[asset.fileName];
2015
+ if (duplicateAsset) {
2016
+ const content = duplicateAsset.type === 'asset' ? duplicateAsset.source : duplicateAsset.code;
2017
+ // don't emit if the file name and the content is same
2018
+ if (isSameContent(content, asset.source)) {
2019
+ return;
2020
+ }
2021
+ }
2022
+ this.emitFile({
2023
+ type: 'asset',
2024
+ fileName: asset.fileName,
2025
+ source: asset.source
2026
+ });
2027
+ });
2028
+ workerMap.assets.clear();
2029
+ }
2030
+ };
2031
+ }
2032
+
2033
+ /**@target web */
2034
+ function alphaTab(options) {
2035
+ const plugins = [];
2036
+ options ?? (options = {});
2037
+ plugins.push(importMetaUrlPlugin(options));
2038
+ plugins.push(workerPlugin(options));
2039
+ plugins.push(copyAssetsPlugin(options));
2040
+ return plugins;
2041
+ }
2042
+
2043
+ export { alphaTab };
2044
+ //# sourceMappingURL=alphaTab.vite.mjs.map