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

Fix internal forks plugin to support ESM packages #23254

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
15 changes: 13 additions & 2 deletions scripts/rollup/plugins/use-forks-plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,19 +26,30 @@ function resolveRelatively(importee, importer) {
}
}

function naivelyResolveInternalModulePath(p) {
// Naive version of require.resolve, because require.resolve is not available
// in the ESM world. We don't need this to be generally correct; we only use
// it to resolve internal forks at build time.
let resolved = path.resolve(process.cwd(), 'packages', p);
if (!resolved.endsWith('.js')) {
resolved += '.js';
}
return resolved;
}

let resolveCache = new Map();
function useForks(forks) {
let resolvedForks = new Map();
Object.keys(forks).forEach(srcModule => {
const targetModule = forks[srcModule];
resolvedForks.set(
require.resolve(srcModule),
naivelyResolveInternalModulePath(srcModule),
// targetModule could be a string (a file path),
// or an error (which we'd throw if it gets used).
// Don't try to "resolve" errors, but cache
// resolved file paths.
typeof targetModule === 'string'
? require.resolve(targetModule)
? naivelyResolveInternalModulePath(targetModule)
: targetModule
);
});
Expand Down