Skip to content

Commit ea92b0e

Browse files
authored
Merge pull request #4 from barelyhuman/3-unused-imports
Fix: Adex Loader Plugin | Add in handling for unused imports that add a default key handler
2 parents 887dec5 + 4f2c14d commit ea92b0e

File tree

3 files changed

+96
-23
lines changed

3 files changed

+96
-23
lines changed

adex/package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@
3131
},
3232
"devDependencies": {
3333
"@barelyhuman/prettier-config": "^1.1.0",
34+
"@types/babel__generator": "^7.6.8",
35+
"@types/babel__parser": "^7.1.1",
36+
"@types/babel__traverse": "^7.20.5",
3437
"bumpp": "^9.2.1",
3538
"vite": "^5.0.10"
3639
},

adex/src/lib/adex-loader.js

Lines changed: 65 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -22,31 +22,77 @@ export function adexLoader() {
2222
treeShaking: true,
2323
})
2424

25-
const ast = parse(transformResult.code, {
26-
sourceType: 'module',
25+
const ast = moduleParser(transformResult.code)
26+
const astWithoutLoader = removeLoaderFromAST(ast)
27+
const astWithoutImports = removeUnusedImports(astWithoutLoader)
28+
console.log({
29+
ast,
30+
astWithoutLoader,
31+
astWithoutImports,
2732
})
2833

29-
traverse(ast, {
30-
Identifier(path) {
31-
if (path.node.name !== 'loader') return
34+
return generate(astWithoutImports)
35+
},
36+
}
37+
}
3238

33-
if (path.parent) {
34-
if (path.parent.type === 'ExportSpecifier') {
35-
path.parentPath.remove()
36-
}
39+
function moduleParser(source) {
40+
return parse(source, {
41+
sourceType: 'module',
42+
})
43+
}
3744

38-
if (path.parentPath.parentPath) {
39-
if (
40-
path.parentPath.parentPath.node.type === 'VariableDeclaration'
41-
) {
42-
path.parentPath.parentPath.remove()
43-
}
44-
}
45+
function removeLoaderFromAST(ast) {
46+
traverse(ast, {
47+
Identifier(path) {
48+
if (path.node.name !== 'loader') return
49+
50+
if (path.parent) {
51+
if (path.parent.type === 'ExportSpecifier') {
52+
path.parentPath.remove()
53+
}
54+
55+
if (path.parentPath.parentPath) {
56+
if (path.parentPath.parentPath.node.type === 'VariableDeclaration') {
57+
path.parentPath.parentPath.remove()
4558
}
46-
},
47-
})
59+
}
60+
}
61+
},
62+
})
63+
return moduleParser(generate(ast).code)
64+
}
65+
66+
function removeUnusedImports(ast) {
67+
const paths = new Map()
4868

49-
return generate(ast)
69+
const visitor = {
70+
ImportSpecifier(path) {
71+
paths.set(path.node.local.name, path)
72+
},
73+
ImportDefaultSpecifier(path) {
74+
paths.set(path.node.local.name, path)
75+
},
76+
Program: {
77+
exit(path) {
78+
for (let [key, path] of paths.entries()) {
79+
const binding = path.scope.bindings[key]
80+
if (!binding) continue
81+
if (binding.references !== 0) continue
82+
if (path.node.type == 'ImportDefaultSpecifier') {
83+
path.parentPath.remove()
84+
} else {
85+
if (path.parent.specifiers.length === 1) {
86+
path.parentPath.remove()
87+
} else {
88+
path.remove()
89+
}
90+
}
91+
}
92+
},
5093
},
5194
}
95+
96+
traverse(ast, visitor)
97+
return moduleParser(generate(ast).code)
5298
}

pnpm-lock.yaml

Lines changed: 28 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)