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
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,17 @@ See the [`recipes/`](./recipes/) directory for complete workflow examples:
- [`basic.yml`](./recipes/basic.yml) - Basic dependency diff on pull requests
- [`bundle-diff.yml`](./recipes/bundle-diff.yml) - Advanced workflow with package bundle size analysis

## Always Report Install Size

If you'd like to always report install size, whether it reduces or increases, you can set the `size-threshold` input to `-1`.

```yaml
- name: Create Diff
uses: e18e/action-dependency-diff@v1
with:
size-threshold: '-1'
```

## Package Bundle Analysis

In addition to analyzing dependency changes, this action can optionally compare the actual bundle sizes of your packages by examining `npm pack` outputs. This provides insights into the **bundle size** (what gets published) rather than just the **install size** (what gets installed with dependencies).
Expand Down
82 changes: 51 additions & 31 deletions build/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -24903,7 +24903,32 @@ function formatBytes(bytes) {
}

// src/checks/dependency-size.ts
async function scanForDependencySize(messages, threshold, currentDeps, baseDeps, currentLockFile) {
async function removeUnsupportedOptionalDependencies(lockFile, versionInfo) {
const allOptionalVersions = /* @__PURE__ */ new Map();
for (const pkg of lockFile.packages) {
traverse(pkg, {
optionalDependency: (node) => {
const entry = allOptionalVersions.get(node.name) ?? /* @__PURE__ */ new Set();
entry.add(node.version);
allOptionalVersions.set(node.name, entry);
}
});
}
for (const [pkg, versions] of allOptionalVersions) {
for (const version of versions) {
const pkgMeta = await fetchPackageMetadata(pkg, version);
if (pkgMeta && !isSupportedArchitecture(pkgMeta, "linux", "x64", "glibc")) {
const newEntry = versionInfo.findIndex(
(v) => v.name === pkg && v.version === version
);
if (newEntry !== -1) {
versionInfo.splice(newEntry, 1);
}
}
}
}
}
async function scanForDependencySize(messages, threshold, currentDeps, baseDeps, currentLockFile, baseLockFile) {
const newVersions = [];
const removedVersions = [];
for (const [packageName, currentVersionSet] of currentDeps) {
Expand All @@ -24930,29 +24955,10 @@ async function scanForDependencySize(messages, threshold, currentDeps, baseDeps,
}
}
if (newVersions.length > 0) {
const allOptionalVersions = /* @__PURE__ */ new Map();
for (const pkg of currentLockFile.packages) {
traverse(pkg, {
optionalDependency: (node) => {
const entry = allOptionalVersions.get(node.name) ?? /* @__PURE__ */ new Set();
entry.add(node.version);
allOptionalVersions.set(node.name, entry);
}
});
}
for (const [pkg, versions] of allOptionalVersions) {
for (const version of versions) {
const pkgMeta = await fetchPackageMetadata(pkg, version);
if (pkgMeta && !isSupportedArchitecture(pkgMeta, "linux", "x64", "glibc")) {
const entry = newVersions.findIndex(
(v) => v.name === pkg && v.version === version
);
if (entry !== -1) {
newVersions.splice(entry, 1);
}
}
}
}
await removeUnsupportedOptionalDependencies(currentLockFile, newVersions);
}
if (removedVersions.length > 0) {
await removeUnsupportedOptionalDependencies(baseLockFile, removedVersions);
}
core5.info(`Found ${newVersions.length} new package versions`);
core5.info(`Found ${removedVersions.length} removed package versions.`);
Expand All @@ -24967,16 +24973,29 @@ async function scanForDependencySize(messages, threshold, currentDeps, baseDeps,
core5.info(
`Total dependency size increase: ${sizeData ? formatBytes(sizeData.totalSize) : "unknown"}`
);
if (sizeData !== null && sizeData.totalSize >= threshold) {
const shouldShow = threshold === -1 || sizeData !== null && sizeData.totalSize >= threshold;
if (shouldShow && sizeData !== null) {
const packageRows = Array.from(sizeData.packageSizes.entries()).sort(([, a], [, b]) => b - a).map(([pkg, size]) => `| ${pkg} | ${formatBytes(size)} |`).join("\n");
messages.push(
`## \u26A0\uFE0F Large Dependency Size Increase
let alert = "";
if (threshold !== -1 && sizeData.totalSize >= threshold) {
alert = `> [!WARNING]
> This PR adds ${formatBytes(sizeData.totalSize)} of new dependencies, which exceeds the threshold of ${formatBytes(threshold)}.

This PR adds ${formatBytes(sizeData.totalSize)} of new dependencies, which exceeds the threshold of ${formatBytes(threshold)}.
`;
} else if (sizeData.totalSize < 0) {
alert = `> [!NOTE]
> :tada: This PR removes ${formatBytes(Math.abs(sizeData.totalSize))} of dependencies.

`;
}
messages.push(
`## \u{1F4CA} Dependency Size Changes

| \u{1F4E6} Package | \u{1F4CF} Size |
${alert}| \u{1F4E6} Package | \u{1F4CF} Size |
| --- | --- |
${packageRows}`
${packageRows}

**Total size change:** ${formatBytes(sizeData.totalSize)}`
);
}
} catch (err) {
Expand Down Expand Up @@ -25168,7 +25187,8 @@ async function run() {
sizeThreshold,
currentDeps,
baseDeps,
parsedCurrentLock
parsedCurrentLock,
parsedBaseLock
);
await scanForProvenance(messages, currentDeps, baseDeps);
const basePackagesPattern = core7.getInput("base-packages");
Expand Down
91 changes: 57 additions & 34 deletions src/checks/dependency-size.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,47 @@ import {
} from '../npm.js';
import {formatBytes} from '../common.js';

async function removeUnsupportedOptionalDependencies(
lockFile: ParsedLockFile,
versionInfo: Array<{name: string; version: string; isNewPackage?: boolean}>
): Promise<void> {
const allOptionalVersions = new Map<string, Set<string>>();

for (const pkg of lockFile.packages) {
traverse(pkg, {
optionalDependency: (node) => {
const entry = allOptionalVersions.get(node.name) ?? new Set<string>();
entry.add(node.version);
allOptionalVersions.set(node.name, entry);
}
});
}

for (const [pkg, versions] of allOptionalVersions) {
for (const version of versions) {
const pkgMeta = await fetchPackageMetadata(pkg, version);
if (
pkgMeta &&
!isSupportedArchitecture(pkgMeta, 'linux', 'x64', 'glibc')
) {
const newEntry = versionInfo.findIndex(
(v) => v.name === pkg && v.version === version
);
if (newEntry !== -1) {
versionInfo.splice(newEntry, 1);
}
}
}
}
}

export async function scanForDependencySize(
messages: string[],
threshold: number,
currentDeps: Map<string, Set<string>>,
baseDeps: Map<string, Set<string>>,
currentLockFile: ParsedLockFile
currentLockFile: ParsedLockFile,
baseLockFile: ParsedLockFile
): Promise<void> {
const newVersions: Array<{
name: string;
Expand Down Expand Up @@ -52,34 +87,11 @@ export async function scanForDependencySize(
}

if (newVersions.length > 0) {
const allOptionalVersions = new Map<string, Set<string>>();

for (const pkg of currentLockFile.packages) {
traverse(pkg, {
optionalDependency: (node) => {
const entry = allOptionalVersions.get(node.name) ?? new Set<string>();
entry.add(node.version);
allOptionalVersions.set(node.name, entry);
}
});
}
await removeUnsupportedOptionalDependencies(currentLockFile, newVersions);
}

for (const [pkg, versions] of allOptionalVersions) {
for (const version of versions) {
const pkgMeta = await fetchPackageMetadata(pkg, version);
if (
pkgMeta &&
!isSupportedArchitecture(pkgMeta, 'linux', 'x64', 'glibc')
) {
const entry = newVersions.findIndex(
(v) => v.name === pkg && v.version === version
);
if (entry !== -1) {
newVersions.splice(entry, 1);
}
}
}
}
if (removedVersions.length > 0) {
await removeUnsupportedOptionalDependencies(baseLockFile, removedVersions);
}

core.info(`Found ${newVersions.length} new package versions`);
Expand All @@ -101,20 +113,31 @@ export async function scanForDependencySize(
}`
);

if (sizeData !== null && sizeData.totalSize >= threshold) {
const shouldShow =
threshold === -1 ||
(sizeData !== null && sizeData.totalSize >= threshold);

if (shouldShow && sizeData !== null) {
const packageRows = Array.from(sizeData.packageSizes.entries())
.sort(([, a], [, b]) => b - a)
.map(([pkg, size]) => `| ${pkg} | ${formatBytes(size)} |`)
.join('\n');

messages.push(
`## ⚠️ Large Dependency Size Increase
let alert = '';
if (threshold !== -1 && sizeData.totalSize >= threshold) {
alert = `> [!WARNING]\n> This PR adds ${formatBytes(sizeData.totalSize)} of new dependencies, which exceeds the threshold of ${formatBytes(threshold)}.\n\n`;
} else if (sizeData.totalSize < 0) {
alert = `> [!NOTE]\n> :tada: This PR removes ${formatBytes(Math.abs(sizeData.totalSize))} of dependencies.\n\n`;
}

This PR adds ${formatBytes(sizeData.totalSize)} of new dependencies, which exceeds the threshold of ${formatBytes(threshold)}.
messages.push(
`## 📊 Dependency Size Changes

| 📦 Package | 📏 Size |
${alert}| 📦 Package | 📏 Size |
| --- | --- |
${packageRows}`
${packageRows}

**Total size change:** ${formatBytes(sizeData.totalSize)}`
);
}
} catch (err) {
Expand Down
3 changes: 2 additions & 1 deletion src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,8 @@ async function run(): Promise<void> {
sizeThreshold,
currentDeps,
baseDeps,
parsedCurrentLock
parsedCurrentLock,
parsedBaseLock
);
await scanForProvenance(messages, currentDeps, baseDeps);

Expand Down