Skip to content

Commit

Permalink
fix(babel-helpers): support finding in-file helpers
Browse files Browse the repository at this point in the history
  • Loading branch information
pionxzh committed Oct 2, 2023
1 parent bf9cfc7 commit 4cef84a
Showing 1 changed file with 19 additions and 1 deletion.
20 changes: 19 additions & 1 deletion packages/unminify/src/utils/import.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export function findHelperLocals(
moduleName: string,
moduleEsmName?: string,
): string[] {
const { j, root } = context
const { j, root, filename } = context

const importManager = new ImportManager()
importManager.collectEsModuleImport(j, root)
Expand All @@ -40,6 +40,18 @@ export function findHelperLocals(
if (moduleMapping && moduleMeta) {
const moduleMappingEntries = Object.entries(moduleMapping)

// helpers in current module
const currentModuleId = moduleMappingEntries.find(([_, path]) => path === filename)?.[0] || filename
const currentTags = moduleMeta[currentModuleId]?.tags
if (currentTags) {
Object.entries(currentTags).forEach(([local, tags]) => {
if (tags.includes(moduleName)) {
result.push(local)
}
})
}

// helpers in other modules
imports.forEach((imported) => {
const source = moduleMappingEntries.find(([_, path]) => path === imported.source.toString())?.[0] || imported.source
const targetModule = moduleMeta[source]
Expand Down Expand Up @@ -107,6 +119,12 @@ export function removeHelperImport(j: JSCodeshift, scope: Scope | null, name: st
return
}

const functionDeclaration = j(declaration).closest(j.FunctionDeclaration)
if (functionDeclaration.size() === 1) {
functionDeclaration.remove()
return
}

const variableDeclarator = j(declaration).closest(j.VariableDeclarator)
if (variableDeclarator.size() === 1) {
const variableDeclaration = variableDeclarator.closest(j.VariableDeclaration)
Expand Down

0 comments on commit 4cef84a

Please sign in to comment.