From 59fa61aef1d17f2c7fc2065f59a4c67b358c8ff1 Mon Sep 17 00:00:00 2001 From: Pavel Feldman Date: Wed, 15 Jun 2022 15:14:45 -0800 Subject: [PATCH] fix(ct): relax react import pattern (#14896) --- packages/playwright-test/src/plugins/vitePlugin.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/packages/playwright-test/src/plugins/vitePlugin.ts b/packages/playwright-test/src/plugins/vitePlugin.ts index d1937181fb..42675d017d 100644 --- a/packages/playwright-test/src/plugins/vitePlugin.ts +++ b/packages/playwright-test/src/plugins/vitePlugin.ts @@ -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: '' } }; }