@@ -4,7 +4,7 @@ import { deriveAnonymousFunctionName } from '../src/core/util'
44import {
55 addMissingFlytrapImports ,
66 findStartingIndex ,
7- getCoreExports
7+ getRequiredExportsForCapture
88} from '../src/transform/imports'
99import MagicString from 'magic-string'
1010import { FLYTRAP_PACKAGE_NAME } from '../src/core/config'
@@ -292,8 +292,12 @@ it('finds correct place to add code to (after directives)', () => {
292292
293293// function flytrapImport
294294
295- const getFlytrapImports = ( ) => {
295+ /* const getFlytrapImports = () => {
296296 return `import { ${getCoreExports().join(', ')} } from '${FLYTRAP_PACKAGE_NAME}'`
297+ }*/
298+
299+ const getFlytrapRequiredImports = ( ) => {
300+ return `import { ${ getRequiredExportsForCapture ( ) . join ( ', ' ) } } from '${ FLYTRAP_PACKAGE_NAME } ';`
297301}
298302
299303describe ( 'addMissingImports transform' , ( ) => {
@@ -320,7 +324,7 @@ describe('addMissingImports transform', () => {
320324 toOneLine ( `
321325 'use client';
322326
323- ${ getFlytrapImports ( ) }
327+ ${ getFlytrapRequiredImports ( ) }
324328
325329 function foo() {}
326330 ` )
@@ -332,7 +336,7 @@ describe('addMissingImports transform', () => {
332336 // comment
333337 'use client';
334338
335- ${ getFlytrapImports ( ) }
339+ ${ getFlytrapRequiredImports ( ) }
336340
337341 function foo() {}
338342 ` )
@@ -343,14 +347,36 @@ describe('addMissingImports transform', () => {
343347 toOneLine ( addMissingFlytrapImports ( new MagicString ( wrongUseClientFixture ) ) . toString ( ) )
344348 ) . toBe (
345349 toOneLine ( `
346- ${ getFlytrapImports ( ) }
350+ ${ getFlytrapRequiredImports ( ) }
347351 function foo() {}
348352 'use client';
349353 ` )
350354 )
351355 } )
352356
353- it . todo ( 'adds only needed imports' )
357+ it ( 'adds only needed imports' , ( ) => {
358+ const fixture = `import { capture } from 'useflytrap';`
359+ const transformed = addMissingFlytrapImports ( new MagicString ( fixture ) )
360+ expect ( toOneLine ( transformed . toString ( ) ) ) . toEqual (
361+ toOneLine ( `
362+ ${ getFlytrapRequiredImports ( ) }
363+ import { capture } from 'useflytrap';
364+ ` )
365+ )
366+
367+ const fixtureWithRequiredImports = `
368+ import { capture, useFlytrapCall } from 'useflytrap';
369+ `
370+ const transformedWithRequired = addMissingFlytrapImports (
371+ new MagicString ( fixtureWithRequiredImports )
372+ )
373+ expect ( toOneLine ( transformedWithRequired . toString ( ) ) ) . toEqual (
374+ toOneLine ( `
375+ import { useFlytrapCallAsync, useFlytrapFunction, setFlytrapConfig } from 'useflytrap';
376+ import { capture, useFlytrapCall } from 'useflytrap';
377+ ` )
378+ )
379+ } )
354380} )
355381
356382describe ( 'useFlytrapCall(Async) transform' , ( ) => {
@@ -580,7 +606,7 @@ it('transforms .vue files', async () => {
580606 expect ( toOneLine ( ( await unpluginOptions . transform ( fixture , '/app.vue' ) ) . code ) ) . toEqual (
581607 toOneLine ( `
582608 <script setup>
583- import { useFlytrapCall, useFlytrapCallAsync, useFlytrapFunction, setFlytrapConfig, capture, identify, encrypt, decrypt, defineFlytrapConfig, generateKeyPair } from 'useflytrap'
609+ ${ getFlytrapRequiredImports ( ) }
584610
585611 const foo = useFlytrapFunction(function foo() {
586612 }, {
0 commit comments