fix(ct): relax react import pattern (#14896)

This commit is contained in:
Pavel Feldman 2022-06-15 15:14:45 -08:00 committed by GitHub
parent 1ed2e46f07
commit 59fa61aef1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -36,6 +36,7 @@ type CtConfig = {
ctViteConfig?: InlineConfig;
};
const importReactRE = /(^|\n)import\s+(\*\s+as\s+)?React(,|\s+)/;
export function createPlugin(
registerSourceFile: string,
@ -274,7 +275,7 @@ function vitePlugin(registerSource: string, relativeTemplateDir: string, buildIn
}
// Vite React plugin will do this for .jsx files, but not .js files.
if (id.endsWith('.js') && content.includes('React.createElement') && !content.includes('import React')) {
if (id.endsWith('.js') && content.includes('React.createElement') && !content.match(importReactRE)) {
const code = `import React from 'react';\n${content}`;
return { code, map: { mappings: '' } };
}