| 10 | |
| 11 | // Rollup-level linker to guarantee Angular libraries are linked when included in the bundle graph. |
| 12 | function angularRollupLinker(projectRoot?: string): Plugin { |
| 13 | let babel: any = null; |
| 14 | let createLinker: any = null; |
| 15 | const FILTER = /node_modules\/(?:@angular|@nativescript\/angular)\/.*\.[mc]?js$/; |
| 16 | |
| 17 | async function ensureDeps() { |
| 18 | if (babel && createLinker) return; |
| 19 | try { |
| 20 | const req = createRequire((projectRoot ? projectRoot + '/package.json' : import.meta.url) as any); |
| 21 | const babelPath = req.resolve('@babel/core'); |
| 22 | |
| 23 | const linkerPath = req.resolve('@angular/compiler-cli/linker/babel'); |
| 24 | babel = await import(babelPath); |
| 25 | const linkerMod: any = await import(linkerPath); |
| 26 | createLinker = linkerMod.createLinkerPlugin || linkerMod.createEs2015LinkerPlugin; |
| 27 | } catch { |
| 28 | try { |
| 29 | babel = await import('@babel/core'); |
| 30 | } catch {} |
| 31 | try { |
| 32 | const linkerMod: any = await import('@angular/compiler-cli/linker/babel'); |
| 33 | createLinker = linkerMod.createLinkerPlugin || linkerMod.createEs2015LinkerPlugin; |
| 34 | } catch {} |
| 35 | } |
| 36 | } |
| 37 | |
| 38 | return { |
| 39 | name: 'ns-angular-linker-rollup', |
| 40 | enforce: 'pre', |
| 41 | apply: 'build', |
| 42 | async load(id) { |
| 43 | const debug = process.env.VITE_DEBUG_LOGS === 'true' || process.env.VITE_DEBUG_LOGS === '1'; |
| 44 | const cleanId = id.split('?', 1)[0]; |
| 45 | if (!FILTER.test(cleanId)) return null; |
| 46 | try { |
| 47 | await ensureDeps(); |
| 48 | if (!babel || !createLinker) return null; |
| 49 | const fs = await import('node:fs/promises'); |
| 50 | const code = await fs.readFile(cleanId, 'utf8'); |
| 51 | const forceLink = cleanId.includes('/node_modules/@nativescript/angular/') && cleanId.includes('polyfills'); |
| 52 | if (!code) return null; |
| 53 | if (!forceLink && code.indexOf('\u0275\u0275ngDeclare') === -1 && code.indexOf('ɵɵngDeclare') === -1 && code.indexOf('ngDeclare') === -1) return null; |
| 54 | const plugin = createLinker({ sourceMapping: false }); |
| 55 | if (debug) { |
| 56 | try { |
| 57 | console.log('[ns-angular-linker][rollup-load] linking', cleanId); |
| 58 | } catch {} |
| 59 | } |
| 60 | const result = await (babel as any).transformAsync(code, { |
| 61 | filename: cleanId, |
| 62 | configFile: false, |
| 63 | babelrc: false, |
| 64 | sourceMaps: false, |
| 65 | compact: false, |
| 66 | plugins: [plugin], |
| 67 | }); |
| 68 | if (result?.code && result.code !== code) { |
| 69 | return { code: result.code, map: null } as any; |