Skip to content

Commit

Permalink
refactor: human friendly formatting (#2)
Browse files Browse the repository at this point in the history
  • Loading branch information
reslear authored Sep 2, 2024
1 parent 21cd2dd commit 655f6b4
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 11 deletions.
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'

0 comments on commit 655f6b4

Please sign in to comment.