Skip to content
Open
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: 46 additions & 19 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
"obug": "^2.1.1",
"package-manager-detector": "^1.6.0",
"publint": "^0.3.18",
"core-js-compat": "^3.48.0",
"semver": "^7.7.4",
"tinyglobby": "^0.2.15"
},
Expand Down
95 changes: 95 additions & 0 deletions src/analyze/core-js.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
import {glob} from 'tinyglobby';
import {minVersion} from 'semver';
import type {AnalysisContext, ReportPluginResult} from '../types.js';

import coreJsCompat from 'core-js-compat';

const BROAD_IMPORTS = new Set([
'core-js',
'core-js/stable',
'core-js/actual',
'core-js/full'
]);

const SOURCE_GLOB = ['**/*.{js,ts,mjs,cjs,jsx,tsx}'];
const SOURCE_IGNORE = [
'**/node_modules/**',
'**/dist/**',
'**/build/**',
'**/coverage/**',
'**/lib/**'
];

const IMPORT_RE =
Copy link
Contributor

@43081j 43081j Mar 18, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we might be better off using es-module-lexer to actually parse export/import and do it that way.

but not sure where we'd be with require then 🤔 so maybe not...

/(?:import\s+(?:.*\s+from\s+)?|require\s*\()\s*['"]([^'"]+)['"]/g;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we should add some tests for this which is likely to produce false positives. \s+from\s+ can span across multiple imports on the same file, misassociating the import keyword with a later from "..." i agree with james suggestion about using the es-module-lexer to actually parse all of this.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So I am pretty sure @43081j is right, and we would lose support for picking up require('core-js/...'). Do you think we need to keep that?


export async function runCoreJsAnalysis(
context: AnalysisContext
): Promise<ReportPluginResult> {
const messages: ReportPluginResult['messages'] = [];
const pkg = context.packageFile;

const hasCoreJs =
'core-js' in (pkg.dependencies ?? {}) ||
'core-js' in (pkg.devDependencies ?? {}) ||
'core-js-pure' in (pkg.dependencies ?? {}) ||
'core-js-pure' in (pkg.devDependencies ?? {});

if (!hasCoreJs) {
return {messages};
}

const nodeRange = pkg.engines?.node;
let targetVersion = 'current';
if (nodeRange) {
const floor = minVersion(nodeRange);
if (floor) {
targetVersion = floor.version;
}
}

const {list: unnecessaryForTarget} = coreJsCompat.compat({
targets: {node: targetVersion},
inverse: true
});
const unnecessarySet = new Set(unnecessaryForTarget);

const srcGlobs = context.options?.src;
const patterns = srcGlobs && srcGlobs.length > 0 ? srcGlobs : SOURCE_GLOB;
const allFiles = await glob(patterns, {
cwd: context.root,
ignore: SOURCE_IGNORE
});
// filter out any paths that escaped context.root via ../
const files = allFiles.filter((f) => !f.startsWith('..'));

for (const filePath of files) {
let source: string;
try {
source = await context.fs.readFile(filePath);
} catch {
continue;
}

for (const [, specifier] of source.matchAll(IMPORT_RE)) {
if (BROAD_IMPORTS.has(specifier)) {
messages.push({
severity: 'warning',
score: 0,
message: `Broad core-js import "${specifier}" in ${filePath} loads all polyfills at once. Import only the specific modules you need.`
});
} else if (specifier.startsWith('core-js/modules/')) {
const moduleName = specifier.slice('core-js/modules/'.length);
if (unnecessarySet.has(moduleName)) {
messages.push({
severity: 'suggestion',
score: 0,
message: `core-js polyfill "${moduleName}" imported in ${filePath} is unnecessary — your Node.js target (>= ${targetVersion}) already supports this natively.`
});
}
}
}
}

return {messages};
}
4 changes: 3 additions & 1 deletion src/analyze/report.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,14 @@ import {runPlugins} from '../plugin-runner.js';
import {getPackageJson, detectLockfile} from '../utils/package-json.js';
import {parse as parseLockfile} from 'lockparse';
import {runDuplicateDependencyAnalysis} from './duplicate-dependencies.js';
import {runCoreJsAnalysis} from './core-js.js';

const plugins: ReportPlugin[] = [
runPublint,
runReplacements,
runDependencyAnalysis,
runDuplicateDependencyAnalysis
runDuplicateDependencyAnalysis,
runCoreJsAnalysis
];

async function computeInfo(fileSystem: FileSystem) {
Expand Down
6 changes: 6 additions & 0 deletions src/commands/analyze.meta.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,12 @@ export const meta = {
type: 'boolean',
default: false,
description: 'Output results as JSON to stdout'
},
src: {
type: 'string',
multiple: true,
description:
'Glob pattern(s) for source files to scan for imports (e.g. "src/**/*.ts"). Defaults to scanning all JS/TS files from the project root.'
}
}
} as const;
2 changes: 2 additions & 0 deletions src/commands/analyze.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,10 +86,12 @@ export async function run(ctx: CommandContext<typeof meta>) {
}

const customManifests = ctx.values['manifest'];
const srcDirs = ctx.values['src'];

const {stats, messages} = await report({
root,
manifest: customManifests,
src: srcDirs,
categories: parsedCategories
});

Expand Down
Loading
Loading