Skip to content

Commit

Permalink
bug/issue 1397 handle wildcard exports for sub export condition types (
Browse files Browse the repository at this point in the history
  • Loading branch information
thescientist13 authored Jan 26, 2025
1 parent a1423cd commit d92c2a7
Show file tree
Hide file tree
Showing 6 changed files with 198 additions and 150 deletions.
1 change: 1 addition & 0 deletions packages/cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@
"redux-thunk": "^3.1.0",
"rehype-autolink-headings": "^4.0.0",
"rehype-slug": "^3.0.0",
"signal-utils": "^0.21.1",
"simpledotcss": "^1.0.0",
"tslib": "^2.8.1"
}
Expand Down
19 changes: 13 additions & 6 deletions packages/cli/src/lib/walker-package-ranger.js
Original file line number Diff line number Diff line change
Expand Up @@ -115,9 +115,11 @@ function patternRoot(pattern) {
* "./icons/*": "./icons/*" - https://unpkg.com/browse/@spectrum-web-components/[email protected]/package.json
* "./components/*": "./dist/components/*.js" - https://unpkg.com/browse/@uswds/[email protected]/package.json
* "./src/components/*": "./src/components/* /index.js - https://unpkg.com/browse/@uswds/[email protected]/package.json
* "./*": { "default": "./dist/*.ts.js" } - https://unpkg.com/browse/[email protected]/package.json
*/
async function walkExportPatterns(dependency, sub, subValue, resolvedRoot) {
// find the "deepest" segment we can start from to avoid unnecessary file scanning / crawling
const rootSubOffset = patternRoot(sub);
const rootSubValueOffset = patternRoot(subValue);

// ideally we can use fs.glob when it comes out of experimental
Expand All @@ -135,12 +137,13 @@ async function walkExportPatterns(dependency, sub, subValue, resolvedRoot) {
walkDirectoryForExportPatterns(new URL(`./${file}/`, directoryUrl));
} else if (regexPattern.test(filePathUrl.href)) {
const relativePath = filePathUrl.href.replace(resolvedRoot, '');
// naive way to offset a subValue pattern to the sub pattern
// ex. "./js/*": "./packages/*/src/index.js",
// https://unpkg.com/browse/@uswds/[email protected]/package.json
const rootSubRelativePath = relativePath.replace(rootSubValueOffset, '');
// naive way to offset a subValue pattern to the sub pattern when dealing with wildcards
// ex. "./js/*": "./packages/*/src/index.js" -> /js/<package-name>/src/index.js
const rootSubRelativePath = sub.endsWith('*')
? `./${relativePath}`.replace(subValue.split('*')[0], '').replace(subValue.split('*')[1], '')
: relativePath.replace(rootSubValueOffset, '');

updateImportMap(`${dependency}/${rootSubRelativePath}`, relativePath, resolvedRoot);
updateImportMap(`${dependency}${rootSubOffset}/${rootSubRelativePath}`, relativePath, resolvedRoot);
}
});
}
Expand Down Expand Up @@ -182,7 +185,11 @@ async function walkPackageForExports(dependency, packageJson, resolvedRoot) {
for (const condition of SUPPORTED_EXPORT_CONDITIONS) {
if (exports[sub][condition]) {
matched = true;
trackExportConditions(dependency, exports, sub, condition, resolvedRoot);
if (sub.indexOf('*') >= 0) {
await walkExportPatterns(dependency, sub, exports[sub][condition], resolvedRoot);
} else {
trackExportConditions(dependency, exports, sub, condition, resolvedRoot);
}
break;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ describe('Develop Greenwood With: ', function() {
Object.keys(expectedImportMap).forEach((key) => {
expect(expectedImportMap[key].startsWith('/~/')).to.equal(true);

// truncate full location to avoid difference between local dev and CI workspace paths
// truncate full location to avoid differences between local dev and CI workspace paths
// e.g. passes on my machine
const actualSubPath = importMap[key].slice(importMap[key].indexOf('/node_modules/'));
const expectedSubPath = expectedImportMap[key].slice(expectedImportMap[key].indexOf('/node_modules/'));
Expand Down
Loading

0 comments on commit d92c2a7

Please sign in to comment.