22// https://github.com/vercel/next.js/blob/canary/packages/next/build/babel/plugins/next-ssg-transform.ts
33// This is adapted to work with routeData functions. It can be run in two modes, one which preserves the routeData and the Component in the same file, and one which creates a
44
5+ import type { NodePath , PluginObj , PluginPass } from "@babel/core" ;
6+ import type * as Babel from '@babel/core' ;
7+ import { Binding } from "@babel/traverse" ;
58import { basename } from "pathe" ;
6- import { Plugin , ViteDevServer } from "vite" ;
9+ import type { Plugin , ResolvedConfig , ViteDevServer } from "vite" ;
710
8- function treeShakeTransform ( { types : t } ) {
9- function getIdentifier ( path ) {
11+ type State = Omit < PluginPass , "opts" > & { opts : { pick : string [ ] } , refs : Set < any > , done : boolean }
12+
13+ function treeShakeTransform ( { types : t } : typeof Babel ) : PluginObj < State > {
14+ function getIdentifier ( path : any ) {
1015 const parentPath = path . parentPath ;
1116 if ( parentPath . type === "VariableDeclarator" ) {
1217 const pp = parentPath ;
@@ -24,9 +29,9 @@ function treeShakeTransform({ types: t }) {
2429 return path . node . id && path . node . id . type === "Identifier" ? path . get ( "id" ) : null ;
2530 }
2631
27- function isIdentifierReferenced ( ident ) {
28- const b = ident . scope . getBinding ( ident . node . name ) ;
29- if ( b && b . referenced ) {
32+ function isIdentifierReferenced ( ident : any ) {
33+ const b : Binding | undefined = ident . scope . getBinding ( ident . node . name ) ;
34+ if ( b ? .referenced ) {
3035 if ( b . path . type === "FunctionDeclaration" ) {
3136 return ! b . constantViolations
3237 . concat ( b . referencePaths )
@@ -36,13 +41,13 @@ function treeShakeTransform({ types: t }) {
3641 }
3742 return false ;
3843 }
39- function markFunction ( path , state ) {
44+ function markFunction ( path : any , state : any ) {
4045 const ident = getIdentifier ( path ) ;
4146 if ( ident && ident . node && isIdentifierReferenced ( ident ) ) {
4247 state . refs . add ( ident ) ;
4348 }
4449 }
45- function markImport ( path , state ) {
50+ function markImport ( path : any , state : any ) {
4651 const local = path . get ( "local" ) ;
4752 if ( isIdentifierReferenced ( local ) ) {
4853 state . refs . add ( local ) ;
@@ -57,34 +62,34 @@ function treeShakeTransform({ types: t }) {
5762 state . done = false ;
5863 path . traverse (
5964 {
60- VariableDeclarator ( variablePath , variableState ) {
65+ VariableDeclarator ( variablePath , variableState : any ) {
6166 if ( variablePath . node . id . type === "Identifier" ) {
6267 const local = variablePath . get ( "id" ) ;
6368 if ( isIdentifierReferenced ( local ) ) {
6469 variableState . refs . add ( local ) ;
6570 }
6671 } else if ( variablePath . node . id . type === "ObjectPattern" ) {
6772 const pattern = variablePath . get ( "id" ) ;
68- const properties = pattern . get ( "properties" ) ;
73+ const properties = pattern . get ( "properties" ) as Array < NodePath > ;
6974 properties . forEach ( p => {
7075 const local = p . get (
7176 p . node . type === "ObjectProperty"
7277 ? "value"
7378 : p . node . type === "RestElement"
7479 ? "argument"
7580 : ( function ( ) {
76- throw new Error ( "invariant" ) ;
77- } ) ( )
81+ throw new Error ( "invariant" ) ;
82+ } ) ( )
7883 ) ;
7984 if ( isIdentifierReferenced ( local ) ) {
8085 variableState . refs . add ( local ) ;
8186 }
8287 } ) ;
8388 } else if ( variablePath . node . id . type === "ArrayPattern" ) {
8489 const pattern = variablePath . get ( "id" ) ;
85- const elements = pattern . get ( "elements" ) ;
90+ const elements = pattern . get ( "elements" ) as Array < NodePath > ;
8691 elements . forEach ( e => {
87- let local ;
92+ let local : NodePath < any > ;
8893 if ( e . node && e . node . type === "Identifier" ) {
8994 local = e ;
9095 } else if ( e . node && e . node . type === "RestElement" ) {
@@ -131,14 +136,14 @@ function treeShakeTransform({ types: t }) {
131136 }
132137 switch ( decl . node . type ) {
133138 case "FunctionDeclaration" : {
134- const name = decl . node . id . name ;
135- if ( state . opts . pick && ! state . opts . pick . includes ( name ) ) {
139+ const name = decl . node . id ? .name ;
140+ if ( name && state . opts . pick && ! state . opts . pick . includes ( name ) ) {
136141 exportNamedPath . remove ( ) ;
137142 }
138143 break ;
139144 }
140145 case "VariableDeclaration" : {
141- const inner = decl . get ( "declarations" ) ;
146+ const inner = decl . get ( "declarations" ) as Array < NodePath < any > > ;
142147 inner . forEach ( d => {
143148 if ( d . node . id . type !== "Identifier" ) {
144149 return ;
@@ -175,8 +180,8 @@ function treeShakeTransform({ types: t }) {
175180 ) ;
176181
177182 const refs = state . refs ;
178- let count ;
179- function sweepFunction ( sweepPath ) {
183+ let count = 0 ;
184+ const sweepFunction = ( sweepPath : any ) => {
180185 const ident = getIdentifier ( sweepPath ) ;
181186 if ( ident && ident . node && refs . has ( ident ) && ! isIdentifierReferenced ( ident ) ) {
182187 ++ count ;
@@ -190,7 +195,7 @@ function treeShakeTransform({ types: t }) {
190195 }
191196 }
192197 }
193- function sweepImport ( sweepPath ) {
198+ function sweepImport ( sweepPath : any ) {
194199 const local = sweepPath . get ( "local" ) ;
195200 if ( refs . has ( local ) && ! isIdentifierReferenced ( local ) ) {
196201 ++ count ;
@@ -222,8 +227,8 @@ function treeShakeTransform({ types: t }) {
222227 : p . node . type === "RestElement"
223228 ? "argument"
224229 : ( function ( ) {
225- throw new Error ( "invariant" ) ;
226- } ) ( )
230+ throw new Error ( "invariant" ) ;
231+ } ) ( )
227232 ) ;
228233 if ( refs . has ( local ) && ! isIdentifierReferenced ( local ) ) {
229234 ++ count ;
@@ -238,7 +243,7 @@ function treeShakeTransform({ types: t }) {
238243 const beforeCount = count ;
239244 const elements = pattern . get ( "elements" ) ;
240245 elements . forEach ( e => {
241- let local ;
246+ let local : NodePath < any > | undefined ;
242247 if ( e . node && e . node . type === "Identifier" ) {
243248 local = e ;
244249 } else if ( e . node && e . node . type === "RestElement" ) {
@@ -271,8 +276,7 @@ function treeShakeTransform({ types: t }) {
271276}
272277
273278export function treeShake ( ) : Plugin {
274- /** @type {import('../vite-dev.d.ts').ViteConfig } */
275- let config ;
279+ let config : ResolvedConfig ;
276280 let cache : Record < string , any > = { } ;
277281 let server : ViteDevServer ;
278282
@@ -348,7 +352,7 @@ export function treeShake(): Plugin {
348352 if ( ! ext ) return ;
349353 if ( query . has ( "pick" ) && [ "js" , "jsx" , "ts" , "tsx" ] . includes ( ext ) ) {
350354 const transformed = await transform ( id , code ) ;
351- if ( ! transformed ) return ;
355+ if ( ! transformed ?. code ) return ;
352356
353357 cache [ path ] ??= { } ;
354358 cache [ path ] [ id ] = transformed . code ;
0 commit comments