|
| 1 | +import {glob} from 'tinyglobby'; |
| 2 | +import {minVersion} from 'semver'; |
| 3 | +import {relative, join} from 'path'; |
| 4 | +import type {AnalysisContext, ReportPluginResult} from '../types.js'; |
| 5 | + |
| 6 | +import coreJsCompat from 'core-js-compat'; |
| 7 | + |
| 8 | +const BROAD_IMPORTS = new Set([ |
| 9 | + 'core-js', |
| 10 | + 'core-js/stable', |
| 11 | + 'core-js/actual', |
| 12 | + 'core-js/full' |
| 13 | +]); |
| 14 | + |
| 15 | +const SOURCE_GLOB = ['**/*.{js,ts,mjs,cjs,jsx,tsx}']; |
| 16 | +const SOURCE_IGNORE = [ |
| 17 | + '**/node_modules/**', |
| 18 | + '**/dist/**', |
| 19 | + '**/build/**', |
| 20 | + '**/coverage/**', |
| 21 | + '**/lib/**' |
| 22 | +]; |
| 23 | + |
| 24 | +const IMPORT_RE = |
| 25 | + /(?:import\s+(?:.*\s+from\s+)?|require\s*\()\s*['"]([^'"]+)['"]/g; |
| 26 | + |
| 27 | +export async function runCoreJsAnalysis( |
| 28 | + context: AnalysisContext |
| 29 | +): Promise<ReportPluginResult> { |
| 30 | + const messages: ReportPluginResult['messages'] = []; |
| 31 | + const pkg = context.packageFile; |
| 32 | + |
| 33 | + const hasCoreJs = |
| 34 | + 'core-js' in (pkg.dependencies ?? {}) || |
| 35 | + 'core-js' in (pkg.devDependencies ?? {}) || |
| 36 | + 'core-js-pure' in (pkg.dependencies ?? {}) || |
| 37 | + 'core-js-pure' in (pkg.devDependencies ?? {}); |
| 38 | + |
| 39 | + if (!hasCoreJs) { |
| 40 | + return {messages}; |
| 41 | + } |
| 42 | + |
| 43 | + const nodeRange = pkg.engines?.node; |
| 44 | + let targetVersion = 'current'; |
| 45 | + if (nodeRange) { |
| 46 | + const floor = minVersion(nodeRange); |
| 47 | + if (floor) { |
| 48 | + targetVersion = floor.version; |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + const {list: unnecessaryForTarget} = coreJsCompat.compat({ |
| 53 | + targets: {node: targetVersion}, |
| 54 | + inverse: true |
| 55 | + }); |
| 56 | + const unnecessarySet = new Set(unnecessaryForTarget); |
| 57 | + |
| 58 | + const srcGlobs = context.options?.src; |
| 59 | + const patterns = srcGlobs && srcGlobs.length > 0 ? srcGlobs : SOURCE_GLOB; |
| 60 | + const allFiles = await glob(patterns, { |
| 61 | + cwd: context.root, |
| 62 | + ignore: SOURCE_IGNORE |
| 63 | + }); |
| 64 | + // filter out any paths that escaped context.root via ../ |
| 65 | + const files = allFiles.filter( |
| 66 | + (f) => !relative(context.root, join(context.root, f)).startsWith('..') |
| 67 | + ); |
| 68 | + |
| 69 | + for (const filePath of files) { |
| 70 | + let source: string; |
| 71 | + try { |
| 72 | + source = await context.fs.readFile(filePath); |
| 73 | + } catch { |
| 74 | + continue; |
| 75 | + } |
| 76 | + |
| 77 | + for (const [, specifier] of source.matchAll(IMPORT_RE)) { |
| 78 | + if (BROAD_IMPORTS.has(specifier)) { |
| 79 | + messages.push({ |
| 80 | + severity: 'warning', |
| 81 | + score: 0, |
| 82 | + message: `Broad core-js import "${specifier}" in ${filePath} loads all polyfills at once. Import only the specific modules you need.` |
| 83 | + }); |
| 84 | + } else if (specifier.startsWith('core-js/modules/')) { |
| 85 | + const moduleName = specifier.slice('core-js/modules/'.length); |
| 86 | + if (unnecessarySet.has(moduleName)) { |
| 87 | + messages.push({ |
| 88 | + severity: 'suggestion', |
| 89 | + score: 0, |
| 90 | + message: `core-js polyfill "${moduleName}" imported in ${filePath} is unnecessary — your Node.js target (>= ${targetVersion}) already supports this natively.` |
| 91 | + }); |
| 92 | + } |
| 93 | + } |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + return {messages}; |
| 98 | +} |
0 commit comments