Skip to content

Commit

Permalink
fix(rollup): improve external detection (#488)
Browse files Browse the repository at this point in the history
  • Loading branch information
pi0 authored Jan 13, 2025
1 parent c3bcc47 commit 53a23ea
Showing 1 changed file with 39 additions and 23 deletions.
62 changes: 39 additions & 23 deletions src/builders/rollup/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import alias from "@rollup/plugin-alias";
import replace from "@rollup/plugin-replace";
import { resolve, isAbsolute } from "pathe";
import { resolveAlias } from "pathe/utils";
import { parseNodeModulePath } from "mlly";
import { arrayIncludes, getpkg, warn } from "../../utils";
import type { BuildContext, RollupOptions } from "../../types";
import { esbuild } from "./plugins/esbuild";
Expand Down Expand Up @@ -57,36 +58,51 @@ export function getRollupOptions(ctx: BuildContext): RollupOptions {
},
].filter(Boolean),

external(id) {
id = resolveAlias(id, _aliases);
const pkg = getpkg(id);
const isExplicitExternal =
arrayIncludes(ctx.options.externals, pkg) ||
arrayIncludes(ctx.options.externals, id);
if (isExplicitExternal) {
return true;
}
external(originalId) {
// Resolve aliases
const resolvedId = resolveAlias(originalId, _aliases);

// Try to guess package name of id
const pkgName =
parseNodeModulePath(resolvedId)?.name ||
parseNodeModulePath(originalId)?.name ||
getpkg(originalId);

// Check for explicit external rules
if (
ctx.options.rollup.inlineDependencies === true ||
id[0] === "." ||
isAbsolute(id) ||
/src[/\\]/.test(id) ||
id.startsWith(ctx.pkg.name!)
arrayIncludes(ctx.options.externals, pkgName) ||
arrayIncludes(ctx.options.externals, originalId) ||
arrayIncludes(ctx.options.externals, resolvedId)
) {
return false;
return true;
}
if (Array.isArray(ctx.options.rollup.inlineDependencies)) {
const isExplicitlyInlined =
arrayIncludes(ctx.options.rollup.inlineDependencies, pkg) ||
arrayIncludes(ctx.options.rollup.inlineDependencies, id);
if (isExplicitlyInlined) {

// Source is always bundled
for (const id of [originalId, resolvedId]) {
if (
id[0] === "." ||
isAbsolute(id) ||
/src[/\\]/.test(id) ||
id.startsWith(ctx.pkg.name!)
) {
return false;
}
}
if (!isExplicitExternal) {
warn(ctx, `Inlined implicit external ${id}`);

// Check for other explicit inline rules
if (
ctx.options.rollup.inlineDependencies === true ||
(Array.isArray(ctx.options.rollup.inlineDependencies) &&
(arrayIncludes(ctx.options.rollup.inlineDependencies, pkgName) ||
arrayIncludes(ctx.options.rollup.inlineDependencies, originalId) ||
arrayIncludes(ctx.options.rollup.inlineDependencies, resolvedId)))
) {
return false;
}
return isExplicitExternal;

// Inline by default, but also show a warning, since it is an implicit behavior
warn(ctx, `Implicitly bundling "${originalId}"`);
return false;
},

onwarn(warning, rollupWarn) {
Expand Down

0 comments on commit 53a23ea

Please sign in to comment.