chore: do not load code for babeling twice (#23025)
This commit is contained in:
parent
edd003c230
commit
bf1df9678f
|
|
@ -83,9 +83,9 @@ function babelTransformOptions(isTypeScript: boolean, isModule: boolean, plugins
|
||||||
// breaks playwright evaluates.
|
// breaks playwright evaluates.
|
||||||
setPublicClassFields: true,
|
setPublicClassFields: true,
|
||||||
},
|
},
|
||||||
presets: [
|
presets: isTypeScript ? [
|
||||||
[require('@babel/preset-typescript'), { onlyRemoveTypeImports: false }],
|
[require('@babel/preset-typescript'), { onlyRemoveTypeImports: false }],
|
||||||
],
|
] : [],
|
||||||
plugins: [
|
plugins: [
|
||||||
...pluginsPrologue.map(([name, options]) => [require(name), options]),
|
...pluginsPrologue.map(([name, options]) => [require(name), options]),
|
||||||
...plugins,
|
...plugins,
|
||||||
|
|
@ -98,7 +98,7 @@ function babelTransformOptions(isTypeScript: boolean, isModule: boolean, plugins
|
||||||
|
|
||||||
let isTransforming = false;
|
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)
|
if (isTransforming)
|
||||||
return {};
|
return {};
|
||||||
|
|
||||||
|
|
@ -106,7 +106,7 @@ export function babelTransform(filename: string, isTypeScript: boolean, isModule
|
||||||
isTransforming = true;
|
isTransforming = true;
|
||||||
try {
|
try {
|
||||||
const options = babelTransformOptions(isTypeScript, isModule, pluginsPrologue, pluginsEpilogue);
|
const options = babelTransformOptions(isTypeScript, isModule, pluginsPrologue, pluginsEpilogue);
|
||||||
return babel.transformFileSync(filename, options)!;
|
return babel.transform(code, { filename, ...options })!;
|
||||||
} finally {
|
} finally {
|
||||||
isTransforming = false;
|
isTransforming = false;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -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 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 const traverse: typeof import('../../bundles/babel/node_modules/@types/babel__traverse').default = require('./babelBundleImpl').traverse;
|
||||||
export type BabelPlugin = [string, any?];
|
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 const babelTransform: BabelTransformFunction = require('./babelBundleImpl').babelTransform;
|
||||||
export type { NodePath, types as T } from '../../bundles/babel/node_modules/@types/babel__core';
|
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';
|
export type { BabelAPI } from '../../bundles/babel/node_modules/@types/babel__helper-plugin-utils';
|
||||||
|
|
|
||||||
|
|
@ -134,15 +134,15 @@ export function resolveHook(filename: string, specifier: string): string | undef
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export function transformHook(preloadedCode: string, filename: string, moduleUrl?: string): string {
|
export function transformHook(originalCode: string, filename: string, moduleUrl?: string): string {
|
||||||
const isTypeScript = filename.endsWith('.ts') || filename.endsWith('.tsx');
|
const isTypeScript = filename.endsWith('.ts') || filename.endsWith('.tsx') || filename.endsWith('.mts') || filename.endsWith('.cts');
|
||||||
const hasPreprocessor =
|
const hasPreprocessor =
|
||||||
process.env.PW_TEST_SOURCE_TRANSFORM &&
|
process.env.PW_TEST_SOURCE_TRANSFORM &&
|
||||||
process.env.PW_TEST_SOURCE_TRANSFORM_SCOPE &&
|
process.env.PW_TEST_SOURCE_TRANSFORM_SCOPE &&
|
||||||
process.env.PW_TEST_SOURCE_TRANSFORM_SCOPE.split(pathSeparator).some(f => filename.startsWith(f));
|
process.env.PW_TEST_SOURCE_TRANSFORM_SCOPE.split(pathSeparator).some(f => filename.startsWith(f));
|
||||||
const pluginsPrologue = babelPlugins;
|
const pluginsPrologue = babelPlugins;
|
||||||
const pluginsEpilogue = hasPreprocessor ? [[process.env.PW_TEST_SOURCE_TRANSFORM!]] as BabelPlugin[] : [];
|
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);
|
const { cachedCode, addToCache } = getFromCompilationCache(filename, hash, moduleUrl);
|
||||||
if (cachedCode)
|
if (cachedCode)
|
||||||
return cachedCode;
|
return cachedCode;
|
||||||
|
|
@ -152,7 +152,7 @@ export function transformHook(preloadedCode: string, filename: string, moduleUrl
|
||||||
process.env.BROWSERSLIST_IGNORE_OLD_DATA = 'true';
|
process.env.BROWSERSLIST_IGNORE_OLD_DATA = 'true';
|
||||||
|
|
||||||
const { babelTransform }: { babelTransform: BabelTransformFunction } = require('./babelBundle');
|
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)
|
if (code)
|
||||||
addToCache!(code, map);
|
addToCache!(code, map);
|
||||||
return code || '';
|
return code || '';
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue