Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

doc(sandpack): improved logic for react import #3582

Merged
merged 8 commits into from
Aug 29, 2024
8 changes: 6 additions & 2 deletions apps/docs/components/sandpack/use-sandpack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,13 +100,17 @@ export const useSandpack = ({
let fileContent = files[key] as string;

// Check if the file content includes 'React' import statements, if not, add it
if (!fileContent.includes("from 'react'") && !fileContent.includes('from "react"')) {
if (
fileContent.includes("React.") &&
!fileContent.includes("from 'react'") &&
!fileContent.includes('from "react"')
) {
fileContent = `${importReact}\n${fileContent}\n`;
}

// Check if file content includes any other dependencies, if yes, add it to dependencies
const importRegex = /import .* from ["'](.*)["']/g;
let match;
let match: RegExpExecArray | null;

while ((match = importRegex.exec(fileContent)) !== null) {
const dependencyName = match[1];
Expand Down