Skip to content

Commit

Permalink
fix: distinguish different build contexts
Browse files Browse the repository at this point in the history
  • Loading branch information
sxzz committed Sep 9, 2024
1 parent a7f61d7 commit 1259581
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 11 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@
"js-tokens": "^9.0.0",
"picocolors": "^1.0.1",
"pkg-types": "^1.2.0",
"unplugin": "^1.12.2"
"unplugin": "^1.14.0"
},
"devDependencies": {
"@sxzz/eslint-config": "^4.1.0",
Expand Down
22 changes: 14 additions & 8 deletions pnpm-lock.yaml

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

32 changes: 30 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@ 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 {
createUnplugin,
type UnpluginBuildContext,
type UnpluginInstance,
} from 'unplugin'
import { resolveOptions, type DepKind, type Options } from './core/options'

export type { DepKind, Options }
Expand All @@ -14,9 +18,25 @@ export const Unused: UnpluginInstance<Options | undefined, false> =
createUnplugin((rawOptions = {}) => {
const options = resolveOptions(rawOptions)
const filter = createFilter(options.include, options.exclude)
const deps = new Set<string>()
const depsRegex: Record<string, RegExp> = {}
const depsState = new Map<object, Set<string>>()
let pkgPath: string
const defaultKey: object = {}

function getBuildId(context: UnpluginBuildContext): object {
const native = context.getNativeBuildContext?.()
if (!native) return defaultKey
if (native.framework === 'esbuild') {
return native.build
}
if (native.framework === 'rspack' || native.framework === 'webpack') {
return native.compiler
}
if (native.framework === 'farm') {
return native.context
}
return defaultKey
}

const name = 'unplugin-unused'
return {
Expand All @@ -28,6 +48,7 @@ export const Unused: UnpluginInstance<Options | undefined, false> =
pkgPath = path.resolve(await resolvePackageJSON(options.root))
const pkg = await readPackageJSON(pkgPath)

const deps = new Set<string>()
for (const kind of options.depKinds) {
const dependencies = Object.keys(pkg[kind] || {})
for (const dep of dependencies) {
Expand All @@ -39,6 +60,9 @@ export const Unused: UnpluginInstance<Options | undefined, false> =
depsRegex[dep] = new RegExp(`["']${escapeStringRegexp(dep)}['"\\/]`)
}
}

const buildId = getBuildId(this)
depsState.set(buildId, deps)
},

transformInclude(id) {
Expand All @@ -47,6 +71,7 @@ export const Unused: UnpluginInstance<Options | undefined, false> =

transform(code, id): undefined {
const tokens = jsTokens(code, { jsx: /\.[jt]sx?$/.test(id) })
const deps = depsState.get(getBuildId(this))!
for (const { type, value } of tokens) {
if (type.endsWith('Comment')) continue
for (const dep of deps) {
Expand All @@ -60,6 +85,9 @@ export const Unused: UnpluginInstance<Options | undefined, false> =
},

buildEnd() {
const id = getBuildId(this)
const deps = depsState.get(id)!
depsState.delete(id)
if (deps.size) {
const message =
`Unused ${pc.cyan(deps.size)} dependencies found: \n\n` +
Expand Down

0 comments on commit 1259581

Please sign in to comment.