-
-
Notifications
You must be signed in to change notification settings - Fork 12
feat: add core-js check
#182
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
25 commits
Select commit
Hold shift + click to select a range
92d081e
Add option for JSON output file
AlexanderKaran 4a326c0
Moved back to JSON output
AlexanderKaran 6d7fa88
Merge branch 'main' into json-output
AlexanderKaran ebcc668
Adding Core-JS detection to CLI
AlexanderKaran d0ea974
Update core-js.ts
AlexanderKaran 41ad4bf
Merge branch 'main' into core-js
AlexanderKaran a294200
Fixed lint issues in test
AlexanderKaran 6ee96de
Update src/analyze/core-js.ts
AlexanderKaran 7dbceb9
Move to filesystem
AlexanderKaran c467923
Change target
AlexanderKaran cea0f8e
Added comment
AlexanderKaran 7c65fcf
More detection
AlexanderKaran 23c9933
Import
AlexanderKaran c12813c
Removed build check
AlexanderKaran 43c3c03
Merge branch 'main' into core-js
AlexanderKaran c4865b9
Install
AlexanderKaran f61c510
Vitest Version Missmatch
AlexanderKaran 6a67e80
Merge branch 'vitest' into core-js
AlexanderKaran 496aa0b
Merged in vitest fix
AlexanderKaran e0b6e77
Removed lock
AlexanderKaran 27e68df
Fixes
AlexanderKaran 6bda1fb
Merge branch 'main' into core-js
AlexanderKaran 5bcc793
Updated package
AlexanderKaran a4c11ea
Updated files filter
AlexanderKaran ce2a5ff
Merge branch 'main' into core-js
43081j File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,98 @@ | ||
| import {glob} from 'tinyglobby'; | ||
| import {minVersion} from 'semver'; | ||
| import {relative, join} from 'path'; | ||
| 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 = | ||
| /(?:import\s+(?:.*\s+from\s+)?|require\s*\()\s*['"]([^'"]+)['"]/g; | ||
|
43081j marked this conversation as resolved.
|
||
|
|
||
| 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) => !relative(context.root, join(context.root, 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}; | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.