Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add import and directory support to export map subpath exports #672

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 17 additions & 6 deletions packages/cli/src/plugins/resource/plugin-node-modules.js
Original file line number Diff line number Diff line change
Expand Up @@ -142,8 +142,10 @@ const walkPackageJson = (packageJson = {}) => {
packageExport = esmPath
? esmPath
: fallbackPath;
} else if (exportMapEntry.default) {
packageExport = exportMapEntry.default;
} else if (exportMapEntry.import || exportMapEntry.default) {
packageExport = exportMapEntry.import
? exportMapEntry.import
: exportMapEntry.default;

// use the dependency itself as an entry in the importMap
if (entry === '.') {
Expand All @@ -155,15 +157,24 @@ const walkPackageJson = (packageJson = {}) => {
}

if (packageExport) {
const packageExportLocation = path.join(process.cwd(), 'node_modules', `${dependency}/${packageExport.replace('./', '')}`);

// check all exports of an exportMap entry
// to make sure those deps get added to the importMap
if (packageExport.endsWith('.js')) {
const moduleContents = fs.readFileSync(path.join(process.cwd(), 'node_modules', `${dependency}/${packageExport.replace('./', '')}`));
if (packageExport.endsWith('js')) {
const moduleContents = fs.readFileSync(packageExportLocation);

walkModule(moduleContents, dependency);
updateImportMap(`${dependency}/${packageExport.replace('./', '')}`, `/node_modules/${dependency}/${packageExport.replace('./', '')}`);
} else if (fs.lstatSync(packageExportLocation).isDirectory()) {
fs.readdirSync(packageExportLocation)
.filter(file => file.endsWith('.js') || file.endsWith('.mjs'))
.forEach((file) => {
updateImportMap(`${dependency}/${packageExport.replace('./', '')}${file}`, `/node_modules/${dependency}/${packageExport.replace('./', '')}${file}`);
});
} else {
console.warn('Warning, not able to handle export', `${dependency}/${packageExport}`);
}

updateImportMap(`${dependency}/${packageExport.replace('./', '')}`, `/node_modules/${dependency}/${packageExport.replace('./', '')}`);
}
});

Expand Down