@coderline/alphatab-vite 1.7.0 → 1.7.1

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