| 110 | * @public |
| 111 | */ |
| 112 | export function importMetaUrlPlugin(options: AlphaTabVitePluginOptions): Plugin { |
| 113 | let resolvedConfig: ResolvedConfig; |
| 114 | let isBuild: boolean; |
| 115 | let preserveSymlinks: boolean; |
| 116 | |
| 117 | const isWorkerActive = options.webWorkers !== false; |
| 118 | const isWorkletActive = options.audioWorklets !== false; |
| 119 | const isActive = isWorkerActive || isWorkletActive; |
| 120 | |
| 121 | return { |
| 122 | name: 'vite-plugin-alphatab-url', |
| 123 | enforce: 'pre', |
| 124 | configResolved(config) { |
| 125 | resolvedConfig = config as ResolvedConfig; |
| 126 | isBuild = config.command === 'build'; |
| 127 | preserveSymlinks = config.resolve.preserveSymlinks; |
| 128 | }, |
| 129 | |
| 130 | shouldTransformCachedModule({ code }) { |
| 131 | if (isActive && isBuild && resolvedConfig.build.watch && includesAlphaTabWorker(code)) { |
| 132 | return true; |
| 133 | } |
| 134 | return; |
| 135 | }, |
| 136 | |
| 137 | async transform(code, id, options) { |
| 138 | if (!isActive || options?.ssr || !includesAlphaTabWorker(code)) { |
| 139 | return; |
| 140 | } |
| 141 | |
| 142 | let s: MagicString | undefined; |
| 143 | |
| 144 | // Match `new alphaTabWorker(new ...alphaTabUrl(...))` and any |
| 145 | // `<expr>.addModule(new ...alphaTabUrl(...))` (the worklet path, |
| 146 | // which may have its alias inlined away). Worker vs worklet is |
| 147 | // disambiguated by `getWorkerType` based on the URL string. |
| 148 | const alphaTabWorkerPattern = |
| 149 | /\b(alphaTabWorker|[\w.]+\.addModule)\s*\(\s*(new\s+[^ (]+alphaTabUrl\s*\(\s*('[^']+'|"[^"]+"|`[^`]+`)\s*,\s*import\.meta\.url\s*\))/dg; |
| 150 | |
| 151 | let match: RegExpExecArray | null = alphaTabWorkerPattern.exec(code); |
| 152 | while (match) { |
| 153 | const workerType = getWorkerType(code, match); |
| 154 | |
| 155 | let typeActive = false; |
| 156 | switch (workerType) { |
| 157 | case AlphaTabWorkerTypes.WorkerClassic: |
| 158 | case AlphaTabWorkerTypes.WorkerModule: |
| 159 | typeActive = isWorkerActive; |
| 160 | break; |
| 161 | case AlphaTabWorkerTypes.AudioWorklet: |
| 162 | typeActive = isWorkletActive; |
| 163 | break; |
| 164 | } |
| 165 | |
| 166 | if (!typeActive) { |
| 167 | match = alphaTabWorkerPattern.exec(code); |
| 168 | continue; |
| 169 | } |