Skip to content

Commit

Permalink
feat: show package path
Browse files Browse the repository at this point in the history
  • Loading branch information
sxzz committed Sep 1, 2024
1 parent 99e345f commit 9d25eac
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 15 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "unplugin-unused",
"version": "0.1.1",
"packageManager": "pnpm@9.8.0",
"packageManager": "pnpm@9.9.0",
"description": "Check unused dependencies.",
"type": "module",
"keywords": [
Expand Down
9 changes: 6 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import path from 'node:path'
import process from 'node:process'
import { createFilter } from '@rollup/pluginutils'
import escapeStringRegexp from 'escape-string-regexp'
import jsTokens from 'js-tokens'
import { readPackageJSON } from 'pkg-types'
import { readPackageJSON, resolvePackageJSON } from 'pkg-types'
import { createUnplugin, type UnpluginInstance } from 'unplugin'
import { resolveOptions, type Options } from './core/options'

Expand All @@ -12,6 +13,7 @@ const plugin: UnpluginInstance<Options | undefined, false> = createUnplugin(
const filter = createFilter(options.include, options.exclude)
const deps = new Set<string>()
const depsRegex: Record<string, RegExp> = {}
let pkgPath: string

const name = 'unplugin-unused'
return {
Expand All @@ -20,7 +22,8 @@ const plugin: UnpluginInstance<Options | undefined, false> = createUnplugin(

async buildStart() {
options.root ||= process.cwd()
const pkg = await readPackageJSON(options.root)
pkgPath = path.resolve(await resolvePackageJSON(options.root))
const pkg = await readPackageJSON(pkgPath)
const dependencies = Object.keys(pkg.dependencies || {})
for (const dep of dependencies) {
deps.add(dep)
Expand Down Expand Up @@ -49,7 +52,7 @@ const plugin: UnpluginInstance<Options | undefined, false> = createUnplugin(
buildEnd() {
if (deps.size) {
const error = new Error(
`Unused dependencies found: ${Array.from(deps).join(', ')}`,
`Unused dependencies found!\nDependencies: ${Array.from(deps).join(', ')}\nYou can remove them from ${pkgPath}`,
)
if (options.level === 'error') {
throw error
Expand Down
24 changes: 20 additions & 4 deletions tests/__snapshots__/rollup.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,26 @@ export { basic as default };
"
`;

exports[`rollup > error.js 1`] = `[Error: Unused dependencies found: js-tokens]`;
exports[`rollup > error.js 1`] = `
[Error: Unused dependencies found!
Dependencies: js-tokens
You can remove them from #CWD#/package.json]
`;

exports[`rollup > error-comment.js 1`] = `[Error: Unused dependencies found: @rollup/pluginutils, js-tokens, pkg-types]`;
exports[`rollup > error-comment.js 1`] = `
[Error: Unused dependencies found!
Dependencies: @rollup/pluginutils, js-tokens, pkg-types
You can remove them from #CWD#/package.json]
`;

exports[`rollup > error-tsx.tsx 1`] = `[Error: Unused dependencies found: unplugin]`;
exports[`rollup > error-tsx.tsx 1`] = `
[Error: Unused dependencies found!
Dependencies: unplugin
You can remove them from #CWD#/package.json]
`;

exports[`rollup > error-vue.vue 1`] = `[Error: Unused dependencies found: @rollup/pluginutils, js-tokens, pkg-types]`;
exports[`rollup > error-vue.vue 1`] = `
[Error: Unused dependencies found!
Dependencies: @rollup/pluginutils, js-tokens, pkg-types
You can remove them from #CWD#/package.json]
`;
23 changes: 16 additions & 7 deletions tests/rollup.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import path from 'node:path'
import process from 'node:process'
import {
rollupBuild,
RollupToStringPlugin,
Expand All @@ -12,13 +13,21 @@ describe('rollup', async () => {
await testFixtures(
'*.{js,tsx,vue}',
async (args, id) => {
const { snapshot } = await rollupBuild(id, [
UnpluginUnused({
level: 'error',
}),
RollupToStringPlugin(),
])
return snapshot
try {
const { snapshot } = await rollupBuild(id, [
UnpluginUnused({
level: 'error',
}),
RollupToStringPlugin(),
])
return snapshot
} catch (error: any) {
error.message = error.message.replaceAll(
process.cwd() + path.sep,
'#CWD#/',
)
throw error
}
},
{
cwd: path.resolve(dirname, 'fixtures'),
Expand Down

0 comments on commit 9d25eac

Please sign in to comment.