MCPcopy Create free account
hub / github.com/NativeScript/NativeScript / workerUrlPlugin

Function workerUrlPlugin

packages/vite/helpers/workers.ts:67–90  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

65}
66
67export 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}

Callers 1

baseConfigFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected