* Get the list of files to read the types definitions from the predefined libraries
()
| 70 | * Get the list of files to read the types definitions from the predefined libraries |
| 71 | */ |
| 72 | private async getFilesToRead() { |
| 73 | if (!this.webContainer) return; |
| 74 | |
| 75 | const filesToRead: string[] = []; |
| 76 | const directoriesToRead: string[] = []; |
| 77 | |
| 78 | for (const library of this.librariesToGetTypesFrom) { |
| 79 | // The library's package.json is where the type definitions are defined |
| 80 | const packageJsonFsPath = `./node_modules/${library}/package.json`; |
| 81 | const packageJsonContent = await this.webContainer.fs |
| 82 | .readFile(packageJsonFsPath, 'utf-8') |
| 83 | .catch((error) => { |
| 84 | // Note: "ENOENT" errors occurs: |
| 85 | // - While resetting the NodeRuntimeSandbox. |
| 86 | // - When the library is not a dependency in the project, its package.json won't exist. |
| 87 | // |
| 88 | // In both cases we ignore the error to continue the process. |
| 89 | if (error?.message.startsWith('ENOENT')) { |
| 90 | return; |
| 91 | } |
| 92 | |
| 93 | throw error; |
| 94 | }); |
| 95 | |
| 96 | // if the package.json content is empty, skip this library |
| 97 | if (!packageJsonContent) continue; |
| 98 | |
| 99 | // Ensure the worker VFS also receives the package.json file so NodeNext resolution |
| 100 | // can read "exports"/"types" information when resolving imports like '@angular/core'. |
| 101 | filesToRead.push(`/node_modules/${library}/package.json`); |
| 102 | |
| 103 | const packageJson = JSON.parse(packageJsonContent); |
| 104 | |
| 105 | // If the package exposes a top-level types entry, include that directory as a fallback |
| 106 | const topLevelTypes: string | undefined = packageJson.types ?? packageJson.typings; |
| 107 | if (!packageJson?.exports && topLevelTypes) { |
| 108 | const path = `/node_modules/${library}/${this.normalizePath(topLevelTypes)}`; |
| 109 | const directory = path.substring(0, path.lastIndexOf('/')); |
| 110 | directoriesToRead.push(directory); |
| 111 | continue; |
| 112 | } |
| 113 | |
| 114 | if (!packageJson?.exports) continue; |
| 115 | |
| 116 | // Based on `exports` we can identify paths to the types definition files |
| 117 | for (const exportKey of Object.keys(packageJson.exports)) { |
| 118 | const exportEntry = packageJson.exports[exportKey]; |
| 119 | // Handle both object and string entries; for strings we can't infer types, so skip |
| 120 | const types: string | undefined = |
| 121 | exportEntry && typeof exportEntry === 'object' |
| 122 | ? (exportEntry.typings ?? exportEntry.types) |
| 123 | : undefined; |
| 124 | |
| 125 | if (types) { |
| 126 | const path = `/node_modules/${library}/${this.normalizePath(types)}`; |
| 127 | |
| 128 | // We want to pull all the d.ts files in the directory |
| 129 | // as the file pointed `path` might also import other d.ts files |
no test coverage detected