Skip to content

Commit

Permalink
fix: detect for default and named exports, close #10
Browse files Browse the repository at this point in the history
  • Loading branch information
antfu committed Aug 25, 2021
1 parent 5ed289f commit d9caa35
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/core/transform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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()))
}

Expand Down
16 changes: 16 additions & 0 deletions test/__snapshots__/transform.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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 <div>{ count }</div>
}
"
`;

exports[`transform fixtures vue.js 1`] = `
"import { ref } from 'vue';const a = ref(0)
"
Expand Down
7 changes: 7 additions & 0 deletions test/fixtures/react-existing.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import React, { useState } from 'react';

export function Component() {
const [count, setCount] = useState()

return <div>{ count }</div>
}

0 comments on commit d9caa35

Please sign in to comment.