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(externals): check explicit inline rules on resolved id #2288

Merged
merged 5 commits into from Mar 21, 2024
Merged
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
26 changes: 19 additions & 7 deletions src/rollup/plugins/externals.ts
Expand Up @@ -61,6 +61,19 @@ export function externals(opts: NodeExternalsOptions): Plugin {
.map((p) => normalizeMatcher(p))
.sort((a, b) => b.score - a.score);

// Utility to check explicit inlines
const isExplicitInline = (id: string, importer: string) => {
const inlineMatch = inlineMatchers.find((m) => m(id, importer));
const externalMatch = externalMatchers.find((m) => m(id, importer));
if (
inlineMatch &&
(!externalMatch ||
(externalMatch && inlineMatch.score > externalMatch.score))
) {
return true;
}
};

return {
name: "node-externals",
async resolveId(originalId, importer, options) {
Expand All @@ -83,13 +96,7 @@ export function externals(opts: NodeExternalsOptions): Plugin {
const id = normalize(originalId);

// Check for explicit inlines and externals
const inlineMatch = inlineMatchers.find((m) => m(id, importer));
const externalMatch = externalMatchers.find((m) => m(id, importer));
if (
inlineMatch &&
(!externalMatch ||
(externalMatch && inlineMatch.score > externalMatch.score))
) {
if (isExplicitInline(id, importer)) {
return null;
}

Expand All @@ -98,6 +105,11 @@ export function externals(opts: NodeExternalsOptions): Plugin {
id,
};

// Check for explicit inlines and externals
if (isExplicitInline(resolved.id, importer)) {
return null;
}

// Try resolving with mlly as fallback
if (
!isAbsolute(resolved.id) ||
Expand Down