| 65 | } |
| 66 | |
| 67 | export function workerUrlPlugin() { |
| 68 | return { |
| 69 | name: 'nativescript-worker-url-transform', |
| 70 | generateBundle(options, bundle) { |
| 71 | // Transform the main bundle to use NativeScript worker paths |
| 72 | for (const [fileName, chunk] of Object.entries(bundle) as any) { |
| 73 | if (chunk.type === 'chunk' && !fileName.includes('.worker')) { |
| 74 | // Transform Vite's worker URL pattern to NativeScript's expected format |
| 75 | // From: new Worker(new URL(/* @vite-ignore */ "/assets/sample.worker-C6wW8q2-.js", import.meta.url)) |
| 76 | // To: new Worker('~/' + 'assets/sample.worker-C6wW8q2-.js') |
| 77 | const workerUrlRegex = /new\s+Worker\s*\(\s*new\s+URL\s*\(\s*(?:\/\*[^*]*\*\/\s*)?["']([^"']+)["']\s*,\s*import\.meta\.url\s*\)\s*\)/g; |
| 78 | |
| 79 | if (workerUrlRegex.test(chunk.code)) { |
| 80 | chunk.code = chunk.code.replace(workerUrlRegex, (match, assetPath) => { |
| 81 | // Use the full asset path including assets/ folder |
| 82 | const fullPath = assetPath.startsWith('/') ? assetPath.slice(1) : assetPath; |
| 83 | return `new Worker('~/' + '${fullPath}')`; |
| 84 | }); |
| 85 | } |
| 86 | } |
| 87 | } |
| 88 | }, |
| 89 | }; |
| 90 | } |