| 2 | import { nsConfigToJson, resolveNativeScriptPlatformFile } from './utils.js'; |
| 3 | |
| 4 | export function getWorkerPlugins(platform: string) { |
| 5 | return [ |
| 6 | // Handle ~/package.json virtual module for workers |
| 7 | { |
| 8 | name: 'worker-virtual-package-json', |
| 9 | resolveId(id) { |
| 10 | if (id === '~/package.json') { |
| 11 | return '\0worker:nsconfig-json'; // Use a completely different virtual ID that doesn't look like JSON |
| 12 | } |
| 13 | return null; |
| 14 | }, |
| 15 | load(id) { |
| 16 | if (id === '\0worker:nsconfig-json') { |
| 17 | const configObject = nsConfigToJson(); |
| 18 | // Return the NativeScript config as a JavaScript module |
| 19 | return `export default ${JSON.stringify(configObject, null, 2)};`; |
| 20 | } |
| 21 | return null; |
| 22 | }, |
| 23 | }, |
| 24 | // Resolve NativeScript platform-specific files for workers |
| 25 | { |
| 26 | name: 'nativescript-platform-resolver-worker', |
| 27 | resolveId(id, importer) { |
| 28 | // Handle relative imports from node_modules (not just @nativescript/core) |
| 29 | if (importer) { |
| 30 | const resolvedPath = path.resolve(path.dirname(importer), id); |
| 31 | |
| 32 | // Try different extensions with platform-specific resolution |
| 33 | const extensions = ['.js', '.mjs', '.ts']; |
| 34 | |
| 35 | for (const ext of extensions) { |
| 36 | const testPath = resolvedPath + ext; |
| 37 | // Use the existing NativeScript platform file resolver |
| 38 | const platformResolvedFile = resolveNativeScriptPlatformFile(testPath, platform); |
| 39 | if (platformResolvedFile) { |
| 40 | return platformResolvedFile; |
| 41 | } |
| 42 | } |
| 43 | |
| 44 | return null; |
| 45 | } |
| 46 | return null; |
| 47 | }, |
| 48 | }, |
| 49 | // Handle import.meta expressions in workers |
| 50 | { |
| 51 | name: 'worker-import-meta-handler', |
| 52 | transform(code, id) { |
| 53 | // Replace import.meta.dirname with a static value for workers |
| 54 | if (code.includes('import.meta.dirname')) { |
| 55 | code = code.replace(/import\.meta\.dirname/g, '""'); |
| 56 | } |
| 57 | // Replace import.meta.url with a static value for workers |
| 58 | if (code.includes('import.meta.url')) { |
| 59 | code = code.replace(/import\.meta\.url/g, '"file:///app/"'); |
| 60 | } |
| 61 | return code; |