@@ -8,6 +8,7 @@ import { getImportIdentifier } from "./get-import-identifier.ts";
88import { getRootStatementPath } from "./get-root-statement-path.ts" ;
99import { isStatementTopLevel } from "./is-statement-top-level.ts" ;
1010import { isPathValid , unwrapPath } from "./paths.ts" ;
11+ import { removeUnusedVariables } from "./remove-unused-variables.ts" ;
1112import type { ImportDefinition } from "./types.ts" ;
1213
1314export interface StateContext {
@@ -124,6 +125,8 @@ function transformFunction(
124125 ] ) ,
125126 ) ;
126127 }
128+
129+ path . scope . crawl ( ) ;
127130}
128131
129132function traceBinding ( path : babel . NodePath , name : string ) : Binding | undefined {
@@ -166,23 +169,6 @@ function transformBindingForServer(ctx: StateContext, binding: Binding) {
166169 }
167170}
168171
169- function treeshake ( path : babel . NodePath , name : string ) : void {
170- const binding = path . scope . getBinding ( name ) ;
171-
172- if ( ! ( binding && binding . references + binding . constantViolations . length > 0 ) ) {
173- if ( isPathValid ( path . parentPath , t . isImportDeclaration ) ) {
174- const parent = path . parentPath ;
175- if ( parent . node . specifiers . length === 1 ) {
176- parent . remove ( ) ;
177- } else {
178- path . remove ( ) ;
179- }
180- } else {
181- path . remove ( ) ;
182- }
183- }
184- }
185-
186172interface State extends babel . PluginPass {
187173 opts : StateContext ;
188174}
@@ -377,20 +363,11 @@ export function directivesPlugin(): babel.PluginObj<State> {
377363 transformFunction ( ctx . opts , path , false ) ;
378364 } ,
379365 } ) ;
380-
381366 program . scope . crawl ( ) ;
382- // Tree-shaking
383- program . traverse ( {
384- ImportDefaultSpecifier ( path ) {
385- treeshake ( path , path . node . local . name ) ;
386- } ,
387- ImportNamespaceSpecifier ( path ) {
388- treeshake ( path , path . node . local . name ) ;
389- } ,
390- ImportSpecifier ( path ) {
391- treeshake ( path , path . node . local . name ) ;
392- } ,
393- } ) ;
367+
368+ if ( ctx . opts . count > 0 ) {
369+ removeUnusedVariables ( program ) ;
370+ }
394371 }
395372 } ,
396373 } ,
0 commit comments