Skip to content

Commit 91afbaa

Browse files
committed
fix: only add needed imports
1 parent 5df4d72 commit 91afbaa

File tree

3 files changed

+52
-22
lines changed

3 files changed

+52
-22
lines changed

src/transform/imports.ts

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
import MagicString from 'magic-string'
2-
import { findStaticImports } from 'mlly'
2+
import { findStaticImports, parseStaticImport } from 'mlly'
33
import { FLYTRAP_PACKAGE_NAME } from '../core/config'
44
import { parse } from '@babel/parser'
55
import { FlytrapConfig } from '../core/types'
66

7+
export function getRequiredExportsForCapture(): string[] {
8+
return ['useFlytrapCall', 'useFlytrapCallAsync', 'useFlytrapFunction', 'setFlytrapConfig']
9+
}
10+
711
export function getCoreExports(): string[] {
812
return [
913
'useFlytrapCall',
@@ -37,18 +41,23 @@ export function addMissingFlytrapImports(s: MagicString) {
3741
const statements = findStaticImports(s.toString()).filter(
3842
(i) => i.specifier === FLYTRAP_PACKAGE_NAME
3943
)
44+
const parsedImports = statements.map((importStatement) => parseStaticImport(importStatement))
45+
const importedFunctions = parsedImports.reduce(
46+
(acc, curr) => [...acc, ...Object.keys(curr.namedImports ?? {})],
47+
[] as string[]
48+
)
49+
const importsToBeAdded = getRequiredExportsForCapture().filter(
50+
(i) => !importedFunctions.includes(i)
51+
)
4052

41-
// Remove existing Flytrap import statements
42-
for (let i = 0; i < statements.length; i++) {
43-
s.remove(statements[i].start, statements[i].end)
53+
if (importsToBeAdded.length > 0) {
54+
const startingIndex = findStartingIndex(s)
55+
s.appendLeft(
56+
startingIndex,
57+
`\n\nimport { ${importsToBeAdded.join(', ')} } from '${FLYTRAP_PACKAGE_NAME}';\n\n`
58+
)
4459
}
4560

46-
const startingIndex = findStartingIndex(s)
47-
48-
s.appendLeft(
49-
startingIndex,
50-
`\n\nimport { ${getCoreExports().join(', ')} } from '${FLYTRAP_PACKAGE_NAME}'\n\n`
51-
)
5261
return s
5362
}
5463

test/transform.test.ts

Lines changed: 33 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { deriveAnonymousFunctionName } from '../src/core/util'
44
import {
55
addMissingFlytrapImports,
66
findStartingIndex,
7-
getCoreExports
7+
getRequiredExportsForCapture
88
} from '../src/transform/imports'
99
import MagicString from 'magic-string'
1010
import { 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

299303
describe('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

356382
describe('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
}, {

transform/package.json

Lines changed: 0 additions & 5 deletions
This file was deleted.

0 commit comments

Comments
 (0)