From bf1df9678fc5d79f246ca288626128735b570c72 Mon Sep 17 00:00:00 2001 From: Pavel Feldman Date: Mon, 15 May 2023 15:12:10 -0700 Subject: [PATCH] chore: do not load code for babeling twice (#23025) --- .../playwright-test/bundles/babel/src/babelBundleImpl.ts | 8 ++++---- packages/playwright-test/src/common/babelBundle.ts | 2 +- packages/playwright-test/src/common/transform.ts | 8 ++++---- 3 files changed, 9 insertions(+), 9 deletions(-) diff --git a/packages/playwright-test/bundles/babel/src/babelBundleImpl.ts b/packages/playwright-test/bundles/babel/src/babelBundleImpl.ts index 4e2d8b6bc4..0f4ab2979b 100644 --- a/packages/playwright-test/bundles/babel/src/babelBundleImpl.ts +++ b/packages/playwright-test/bundles/babel/src/babelBundleImpl.ts @@ -83,9 +83,9 @@ function babelTransformOptions(isTypeScript: boolean, isModule: boolean, plugins // breaks playwright evaluates. setPublicClassFields: true, }, - presets: [ + presets: isTypeScript ? [ [require('@babel/preset-typescript'), { onlyRemoveTypeImports: false }], - ], + ] : [], plugins: [ ...pluginsPrologue.map(([name, options]) => [require(name), options]), ...plugins, @@ -98,7 +98,7 @@ function babelTransformOptions(isTypeScript: boolean, isModule: boolean, plugins let isTransforming = false; -export function babelTransform(filename: string, isTypeScript: boolean, isModule: boolean, pluginsPrologue: [string, any?][], pluginsEpilogue: [string, any?][]): BabelFileResult { +export function babelTransform(code: string, filename: string, isTypeScript: boolean, isModule: boolean, pluginsPrologue: [string, any?][], pluginsEpilogue: [string, any?][]): BabelFileResult { if (isTransforming) return {}; @@ -106,7 +106,7 @@ export function babelTransform(filename: string, isTypeScript: boolean, isModule isTransforming = true; try { const options = babelTransformOptions(isTypeScript, isModule, pluginsPrologue, pluginsEpilogue); - return babel.transformFileSync(filename, options)!; + return babel.transform(code, { filename, ...options })!; } finally { isTransforming = false; } diff --git a/packages/playwright-test/src/common/babelBundle.ts b/packages/playwright-test/src/common/babelBundle.ts index 278dd500df..632e9e3d9d 100644 --- a/packages/playwright-test/src/common/babelBundle.ts +++ b/packages/playwright-test/src/common/babelBundle.ts @@ -21,7 +21,7 @@ export const types: typeof import('../../bundles/babel/node_modules/@types/babel export const parse: typeof import('../../bundles/babel/node_modules/@babel/parser/typings/babel-parser').parse = require('./babelBundleImpl').parse; export const traverse: typeof import('../../bundles/babel/node_modules/@types/babel__traverse').default = require('./babelBundleImpl').traverse; export type BabelPlugin = [string, any?]; -export type BabelTransformFunction = (filename: string, isTypeScript: boolean, isModule: boolean, pluginsPrefix: BabelPlugin[], pluginsSuffix: BabelPlugin[]) => BabelFileResult; +export type BabelTransformFunction = (code: string, filename: string, isTypeScript: boolean, isModule: boolean, pluginsPrefix: BabelPlugin[], pluginsSuffix: BabelPlugin[]) => BabelFileResult; export const babelTransform: BabelTransformFunction = require('./babelBundleImpl').babelTransform; export type { NodePath, types as T } from '../../bundles/babel/node_modules/@types/babel__core'; export type { BabelAPI } from '../../bundles/babel/node_modules/@types/babel__helper-plugin-utils'; diff --git a/packages/playwright-test/src/common/transform.ts b/packages/playwright-test/src/common/transform.ts index e97c41bb72..c43be7c3fc 100644 --- a/packages/playwright-test/src/common/transform.ts +++ b/packages/playwright-test/src/common/transform.ts @@ -134,15 +134,15 @@ export function resolveHook(filename: string, specifier: string): string | undef } } -export function transformHook(preloadedCode: string, filename: string, moduleUrl?: string): string { - const isTypeScript = filename.endsWith('.ts') || filename.endsWith('.tsx'); +export function transformHook(originalCode: string, filename: string, moduleUrl?: string): string { + const isTypeScript = filename.endsWith('.ts') || filename.endsWith('.tsx') || filename.endsWith('.mts') || filename.endsWith('.cts'); const hasPreprocessor = process.env.PW_TEST_SOURCE_TRANSFORM && process.env.PW_TEST_SOURCE_TRANSFORM_SCOPE && process.env.PW_TEST_SOURCE_TRANSFORM_SCOPE.split(pathSeparator).some(f => filename.startsWith(f)); const pluginsPrologue = babelPlugins; const pluginsEpilogue = hasPreprocessor ? [[process.env.PW_TEST_SOURCE_TRANSFORM!]] as BabelPlugin[] : []; - const hash = calculateHash(preloadedCode, filename, !!moduleUrl, pluginsPrologue, pluginsEpilogue); + const hash = calculateHash(originalCode, filename, !!moduleUrl, pluginsPrologue, pluginsEpilogue); const { cachedCode, addToCache } = getFromCompilationCache(filename, hash, moduleUrl); if (cachedCode) return cachedCode; @@ -152,7 +152,7 @@ export function transformHook(preloadedCode: string, filename: string, moduleUrl process.env.BROWSERSLIST_IGNORE_OLD_DATA = 'true'; const { babelTransform }: { babelTransform: BabelTransformFunction } = require('./babelBundle'); - const { code, map } = babelTransform(filename, isTypeScript, !!moduleUrl, pluginsPrologue, pluginsEpilogue); + const { code, map } = babelTransform(originalCode, filename, isTypeScript, !!moduleUrl, pluginsPrologue, pluginsEpilogue); if (code) addToCache!(code, map); return code || '';