(req: NodeRequire, pkgName: string, cacheRoot: string, debug: boolean)
| 45 | } |
| 46 | |
| 47 | async function prelinkPackage(req: NodeRequire, pkgName: string, cacheRoot: string, debug: boolean) { |
| 48 | const pkgEntry = req.resolve(path.join(pkgName, 'package.json')); |
| 49 | const pkgDir = path.dirname(pkgEntry); |
| 50 | const fesmDir = path.join(pkgDir, 'fesm2022'); |
| 51 | if (!(await pathExists(fesmDir))) return; |
| 52 | // Lazy load linker deps from app |
| 53 | let babel: any = null; |
| 54 | let createLinker: any = null; |
| 55 | try { |
| 56 | const babelPath = req.resolve('@babel/core'); |
| 57 | const linkerPath = req.resolve('@angular/compiler-cli/linker/babel'); |
| 58 | babel = await import(babelPath); |
| 59 | const linkerMod: any = await import(linkerPath); |
| 60 | createLinker = linkerMod.createLinkerPlugin || linkerMod.createEs2015LinkerPlugin; |
| 61 | } catch { |
| 62 | return; // Can't link without deps; skip quietly |
| 63 | } |
| 64 | const destDir = path.join(cacheRoot, pkgName, 'fesm2022'); |
| 65 | await ensureDir(destDir); |
| 66 | const files = (await fs.readdir(fesmDir)).filter((f) => f.endsWith('.mjs')); |
| 67 | for (const file of files) { |
| 68 | const srcFile = path.join(fesmDir, file); |
| 69 | const cacheFile = path.join(destDir, file); |
| 70 | await copyOrLinkInto(babel, createLinker, srcFile, cacheFile); |
| 71 | if (debug) { |
| 72 | try { |
| 73 | console.log(`[ns-angular-prelink] cached`, `${pkgName}/fesm2022/${file}`); |
| 74 | } catch {} |
| 75 | } |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | export function createAngularPrelinkPlugin(projectRoot?: string): Plugin { |
| 80 | const req = createRequire(projectRoot ? projectRoot + '/package.json' : import.meta.url); |
no test coverage detected