Skip to content
Merged
Show file tree
Hide file tree
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
65 changes: 42 additions & 23 deletions .github/actions/size-limit/src/package/changes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,32 +62,51 @@ export const getChangedPackages = async (
// Map changed files to packages
for (const file of changedFiles) {
// Check if file is in packages directory
const packagesMatch = file.match(/^packages\/((?:@[^/]+\/)?[^/]+)\//i);
if (packagesMatch?.[1]) {
const packageDir = packagesMatch[1];
if (file.startsWith("packages/")) {
// Find the nearest package.json
const parts = file.split("/");
// Start from the directory containing the file, go up until we hit "packages"
// parts[0] is "packages"
let packageFound = false;

// Get actual package name from package.json
const packageJsonPath = join(
process.cwd(),
"packages",
packageDir,
"package.json",
);
if (existsSync(packageJsonPath)) {
try {
const packageJson = JSON.parse(
readFileSync(packageJsonPath, "utf-8"),
);
if (packageJson.name) {
changedPackages.add(packageJson.name);
} else {
changedPackages.add(packageDir);
// Iterate from deep to shallow, but stop before "packages" (index 0)
for (let i = parts.length - 1; i > 0; i--) {
const potentialPackageDir = parts.slice(1, i).join("/");
if (!potentialPackageDir) continue;

const packageJsonPath = join(
process.cwd(),
"packages",
potentialPackageDir,
"package.json",
);

if (existsSync(packageJsonPath)) {
try {
const packageJson = JSON.parse(
readFileSync(packageJsonPath, "utf-8"),
);
if (packageJson.name) {
changedPackages.add(packageJson.name);
packageFound = true;
break;
}
} catch {
// Ignore invalid package.json
}
} catch {
changedPackages.add(packageDir);
}
} else {
changedPackages.add(packageDir);
}

// Fallback: if we couldn't find a package.json, try to guess the package directory
// This handles cases where package.json might be deleted or we can't read it
if (!packageFound) {
// Regex that handles scoped and unscoped packages
const packagesMatch = file.match(
/^packages\/((?:@[^/]+\/)?[^/]+)\//i,
);
if (packagesMatch?.[1]) {
changedPackages.add(packagesMatch[1]);
}
}
}
}
Expand Down
9 changes: 7 additions & 2 deletions .github/actions/size-limit/src/size-limit/package.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,7 @@ export const runSizeLimitOnPackage = async (

return {
...config,
webpack: false, // Use esbuild instead of webpack
// Configure esbuild to mark Node.js built-in modules as external
// Use esbuild (default in preset-small-lib) and configure it to mark Node.js built-in modules as external
// This tells esbuild not to try to bundle these modules
// size-limit passes this to esbuild's external option
ignore: [
Expand All @@ -106,6 +105,12 @@ export const runSizeLimitOnPackage = async (
: []),
...nodeBuiltinModules.map((m) => `node:${m}`),
...nodeBuiltinModules,
// Automatically ignore peerDependencies
// This prevents bundling large peer dependencies like arktype or vite
// which should be external in the final bundle anyway
...(packageJson.peerDependencies
? Object.keys(packageJson.peerDependencies)
: []),
],
};
});
Expand Down
Loading