diff --git a/src/core/transform.ts b/src/core/transform.ts index b64bd4d..00146ab 100644 --- a/src/core/transform.ts +++ b/src/core/transform.ts @@ -3,7 +3,7 @@ import { ImportInfo, TransformOptions } from '../types' const excludeRegex = [ // imported from other module - /\bimport\s*\{([\s\S]*?)\}\s*from\b/g, + /\bimport\s*([\w_$]*?),?\s*\{([\s\S]*?)\}\s*from\b/g, // defined as function /\bfunction\s*([\s\S]+?)\s*\(/g, // defined as local variable @@ -16,7 +16,7 @@ export function transform(code: string, id: string, { matchRE, imports }: Transf // remove those already defined for (const regex of excludeRegex) { Array.from(code.matchAll(regex)) - .flatMap(i => i[1]?.split(',') || []) + .flatMap(i => [...(i[1]?.split(',') || []), ...(i[2]?.split(',') || [])]) .forEach(i => matched.delete(i.trim())) } diff --git a/test/__snapshots__/transform.test.ts.snap b/test/__snapshots__/transform.test.ts.snap index 8e53273..d0ea0b3 100644 --- a/test/__snapshots__/transform.test.ts.snap +++ b/test/__snapshots__/transform.test.ts.snap @@ -5,6 +5,11 @@ exports[`transform fixtures custom.js 1`] = ` " `; +exports[`transform fixtures non-target.js 1`] = ` +"useNonTarget() +" +`; + exports[`transform fixtures react.jsx 1`] = ` "import { useState } from 'react';export function Component() { const [count, setCount] = useState() @@ -14,6 +19,17 @@ exports[`transform fixtures react.jsx 1`] = ` " `; +exports[`transform fixtures react-existing.jsx 1`] = ` +"import React, { useState } from 'react'; + +export function Component() { + const [count, setCount] = useState() + + return
{ count }
+} +" +`; + exports[`transform fixtures vue.js 1`] = ` "import { ref } from 'vue';const a = ref(0) " diff --git a/test/fixtures/react-existing.jsx b/test/fixtures/react-existing.jsx new file mode 100644 index 0000000..18710fb --- /dev/null +++ b/test/fixtures/react-existing.jsx @@ -0,0 +1,7 @@ +import React, { useState } from 'react'; + +export function Component() { + const [count, setCount] = useState() + + return
{ count }
+}