@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
@@ -0,0 +1,201 @@
1
+ /*!
2
+ * alphaTab Vite Plugin v1.7.1 (, build 24)
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 path = require('node:path');
42
+ const MagicString = require('magic-string');
43
+ const bridge_asset = require('./bridge/asset.cjs');
44
+ const bridge_utils = require('./bridge/utils.cjs');
45
+ const bridge_optimizer = require('./bridge/optimizer.cjs');
46
+ const bridge_resolve = require('./bridge/resolve.cjs');
47
+ const bridge_worker = require('./bridge/worker.cjs');
48
+
49
+ // This file contains a customized and adapted version of the Vite built-in workerImportMetaUrl plugin
50
+ // https://github.com/vitejs/vite/blob/main/packages/vite/src/node/plugins/workerImportMetaUrl.ts
51
+ // The main adaptions are:
52
+ // - Custom syntax detection for workers and audio worklets
53
+ // - Custom worker types and worker URLs to not overlap with the original plugin
54
+ // With https://github.com/vitejs/vite/pull/16422 integrated this custom code should not be needed anymore
55
+ // Original Sources Licensed under:
56
+ // MIT License
57
+ // Copyright (c) 2019-present, Yuxi (Evan) You and Vite contributors
58
+ // Permission is hereby granted, free of charge, to any person obtaining a copy
59
+ // of this software and associated documentation files (the "Software"), to deal
60
+ // in the Software without restriction, including without limitation the rights
61
+ // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
62
+ // copies of the Software, and to permit persons to whom the Software is
63
+ // furnished to do so, subject to the following conditions:
64
+ // The above copyright notice and this permission notice shall be included in all
65
+ // copies or substantial portions of the Software.
66
+ // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
67
+ // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
68
+ // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
69
+ // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
70
+ // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
71
+ // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
72
+ // SOFTWARE.
73
+ const alphaTabWorkerPatterns = [
74
+ ['alphaTabWorker', 'new', 'alphaTabUrl', 'import.meta.url'],
75
+ ['alphaTabWorklet.addModule', 'new', 'alphaTabUrl', 'import.meta.url']
76
+ ];
77
+ function includesAlphaTabWorker(code) {
78
+ for (const pattern of alphaTabWorkerPatterns) {
79
+ let position = 0;
80
+ for (const match of pattern) {
81
+ position = code.indexOf(match, position);
82
+ if (position === -1) {
83
+ break;
84
+ }
85
+ }
86
+ if (position !== -1) {
87
+ return true;
88
+ }
89
+ }
90
+ return false;
91
+ }
92
+ function getWorkerType(code, match) {
93
+ if (match[1].includes('.addModule')) {
94
+ return bridge_worker.AlphaTabWorkerTypes.AudioWorklet;
95
+ }
96
+ const endOfMatch = match.indices[0][1];
97
+ const startOfOptions = code.indexOf('{', endOfMatch);
98
+ if (startOfOptions === -1) {
99
+ return bridge_worker.AlphaTabWorkerTypes.WorkerClassic;
100
+ }
101
+ const endOfOptions = code.indexOf('}', endOfMatch);
102
+ if (endOfOptions === -1) {
103
+ return bridge_worker.AlphaTabWorkerTypes.WorkerClassic;
104
+ }
105
+ const endOfWorkerCreate = code.indexOf(')', endOfMatch);
106
+ if (startOfOptions > endOfWorkerCreate || endOfOptions > endOfWorkerCreate) {
107
+ return bridge_worker.AlphaTabWorkerTypes.WorkerClassic;
108
+ }
109
+ let workerOptions = code.slice(startOfOptions, endOfOptions + 1);
110
+ try {
111
+ workerOptions = bridge_utils.evalValue(workerOptions);
112
+ }
113
+ catch {
114
+ return bridge_worker.AlphaTabWorkerTypes.WorkerClassic;
115
+ }
116
+ if (typeof workerOptions === 'object' && workerOptions?.type === 'module') {
117
+ return bridge_worker.AlphaTabWorkerTypes.WorkerModule;
118
+ }
119
+ return bridge_worker.AlphaTabWorkerTypes.WorkerClassic;
120
+ }
121
+ /**
122
+ * @public
123
+ */
124
+ function importMetaUrlPlugin(options) {
125
+ let resolvedConfig;
126
+ let isBuild;
127
+ let preserveSymlinks;
128
+ const isWorkerActive = options.webWorkers !== false;
129
+ const isWorkletActive = options.audioWorklets !== false;
130
+ const isActive = isWorkerActive || isWorkletActive;
131
+ return {
132
+ name: 'vite-plugin-alphatab-url',
133
+ enforce: 'pre',
134
+ configResolved(config) {
135
+ resolvedConfig = config;
136
+ isBuild = config.command === 'build';
137
+ preserveSymlinks = config.resolve.preserveSymlinks;
138
+ },
139
+ shouldTransformCachedModule({ code }) {
140
+ if (isActive && isBuild && resolvedConfig.build.watch && includesAlphaTabWorker(code)) {
141
+ return true;
142
+ }
143
+ return;
144
+ },
145
+ async transform(code, id, options) {
146
+ if (!isActive || options?.ssr || !includesAlphaTabWorker(code)) {
147
+ return;
148
+ }
149
+ let s;
150
+ const alphaTabWorkerPattern = /\b(alphaTabWorker|alphaTabWorklet\.addModule)\s*\(\s*(new\s+[^ (]+alphaTabUrl\s*\(\s*('[^']+'|"[^"]+"|`[^`]+`)\s*,\s*import\.meta\.url\s*\))/dg;
151
+ let match = alphaTabWorkerPattern.exec(code);
152
+ while (match) {
153
+ const workerType = getWorkerType(code, match);
154
+ let typeActive = false;
155
+ switch (workerType) {
156
+ case bridge_worker.AlphaTabWorkerTypes.WorkerClassic:
157
+ case bridge_worker.AlphaTabWorkerTypes.WorkerModule:
158
+ typeActive = isWorkerActive;
159
+ break;
160
+ case bridge_worker.AlphaTabWorkerTypes.AudioWorklet:
161
+ typeActive = isWorkletActive;
162
+ break;
163
+ }
164
+ if (!typeActive) {
165
+ match = alphaTabWorkerPattern.exec(code);
166
+ continue;
167
+ }
168
+ s ??= new MagicString(code);
169
+ const url = code.slice(match.indices[3][0] + 1, match.indices[3][1] - 1);
170
+ let file = path.resolve(path.dirname(id), url);
171
+ file =
172
+ bridge_resolve.tryFsResolve(file, preserveSymlinks) ??
173
+ bridge_optimizer.tryOptimizedDepResolve(resolvedConfig, options?.ssr === true, url, id, preserveSymlinks) ??
174
+ file;
175
+ let builtUrl;
176
+ if (isBuild) {
177
+ builtUrl = await bridge_worker.workerFileToUrl(resolvedConfig, file);
178
+ }
179
+ else {
180
+ builtUrl = await bridge_asset.fileToUrl(bridge_utils.cleanUrl(file), resolvedConfig);
181
+ builtUrl = bridge_utils.injectQuery(builtUrl, `${bridge_worker.WORKER_FILE_ID}&type=${workerType}`);
182
+ }
183
+ s.update(match.indices[3][0], match.indices[3][1],
184
+ // add `'' +` to skip vite:asset-import-meta-url plugin
185
+ `new URL('' + ${JSON.stringify(builtUrl)}, import.meta.url)`);
186
+ match = alphaTabWorkerPattern.exec(code);
187
+ }
188
+ if (s) {
189
+ return {
190
+ code: s.toString(),
191
+ map: resolvedConfig.command === 'build' && resolvedConfig.build.sourcemap
192
+ ? s.generateMap({ hires: 'boundary', source: id })
193
+ : null
194
+ };
195
+ }
196
+ return null;
197
+ }
198
+ };
199
+ }
200
+
201
+ exports.importMetaUrlPlugin = importMetaUrlPlugin;
@@ -0,0 +1,5 @@
1
+ import type { AlphaTabVitePluginOptions } from './AlphaTabVitePluginOptions';
2
+ /**
3
+ * @public
4
+ */
5
+ export declare function importMetaUrlPlugin(options: AlphaTabVitePluginOptions): Plugin;
@@ -0,0 +1,197 @@
1
+ /*!
2
+ * alphaTab Vite Plugin v1.7.1 (, build 24)
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
+ import path__default from 'node:path';
38
+ import MagicString from 'magic-string';
39
+ import { fileToUrl } from './bridge/asset.mjs';
40
+ import { cleanUrl, injectQuery, evalValue } from './bridge/utils.mjs';
41
+ import { tryOptimizedDepResolve } from './bridge/optimizer.mjs';
42
+ import { tryFsResolve } from './bridge/resolve.mjs';
43
+ import { AlphaTabWorkerTypes, workerFileToUrl, WORKER_FILE_ID } from './bridge/worker.mjs';
44
+
45
+ // This file contains a customized and adapted version of the Vite built-in workerImportMetaUrl plugin
46
+ // https://github.com/vitejs/vite/blob/main/packages/vite/src/node/plugins/workerImportMetaUrl.ts
47
+ // The main adaptions are:
48
+ // - Custom syntax detection for workers and audio worklets
49
+ // - Custom worker types and worker URLs to not overlap with the original plugin
50
+ // With https://github.com/vitejs/vite/pull/16422 integrated this custom code should not be needed anymore
51
+ // Original Sources Licensed under:
52
+ // MIT License
53
+ // Copyright (c) 2019-present, Yuxi (Evan) You and Vite contributors
54
+ // Permission is hereby granted, free of charge, to any person obtaining a copy
55
+ // of this software and associated documentation files (the "Software"), to deal
56
+ // in the Software without restriction, including without limitation the rights
57
+ // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
58
+ // copies of the Software, and to permit persons to whom the Software is
59
+ // furnished to do so, subject to the following conditions:
60
+ // The above copyright notice and this permission notice shall be included in all
61
+ // copies or substantial portions of the Software.
62
+ // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
63
+ // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
64
+ // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
65
+ // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
66
+ // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
67
+ // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
68
+ // SOFTWARE.
69
+ const alphaTabWorkerPatterns = [
70
+ ['alphaTabWorker', 'new', 'alphaTabUrl', 'import.meta.url'],
71
+ ['alphaTabWorklet.addModule', 'new', 'alphaTabUrl', 'import.meta.url']
72
+ ];
73
+ function includesAlphaTabWorker(code) {
74
+ for (const pattern of alphaTabWorkerPatterns) {
75
+ let position = 0;
76
+ for (const match of pattern) {
77
+ position = code.indexOf(match, position);
78
+ if (position === -1) {
79
+ break;
80
+ }
81
+ }
82
+ if (position !== -1) {
83
+ return true;
84
+ }
85
+ }
86
+ return false;
87
+ }
88
+ function getWorkerType(code, match) {
89
+ if (match[1].includes('.addModule')) {
90
+ return AlphaTabWorkerTypes.AudioWorklet;
91
+ }
92
+ const endOfMatch = match.indices[0][1];
93
+ const startOfOptions = code.indexOf('{', endOfMatch);
94
+ if (startOfOptions === -1) {
95
+ return AlphaTabWorkerTypes.WorkerClassic;
96
+ }
97
+ const endOfOptions = code.indexOf('}', endOfMatch);
98
+ if (endOfOptions === -1) {
99
+ return AlphaTabWorkerTypes.WorkerClassic;
100
+ }
101
+ const endOfWorkerCreate = code.indexOf(')', endOfMatch);
102
+ if (startOfOptions > endOfWorkerCreate || endOfOptions > endOfWorkerCreate) {
103
+ return AlphaTabWorkerTypes.WorkerClassic;
104
+ }
105
+ let workerOptions = code.slice(startOfOptions, endOfOptions + 1);
106
+ try {
107
+ workerOptions = evalValue(workerOptions);
108
+ }
109
+ catch {
110
+ return AlphaTabWorkerTypes.WorkerClassic;
111
+ }
112
+ if (typeof workerOptions === 'object' && workerOptions?.type === 'module') {
113
+ return AlphaTabWorkerTypes.WorkerModule;
114
+ }
115
+ return AlphaTabWorkerTypes.WorkerClassic;
116
+ }
117
+ /**
118
+ * @public
119
+ */
120
+ function importMetaUrlPlugin(options) {
121
+ let resolvedConfig;
122
+ let isBuild;
123
+ let preserveSymlinks;
124
+ const isWorkerActive = options.webWorkers !== false;
125
+ const isWorkletActive = options.audioWorklets !== false;
126
+ const isActive = isWorkerActive || isWorkletActive;
127
+ return {
128
+ name: 'vite-plugin-alphatab-url',
129
+ enforce: 'pre',
130
+ configResolved(config) {
131
+ resolvedConfig = config;
132
+ isBuild = config.command === 'build';
133
+ preserveSymlinks = config.resolve.preserveSymlinks;
134
+ },
135
+ shouldTransformCachedModule({ code }) {
136
+ if (isActive && isBuild && resolvedConfig.build.watch && includesAlphaTabWorker(code)) {
137
+ return true;
138
+ }
139
+ return;
140
+ },
141
+ async transform(code, id, options) {
142
+ if (!isActive || options?.ssr || !includesAlphaTabWorker(code)) {
143
+ return;
144
+ }
145
+ let s;
146
+ const alphaTabWorkerPattern = /\b(alphaTabWorker|alphaTabWorklet\.addModule)\s*\(\s*(new\s+[^ (]+alphaTabUrl\s*\(\s*('[^']+'|"[^"]+"|`[^`]+`)\s*,\s*import\.meta\.url\s*\))/dg;
147
+ let match = alphaTabWorkerPattern.exec(code);
148
+ while (match) {
149
+ const workerType = getWorkerType(code, match);
150
+ let typeActive = false;
151
+ switch (workerType) {
152
+ case AlphaTabWorkerTypes.WorkerClassic:
153
+ case AlphaTabWorkerTypes.WorkerModule:
154
+ typeActive = isWorkerActive;
155
+ break;
156
+ case AlphaTabWorkerTypes.AudioWorklet:
157
+ typeActive = isWorkletActive;
158
+ break;
159
+ }
160
+ if (!typeActive) {
161
+ match = alphaTabWorkerPattern.exec(code);
162
+ continue;
163
+ }
164
+ s ??= new MagicString(code);
165
+ const url = code.slice(match.indices[3][0] + 1, match.indices[3][1] - 1);
166
+ let file = path__default.resolve(path__default.dirname(id), url);
167
+ file =
168
+ tryFsResolve(file, preserveSymlinks) ??
169
+ tryOptimizedDepResolve(resolvedConfig, options?.ssr === true, url, id, preserveSymlinks) ??
170
+ file;
171
+ let builtUrl;
172
+ if (isBuild) {
173
+ builtUrl = await workerFileToUrl(resolvedConfig, file);
174
+ }
175
+ else {
176
+ builtUrl = await fileToUrl(cleanUrl(file), resolvedConfig);
177
+ builtUrl = injectQuery(builtUrl, `${WORKER_FILE_ID}&type=${workerType}`);
178
+ }
179
+ s.update(match.indices[3][0], match.indices[3][1],
180
+ // add `'' +` to skip vite:asset-import-meta-url plugin
181
+ `new URL('' + ${JSON.stringify(builtUrl)}, import.meta.url)`);
182
+ match = alphaTabWorkerPattern.exec(code);
183
+ }
184
+ if (s) {
185
+ return {
186
+ code: s.toString(),
187
+ map: resolvedConfig.command === 'build' && resolvedConfig.build.sourcemap
188
+ ? s.generateMap({ hires: 'boundary', source: id })
189
+ : null
190
+ };
191
+ }
192
+ return null;
193
+ }
194
+ };
195
+ }
196
+
197
+ export { importMetaUrlPlugin };
@@ -0,0 +1,224 @@
1
+ /*!
2
+ * alphaTab Vite Plugin v1.7.1 (, build 24)
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 path = require('node:path');
42
+ const MagicString = require('magic-string');
43
+ const bridge_utils = require('./bridge/utils.cjs');
44
+ const bridge_build = require('./bridge/build.cjs');
45
+ const bridge_constants = require('./bridge/constants.cjs');
46
+ require('node:fs');
47
+ require('vite');
48
+ const bridge_worker = require('./bridge/worker.cjs');
49
+
50
+ function _interopNamespaceDefault(e) {
51
+ const n = Object.create(null, { [Symbol.toStringTag]: { value: 'Module' } });
52
+ if (e) {
53
+ for (const k in e) {
54
+ if (k !== 'default') {
55
+ const d = Object.getOwnPropertyDescriptor(e, k);
56
+ Object.defineProperty(n, k, d.get ? d : {
57
+ enumerable: true,
58
+ get: () => e[k]
59
+ });
60
+ }
61
+ }
62
+ }
63
+ n.default = e;
64
+ return Object.freeze(n);
65
+ }
66
+
67
+ const path__namespace = /*#__PURE__*/_interopNamespaceDefault(path);
68
+
69
+ // This file contains a customized and adapted version of the Vite built-in worker plugin
70
+ // https://github.com/vitejs/vite/blob/main/packages/vite/src/node/plugins/worker.ts
71
+ // This is more or less a 1:1 copy of the original worker plugin with following adaptions:
72
+ // - Only handle syntax variants known to be used in alphaTab
73
+ // - Use the alphaTab URL markers
74
+ // - Some refactoring for better code understanding
75
+ // With https://github.com/vitejs/vite/pull/16422 integrated this custom code might not be needed anymore
76
+ // Some adjustment for audio worklet in vite might be needed to treat them as type "module" workers
77
+ // Original Sources Licensed under:
78
+ // MIT License
79
+ // Copyright (c) 2019-present, Yuxi (Evan) You and Vite contributors
80
+ // Permission is hereby granted, free of charge, to any person obtaining a copy
81
+ // of this software and associated documentation files (the "Software"), to deal
82
+ // in the Software without restriction, including without limitation the rights
83
+ // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
84
+ // copies of the Software, and to permit persons to whom the Software is
85
+ // furnished to do so, subject to the following conditions:
86
+ // The above copyright notice and this permission notice shall be included in all
87
+ // copies or substantial portions of the Software.
88
+ // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
89
+ // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
90
+ // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
91
+ // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
92
+ // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
93
+ // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
94
+ // SOFTWARE.
95
+ const workerFileRE = new RegExp(`(?:\\?|&)${bridge_worker.WORKER_FILE_ID}&type=(\\w+)(?:&|$)`);
96
+ const workerAssetUrlRE = new RegExp(`${bridge_worker.WORKER_ASSET_ID}([a-z\\d]{8})__`, 'g');
97
+ /**
98
+ * @public
99
+ */
100
+ function workerPlugin(options) {
101
+ let resolvedConfig;
102
+ let isBuild;
103
+ let isWorker;
104
+ const isWorkerActive = options.webWorkers !== false;
105
+ const isWorkletActive = options.audioWorklets !== false;
106
+ const isActive = isWorkerActive || isWorkletActive;
107
+ return {
108
+ name: 'vite-plugin-alphatab-worker',
109
+ configResolved(config) {
110
+ resolvedConfig = config;
111
+ isBuild = config.command === 'build';
112
+ isWorker = config.isWorker;
113
+ },
114
+ buildStart() {
115
+ if (!isActive || isWorker) {
116
+ return;
117
+ }
118
+ bridge_worker.workerCache.set(resolvedConfig, {
119
+ assets: new Map(),
120
+ bundle: new Map(),
121
+ fileNameHash: new Map()
122
+ });
123
+ },
124
+ load(id) {
125
+ if (isActive && isBuild && id.includes(bridge_worker.WORKER_FILE_ID)) {
126
+ return '';
127
+ }
128
+ return;
129
+ },
130
+ shouldTransformCachedModule({ id }) {
131
+ if (isActive && isBuild && resolvedConfig.build.watch && id.includes(bridge_worker.WORKER_FILE_ID)) {
132
+ return true;
133
+ }
134
+ return;
135
+ },
136
+ async transform(raw, id) {
137
+ if (!isActive) {
138
+ return;
139
+ }
140
+ const match = workerFileRE.exec(id);
141
+ if (!match) {
142
+ return;
143
+ }
144
+ // inject env to worker file, might be needed by imported scripts
145
+ const envScriptPath = JSON.stringify(path__namespace.posix.join(resolvedConfig.base, bridge_constants.ENV_PUBLIC_PATH));
146
+ const workerType = match[1];
147
+ let injectEnv = '';
148
+ switch (workerType) {
149
+ case bridge_worker.AlphaTabWorkerTypes.WorkerClassic:
150
+ injectEnv = `importScripts(${envScriptPath})\n`;
151
+ break;
152
+ case bridge_worker.AlphaTabWorkerTypes.WorkerModule:
153
+ case bridge_worker.AlphaTabWorkerTypes.AudioWorklet:
154
+ injectEnv = `import ${envScriptPath}\n`;
155
+ break;
156
+ }
157
+ if (injectEnv) {
158
+ const s = new MagicString(raw);
159
+ s.prepend(injectEnv);
160
+ return {
161
+ code: s.toString(),
162
+ map: s.generateMap({ hires: 'boundary' })
163
+ };
164
+ }
165
+ return;
166
+ },
167
+ renderChunk(code, chunk, outputOptions) {
168
+ // when building the worker URLs are replaced with some placeholders
169
+ // here we replace those placeholders with the final file names respecting chunks
170
+ let s;
171
+ const result = () => {
172
+ return (s && {
173
+ code: s.toString(),
174
+ map: resolvedConfig.build.sourcemap ? s.generateMap({ hires: 'boundary' }) : null
175
+ });
176
+ };
177
+ workerAssetUrlRE.lastIndex = 0;
178
+ if (workerAssetUrlRE.test(code)) {
179
+ const toRelativeRuntime = bridge_build.createToImportMetaURLBasedRelativeRuntime(outputOptions.format, resolvedConfig.isWorker);
180
+ s = new MagicString(code);
181
+ workerAssetUrlRE.lastIndex = 0;
182
+ // Replace "VITE_WORKER_ASSET__5aa0ddc0" using relative paths
183
+ const workerMap = bridge_worker.workerCache.get(resolvedConfig.mainConfig || resolvedConfig);
184
+ const { fileNameHash } = workerMap;
185
+ let match = workerAssetUrlRE.exec(code);
186
+ while (match) {
187
+ const [full, hash] = match;
188
+ const filename = fileNameHash.get(hash);
189
+ const replacement = bridge_build.toOutputFilePathInJS(filename, 'asset', chunk.fileName, 'js', resolvedConfig, toRelativeRuntime);
190
+ const replacementString = typeof replacement === 'string'
191
+ ? JSON.stringify(bridge_utils.encodeURIPath(replacement)).slice(1, -1)
192
+ : `"+${replacement.runtime}+"`;
193
+ s.update(match.index, match.index + full.length, replacementString);
194
+ match = workerAssetUrlRE.exec(code);
195
+ }
196
+ }
197
+ return result();
198
+ },
199
+ generateBundle(_, bundle) {
200
+ if (isWorker) {
201
+ return;
202
+ }
203
+ const workerMap = bridge_worker.workerCache.get(resolvedConfig);
204
+ for (const asset of workerMap.assets.values()) {
205
+ const duplicateAsset = bundle[asset.fileName];
206
+ if (duplicateAsset) {
207
+ const content = duplicateAsset.type === 'asset' ? duplicateAsset.source : duplicateAsset.code;
208
+ // don't emit if the file name and the content is same
209
+ if (bridge_worker.isSameContent(content, asset.source)) {
210
+ return;
211
+ }
212
+ }
213
+ this.emitFile({
214
+ type: 'asset',
215
+ fileName: asset.fileName,
216
+ source: asset.source
217
+ });
218
+ }
219
+ workerMap.assets.clear();
220
+ }
221
+ };
222
+ }
223
+
224
+ exports.workerPlugin = workerPlugin;
@@ -0,0 +1,5 @@
1
+ import type { AlphaTabVitePluginOptions } from './AlphaTabVitePluginOptions';
2
+ /**
3
+ * @public
4
+ */
5
+ export declare function workerPlugin(options: AlphaTabVitePluginOptions): Plugin;