-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(packages/lazy-fn-vite-plugin): make
dynamic
function detection …
…stricter (#223)
- Loading branch information
1 parent
6135fd7
commit 7626ed8
Showing
7 changed files
with
160 additions
and
76 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,25 @@ | ||
import type { | ||
Identifier, | ||
ImportDeclaration, | ||
ImportSpecifier, | ||
import { | ||
isIdentifier, | ||
isImportDeclaration, | ||
isImportSpecifier, | ||
} from '@babel/types' | ||
|
||
import { TUONO_MAIN_PACKAGE, TUONO_DYNAMIC_FN_ID } from './constants' | ||
|
||
export const isTuonoDynamicFnImported = ( | ||
path: babel.NodePath<ImportSpecifier>, | ||
): boolean => { | ||
if ((path.node.imported as Identifier).name !== TUONO_DYNAMIC_FN_ID) { | ||
return false | ||
} | ||
if ( | ||
(path.parentPath.node as ImportDeclaration).source.value !== | ||
TUONO_MAIN_PACKAGE | ||
) { | ||
return false | ||
} | ||
return true | ||
/** | ||
* By a given AST Node path returns true if the path involves an import specifier | ||
* importing {@link TUONO_DYNAMIC_FN_ID} | ||
*/ | ||
export const isTuonoDynamicFnImported = (path: babel.NodePath): boolean => { | ||
// If the node isn't an import declaration there is no need to process it | ||
if (!isImportDeclaration(path.parentPath?.node)) return false | ||
|
||
// if the import doesn't import from 'tuono' we don't need to process it | ||
if (path.parentPath.node.source.value !== TUONO_MAIN_PACKAGE) return false | ||
|
||
// ensure that we are processing an import specifier | ||
if (!isImportSpecifier(path.node)) return false | ||
|
||
// finally check if the imported item is `TUONO_DYNAMIC_FN_ID` | ||
return isIdentifier(path.node.imported, { name: TUONO_DYNAMIC_FN_ID }) | ||
} |
9 changes: 9 additions & 0 deletions
9
packages/lazy-fn-vite-plugin/tests/sources/dynamic-only/client.expected.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import React, { Suspense, type JSX } from "react"; | ||
import { __tuono__internal__lazyLoadComponent as dynamic } from "tuono"; | ||
const DynamicComponent = dynamic(() => import("../components/DynamicComponent")); | ||
const Loading = (): JSX.Element => <>Loading</>; | ||
export default function IndexPage(): JSX.Element { | ||
return <Suspense fallback={<Loading />}> | ||
<DynamicComponent /> | ||
</Suspense>; | ||
} |
8 changes: 8 additions & 0 deletions
8
packages/lazy-fn-vite-plugin/tests/sources/dynamic-only/server.expected.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import React, { Suspense, type JSX } from "react"; | ||
import DynamicComponent from "../components/DynamicComponent"; | ||
const Loading = (): JSX.Element => <>Loading</>; | ||
export default function IndexPage(): JSX.Element { | ||
return <Suspense fallback={<Loading />}> | ||
<DynamicComponent /> | ||
</Suspense>; | ||
} |
14 changes: 14 additions & 0 deletions
14
packages/lazy-fn-vite-plugin/tests/sources/dynamic-only/source.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import React, { Suspense, type JSX } from "react"; | ||
import { dynamic } from "tuono"; | ||
|
||
const DynamicComponent = dynamic(() => import("../components/DynamicComponent")) | ||
|
||
const Loading = (): JSX.Element => <>Loading</> | ||
|
||
export default function IndexPage(): JSX.Element { | ||
return ( | ||
<Suspense fallback={<Loading />}> | ||
<DynamicComponent /> | ||
</Suspense> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters