| 55 | * @public |
| 56 | */ |
| 57 | export function workerPlugin(options: AlphaTabVitePluginOptions): Plugin { |
| 58 | let resolvedConfig: ResolvedConfig; |
| 59 | let isBuild: boolean; |
| 60 | let isWorker: boolean; |
| 61 | |
| 62 | const isWorkerActive = options.webWorkers !== false; |
| 63 | const isWorkletActive = options.audioWorklets !== false; |
| 64 | const isActive = isWorkerActive || isWorkletActive; |
| 65 | |
| 66 | return { |
| 67 | name: 'vite-plugin-alphatab-worker', |
| 68 | |
| 69 | configResolved(config) { |
| 70 | resolvedConfig = config as ResolvedConfig; |
| 71 | isBuild = config.command === 'build'; |
| 72 | isWorker = config.isWorker; |
| 73 | }, |
| 74 | |
| 75 | buildStart() { |
| 76 | if (!isActive || isWorker) { |
| 77 | return; |
| 78 | } |
| 79 | workerCache.set(resolvedConfig, { |
| 80 | assets: new Map(), |
| 81 | bundle: new Map(), |
| 82 | fileNameHash: new Map() |
| 83 | }); |
| 84 | }, |
| 85 | |
| 86 | load(id) { |
| 87 | if (isActive && isBuild && id.includes(WORKER_FILE_ID)) { |
| 88 | return ''; |
| 89 | } |
| 90 | return; |
| 91 | }, |
| 92 | |
| 93 | shouldTransformCachedModule({ id }) { |
| 94 | if (isActive && isBuild && resolvedConfig.build.watch && id.includes(WORKER_FILE_ID)) { |
| 95 | return true; |
| 96 | } |
| 97 | return; |
| 98 | }, |
| 99 | |
| 100 | async transform(raw, id) { |
| 101 | if (!isActive) { |
| 102 | return; |
| 103 | } |
| 104 | |
| 105 | const match = workerFileRE.exec(id); |
| 106 | if (!match) { |
| 107 | return; |
| 108 | } |
| 109 | |
| 110 | // inject env to worker file, might be needed by imported scripts |
| 111 | |
| 112 | const envScriptPath = JSON.stringify(path.posix.join(resolvedConfig.base, ENV_PUBLIC_PATH)); |
| 113 | |
| 114 | const workerType = match[1] as AlphaTabWorkerTypes; |