Skip to content
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

refactor: human friendly formatting #2

Merged
merged 2 commits into from
Sep 2, 2024
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
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@
"dependencies": {
"@rollup/pluginutils": "^5.1.0",
"js-tokens": "^9.0.0",
"picocolors": "^1.0.1",
"pkg-types": "^1.2.0",
"unplugin": "^1.12.2"
},
Expand Down
3 changes: 3 additions & 0 deletions pnpm-lock.yaml

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

12 changes: 9 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import process from 'node:process'
import { createFilter } from '@rollup/pluginutils'
import escapeStringRegexp from 'escape-string-regexp'
import jsTokens from 'js-tokens'
import pc from 'picocolors'
import { readPackageJSON, resolvePackageJSON } from 'pkg-types'
import { createUnplugin, type UnpluginInstance } from 'unplugin'
import { resolveOptions, type Options } from './core/options'
Expand Down Expand Up @@ -55,9 +56,14 @@ const plugin: UnpluginInstance<Options | undefined, false> = createUnplugin(

buildEnd() {
if (deps.size) {
const error = new Error(
`Unused dependencies found!\nDependencies: ${Array.from(deps).join(', ')}\nYou can remove them from ${pkgPath}`,
)
const message =
`Unused ${pc.cyan(deps.size)} dependencies found: \n\n` +
`${Array.from(deps)
.map((dep) => `${pc.yellow('-')} ${pc.bold(dep)}`)
.join('\n')}\n\n` +
`You can remove them from ${pkgPath}`

const error = new Error(message)
if (options.level === 'error') {
throw error
} else {
Expand Down
33 changes: 25 additions & 8 deletions tests/__snapshots__/rollup.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -6,32 +6,49 @@ var basic = \`import '@rollup/pluginutils'
import 'js-tokens'
import 'pkg-types'
import 'unplugin'
import 'picocolors'
\`;

export { basic as default };
"
`;

exports[`rollup > error.js 1`] = `
[Error: Unused dependencies found!
Dependencies: js-tokens
[Error: Unused 2 dependencies found:

- js-tokens
- picocolors

You can remove them from #CWD#/package.json]
`;

exports[`rollup > error-comment.js 1`] = `
[Error: Unused dependencies found!
Dependencies: @rollup/pluginutils, js-tokens, pkg-types
[Error: Unused 4 dependencies found:

- @rollup/pluginutils
- js-tokens
- picocolors
- pkg-types

You can remove them from #CWD#/package.json]
`;

exports[`rollup > error-tsx.tsx 1`] = `
[Error: Unused dependencies found!
Dependencies: unplugin
[Error: Unused 2 dependencies found:

- picocolors
- unplugin

You can remove them from #CWD#/package.json]
`;

exports[`rollup > error-vue.vue 1`] = `
[Error: Unused dependencies found!
Dependencies: @rollup/pluginutils, js-tokens, pkg-types
[Error: Unused 4 dependencies found:

- @rollup/pluginutils
- js-tokens
- picocolors
- pkg-types

You can remove them from #CWD#/package.json]
`;
1 change: 1 addition & 0 deletions tests/fixtures/basic.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ import '@rollup/pluginutils'
import 'js-tokens'
import 'pkg-types'
import 'unplugin'
import 'picocolors'