-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
cache / memoize walked packages to account for concurrency in greenwood
- Loading branch information
1 parent
ea10aa9
commit ad7ac8e
Showing
1 changed file
with
6 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,7 +5,6 @@ import fs from 'fs'; | |
const SUPPORTED_EXPORT_CONDITIONS = ['import', 'module-sync', 'default']; | ||
const IMPORT_MAP_RESOLVED_PREFIX = '/~'; | ||
const importMap = new Map(); | ||
const walkedPackages = new Set(); | ||
const diagnostics = {}; | ||
|
||
function updateImportMap(key, value, resolvedRoot) { | ||
|
@@ -71,7 +70,7 @@ function derivePackageRoot(resolved) { | |
return root; | ||
} | ||
|
||
// Helper function to convert export patterns to a regex (thanks ChatGPT :D) | ||
// helper function to convert export patterns to a regex (thanks ChatGPT :D) | ||
function globToRegex(pattern) { | ||
// Escape special regex characters | ||
pattern = pattern.replace(/[.+^${}()|[\]\\]/g, '\\$&'); | ||
|
@@ -86,7 +85,7 @@ function globToRegex(pattern) { | |
return new RegExp('^' + pattern + '$'); | ||
} | ||
|
||
// convert path to its lowest common root | ||
// helper function to convert path to its lowest common root | ||
// e.g. ./img/path/*/index.js -> /img/path | ||
// https://unpkg.com/browse/@uswds/[email protected]/package.json | ||
function patternRoot(pattern) { | ||
|
@@ -212,8 +211,9 @@ async function walkPackageForExports(dependency, packageJson, resolvedRoot) { | |
} | ||
} | ||
|
||
// https://nodejs.org/api/packages.html#package-entry-points | ||
async function walkPackageJson(packageJson = {}) { | ||
// we recursively cache / memoize walkedPackages to account for scenarios where Greenwood can (pre)render concurrently | ||
async function walkPackageJson(packageJson = {}, walkedPackages = new Set()) { | ||
|
||
try { | ||
const dependencies = Object.keys(packageJson.dependencies || {}); | ||
|
||
|
@@ -232,7 +232,7 @@ async function walkPackageJson(packageJson = {}) { | |
if (!walkedPackages.has(name)) { | ||
walkedPackages.add(name); | ||
|
||
await walkPackageJson(resolvedPackageJson); | ||
await walkPackageJson(resolvedPackageJson, walkedPackages); | ||
} | ||
} | ||
} | ||
|