Skip to content

Commit

Permalink
import map generation fixes for different sub and subValue patterns
Browse files Browse the repository at this point in the history
  • Loading branch information
thescientist13 committed Nov 28, 2024
1 parent 6d48421 commit 42a3ae8
Show file tree
Hide file tree
Showing 3 changed files with 126 additions and 13 deletions.
10 changes: 7 additions & 3 deletions packages/cli/src/lib/walker-package-ranger2.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,9 +104,13 @@ async function walkExportPatterns(dependency, sub, subValue, resolvedRoot) {
walkDirectoryForExportPatterns(new URL(`./${file}/`, directoryUrl));
} else if (regexPattern.test(filePathUrl.href)) {
const rootSubOffset = patternRoot(sub);
const relativePath = filePathUrl.href.replace(resolvedRoot, '');
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/uswds@3.10.0/package.json
const rootSubRelativePath = relativePath.replace(rootSubValueOffset, '');

updateImportMap(`${dependency}${rootSubOffset}/${file}`, `/node_modules/${dependency}/${relativePath}`);
updateImportMap(`${dependency}${rootSubOffset}${rootSubRelativePath}`, `/node_modules/${dependency}${relativePath}`);
}
});
}
Expand Down Expand Up @@ -164,7 +168,7 @@ async function walkPackageForExports(dependency, packageJson, resolvedRoot) {
} else if (sub.indexOf('*') >= 0) {
await walkExportPatterns(dependency, sub, exports[sub], resolvedRoot);
} else {
updateImportMap(`${dependency}/${sub}`, `/node_modules/${dependency}/${sub}`);
updateImportMap(`${dependency}/${sub}`, `/node_modules/${dependency}/${exports[sub]}`);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,8 +116,11 @@ describe('Develop Greenwood With: ', function() {
const importMapTags = dom.window.document.querySelectorAll('head > script[type="importmap"]');
const importMapTag = importMapTags[0];
const importMap = JSON.parse(importMapTag.textContent).imports;
const expectedEntriesCount = Object.keys(expectedImportMap).length;
const actualEntriesCount = Object.keys(importMap).length;

expect(importMapTags.length).to.equal(1);
expect(actualEntriesCount).to.equal(expectedEntriesCount);

Object.keys(expectedImportMap).forEach((key) => {
expect(importMap[key]).to.equal(expectedImportMap[key]);
Expand Down
Loading

0 comments on commit 42a3ae8

Please sign in to comment.