chore: only transpile tsx for components (#22504)
This commit is contained in:
parent
bf0fab4927
commit
3c495c5590
|
|
@ -15,9 +15,9 @@
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import path from 'path';
|
import path from 'path';
|
||||||
import type { T, BabelAPI } from './babelBundle';
|
import type { T, BabelAPI } from '../../playwright-test/src/common/babelBundle';
|
||||||
import { types, declare, traverse } from './babelBundle';
|
import { types, declare, traverse } from '@playwright/test/lib/common/babelBundle';
|
||||||
import { js2ts } from './transform';
|
import { js2ts } from '@playwright/test/lib/util';
|
||||||
const t: typeof T = types;
|
const t: typeof T = types;
|
||||||
|
|
||||||
const fullNames = new Map<string, string | undefined>();
|
const fullNames = new Map<string, string | undefined>();
|
||||||
|
|
@ -19,7 +19,7 @@ import type { PlaywrightTestConfig as BasePlaywrightTestConfig, FullConfig } fro
|
||||||
|
|
||||||
import type { InlineConfig, Plugin, ResolveFn, ResolvedConfig } from 'vite';
|
import type { InlineConfig, Plugin, ResolveFn, ResolvedConfig } from 'vite';
|
||||||
import type { TestRunnerPlugin } from '../../playwright-test/src/plugins';
|
import type { TestRunnerPlugin } from '../../playwright-test/src/plugins';
|
||||||
import type { ComponentInfo } from '../../playwright-test/src/common/tsxTransform';
|
import type { ComponentInfo } from './tsxTransform';
|
||||||
import type { AddressInfo } from 'net';
|
import type { AddressInfo } from 'net';
|
||||||
import type { PluginContext } from 'rollup';
|
import type { PluginContext } from 'rollup';
|
||||||
|
|
||||||
|
|
@ -27,10 +27,10 @@ import fs from 'fs';
|
||||||
import path from 'path';
|
import path from 'path';
|
||||||
import { parse, traverse, types as t } from '@playwright/test/lib/common/babelBundle';
|
import { parse, traverse, types as t } from '@playwright/test/lib/common/babelBundle';
|
||||||
import { stoppable } from '@playwright/test/lib/utilsBundle';
|
import { stoppable } from '@playwright/test/lib/utilsBundle';
|
||||||
import { collectComponentUsages, componentInfo } from '@playwright/test/lib/common/tsxTransform';
|
|
||||||
import { assert, calculateSha1 } from 'playwright-core/lib/utils';
|
import { assert, calculateSha1 } from 'playwright-core/lib/utils';
|
||||||
import { getPlaywrightVersion } from 'playwright-core/lib/utils';
|
import { getPlaywrightVersion } from 'playwright-core/lib/utils';
|
||||||
import { setExternalDependencies } from '@playwright/test/lib/common/compilationCache';
|
import { setExternalDependencies } from '@playwright/test/lib/common/compilationCache';
|
||||||
|
import { collectComponentUsages, componentInfo } from './tsxTransform';
|
||||||
|
|
||||||
let stoppableServer: any;
|
let stoppableServer: any;
|
||||||
const playwrightVersion = getPlaywrightVersion();
|
const playwrightVersion = getPlaywrightVersion();
|
||||||
|
|
@ -58,6 +58,10 @@ export function createPlugin(
|
||||||
configDir = configDirectory;
|
configDir = configDirectory;
|
||||||
},
|
},
|
||||||
|
|
||||||
|
babelPlugins: async () => [
|
||||||
|
[require.resolve('./tsxTransform')]
|
||||||
|
],
|
||||||
|
|
||||||
begin: async (suite: Suite) => {
|
begin: async (suite: Suite) => {
|
||||||
const use = config.projects[0].use as CtConfig;
|
const use = config.projects[0].use as CtConfig;
|
||||||
const port = use.ctPort || 3100;
|
const port = use.ctPort || 3100;
|
||||||
|
|
|
||||||
|
|
@ -26,7 +26,14 @@ export { parse } from '@babel/parser';
|
||||||
import traverseFunction from '@babel/traverse';
|
import traverseFunction from '@babel/traverse';
|
||||||
export const traverse = traverseFunction;
|
export const traverse = traverseFunction;
|
||||||
|
|
||||||
export function babelTransform(filename: string, isTypeScript: boolean, isModule: boolean, scriptPreprocessor: string | undefined, additionalPlugin: babel.PluginObj): BabelFileResult {
|
|
||||||
|
let additionalPlugins: [string, any?][] = [];
|
||||||
|
|
||||||
|
export function setBabelPlugins(plugins: [string, any?][]) {
|
||||||
|
additionalPlugins = plugins;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function babelTransform(filename: string, isTypeScript: boolean, isModule: boolean, scriptPreprocessor: string | undefined): BabelFileResult {
|
||||||
const plugins = [];
|
const plugins = [];
|
||||||
|
|
||||||
if (isTypeScript) {
|
if (isTypeScript) {
|
||||||
|
|
@ -72,7 +79,7 @@ export function babelTransform(filename: string, isTypeScript: boolean, isModule
|
||||||
plugins.push([require('@babel/plugin-syntax-import-assertions')]);
|
plugins.push([require('@babel/plugin-syntax-import-assertions')]);
|
||||||
}
|
}
|
||||||
|
|
||||||
plugins.unshift(additionalPlugin);
|
plugins.unshift(...additionalPlugins.map(([name, options]) => [require(name), options]));
|
||||||
|
|
||||||
if (scriptPreprocessor)
|
if (scriptPreprocessor)
|
||||||
plugins.push([scriptPreprocessor]);
|
plugins.push([scriptPreprocessor]);
|
||||||
|
|
|
||||||
|
|
@ -20,11 +20,11 @@
|
||||||
"./lib/cli": "./lib/cli.js",
|
"./lib/cli": "./lib/cli.js",
|
||||||
"./lib/common/babelBundle": "./lib/common/babelBundle.js",
|
"./lib/common/babelBundle": "./lib/common/babelBundle.js",
|
||||||
"./lib/common/compilationCache": "./lib/common/compilationCache.js",
|
"./lib/common/compilationCache": "./lib/common/compilationCache.js",
|
||||||
"./lib/common/tsxTransform": "./lib/common/tsxTransform.js",
|
|
||||||
"./lib/internalsForTest": "./lib/internalsForTest.js",
|
"./lib/internalsForTest": "./lib/internalsForTest.js",
|
||||||
"./lib/experimentalLoader": "./lib/experimentalLoader.js",
|
"./lib/experimentalLoader": "./lib/experimentalLoader.js",
|
||||||
"./lib/mount": "./lib/mount.js",
|
"./lib/mount": "./lib/mount.js",
|
||||||
"./lib/plugins": "./lib/plugins/index.js",
|
"./lib/plugins": "./lib/plugins/index.js",
|
||||||
|
"./lib/util": "./lib/util.js",
|
||||||
"./lib/utilsBundle": "./lib/utilsBundle.js",
|
"./lib/utilsBundle": "./lib/utilsBundle.js",
|
||||||
"./reporter": "./reporter.js"
|
"./reporter": "./reporter.js"
|
||||||
},
|
},
|
||||||
|
|
|
||||||
|
|
@ -20,7 +20,8 @@ export const declare: typeof import('../../bundles/babel/node_modules/@types/bab
|
||||||
export const types: typeof import('../../bundles/babel/node_modules/@types/babel__core').types = require('./babelBundleImpl').types;
|
export const types: typeof import('../../bundles/babel/node_modules/@types/babel__core').types = require('./babelBundleImpl').types;
|
||||||
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 BabelTransformFunction = (filename: string, isTypeScript: boolean, isModule: boolean, scriptPreprocessor: string | undefined, additionalPlugin: [string]) => BabelFileResult;
|
export type BabelTransformFunction = (filename: string, isTypeScript: boolean, isModule: boolean, scriptPreprocessor: string | undefined) => BabelFileResult;
|
||||||
|
export const setBabelPlugins: (plugins: [string, any?][]) => void = require('./babelBundleImpl').setBabelPlugins;
|
||||||
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';
|
||||||
|
|
|
||||||
|
|
@ -23,6 +23,7 @@ import type { Config, Project } from '../../types/test';
|
||||||
import { errorWithFile } from '../util';
|
import { errorWithFile } from '../util';
|
||||||
import { setCurrentConfig } from './globals';
|
import { setCurrentConfig } from './globals';
|
||||||
import { FullConfigInternal } from './config';
|
import { FullConfigInternal } from './config';
|
||||||
|
import { setBabelPlugins } from './babelBundle';
|
||||||
|
|
||||||
const kDefineConfigWasUsed = Symbol('defineConfigWasUsed');
|
const kDefineConfigWasUsed = Symbol('defineConfigWasUsed');
|
||||||
export const defineConfig = (config: any) => {
|
export const defineConfig = (config: any) => {
|
||||||
|
|
@ -40,6 +41,8 @@ export class ConfigLoader {
|
||||||
|
|
||||||
static async deserialize(data: SerializedConfig): Promise<FullConfigInternal> {
|
static async deserialize(data: SerializedConfig): Promise<FullConfigInternal> {
|
||||||
const loader = new ConfigLoader(data.configCLIOverrides);
|
const loader = new ConfigLoader(data.configCLIOverrides);
|
||||||
|
setBabelPlugins(data.babelTransformPlugins);
|
||||||
|
|
||||||
if (data.configFile)
|
if (data.configFile)
|
||||||
return await loader.loadConfigFile(data.configFile);
|
return await loader.loadConfigFile(data.configFile);
|
||||||
return await loader.loadEmptyConfig(data.configDir);
|
return await loader.loadEmptyConfig(data.configDir);
|
||||||
|
|
|
||||||
|
|
@ -42,6 +42,7 @@ export type SerializedConfig = {
|
||||||
configDir: string;
|
configDir: string;
|
||||||
configCLIOverrides: ConfigCLIOverrides;
|
configCLIOverrides: ConfigCLIOverrides;
|
||||||
compilationCache: any;
|
compilationCache: any;
|
||||||
|
babelTransformPlugins: [string, any?][];
|
||||||
};
|
};
|
||||||
|
|
||||||
export type TtyParams = {
|
export type TtyParams = {
|
||||||
|
|
@ -126,11 +127,15 @@ export type TeardownErrorsPayload = {
|
||||||
export type EnvProducedPayload = [string, string | null][];
|
export type EnvProducedPayload = [string, string | null][];
|
||||||
|
|
||||||
export function serializeConfig(config: FullConfigInternal): SerializedConfig {
|
export function serializeConfig(config: FullConfigInternal): SerializedConfig {
|
||||||
|
const babelTransformPlugins: [string, any?][] = [];
|
||||||
|
for (const plugin of config.plugins)
|
||||||
|
babelTransformPlugins.push(...plugin.babelPlugins || []);
|
||||||
const result: SerializedConfig = {
|
const result: SerializedConfig = {
|
||||||
configFile: config.config.configFile,
|
configFile: config.config.configFile,
|
||||||
configDir: config.configDir,
|
configDir: config.configDir,
|
||||||
configCLIOverrides: config.configCLIOverrides,
|
configCLIOverrides: config.configCLIOverrides,
|
||||||
compilationCache: serializeCompilationCache(),
|
compilationCache: serializeCompilationCache(),
|
||||||
|
babelTransformPlugins,
|
||||||
};
|
};
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -23,7 +23,7 @@ import type { TsConfigLoaderResult } from '../third_party/tsconfig-loader';
|
||||||
import { tsConfigLoader } from '../third_party/tsconfig-loader';
|
import { tsConfigLoader } from '../third_party/tsconfig-loader';
|
||||||
import Module from 'module';
|
import Module from 'module';
|
||||||
import type { BabelTransformFunction } from './babelBundle';
|
import type { BabelTransformFunction } from './babelBundle';
|
||||||
import { fileIsModule } from '../util';
|
import { fileIsModule, js2ts } from '../util';
|
||||||
import { getFromCompilationCache, currentFileDepsCollector, belongsToNodeModules } from './compilationCache';
|
import { getFromCompilationCache, currentFileDepsCollector, belongsToNodeModules } from './compilationCache';
|
||||||
|
|
||||||
type ParsedTsConfigData = {
|
type ParsedTsConfigData = {
|
||||||
|
|
@ -132,18 +132,6 @@ export function resolveHook(filename: string, specifier: string): string | undef
|
||||||
return js2ts(path.resolve(path.dirname(filename), specifier));
|
return js2ts(path.resolve(path.dirname(filename), specifier));
|
||||||
}
|
}
|
||||||
|
|
||||||
export function js2ts(resolved: string): string | undefined {
|
|
||||||
const match = resolved.match(/(.*)(\.js|\.jsx|\.mjs)$/);
|
|
||||||
if (!match || fs.existsSync(resolved))
|
|
||||||
return;
|
|
||||||
const tsResolved = match[1] + match[2].replace('js', 'ts');
|
|
||||||
if (fs.existsSync(tsResolved))
|
|
||||||
return tsResolved;
|
|
||||||
const tsxResolved = match[1] + match[2].replace('js', 'tsx');
|
|
||||||
if (fs.existsSync(tsxResolved))
|
|
||||||
return tsxResolved;
|
|
||||||
}
|
|
||||||
|
|
||||||
export function transformHook(code: string, filename: string, moduleUrl?: string): string {
|
export function transformHook(code: string, filename: string, moduleUrl?: string): string {
|
||||||
const { cachedCode, addToCache } = getFromCompilationCache(filename, code, moduleUrl);
|
const { cachedCode, addToCache } = getFromCompilationCache(filename, code, moduleUrl);
|
||||||
if (cachedCode)
|
if (cachedCode)
|
||||||
|
|
@ -161,7 +149,7 @@ export function transformHook(code: string, filename: string, moduleUrl?: string
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const { babelTransform }: { babelTransform: BabelTransformFunction } = require('./babelBundle');
|
const { babelTransform }: { babelTransform: BabelTransformFunction } = require('./babelBundle');
|
||||||
const { code, map } = babelTransform(filename, isTypeScript, !!moduleUrl, hasPreprocessor ? scriptPreprocessor : undefined, [require.resolve('./tsxTransform')]);
|
const { code, map } = babelTransform(filename, isTypeScript, !!moduleUrl, hasPreprocessor ? scriptPreprocessor : undefined);
|
||||||
if (code)
|
if (code)
|
||||||
addToCache!(code, map);
|
addToCache!(code, map);
|
||||||
return code || '';
|
return code || '';
|
||||||
|
|
|
||||||
|
|
@ -20,6 +20,7 @@ import type { Multiplexer } from '../reporters/multiplexer';
|
||||||
export interface TestRunnerPlugin {
|
export interface TestRunnerPlugin {
|
||||||
name: string;
|
name: string;
|
||||||
setup?(config: FullConfig, configDir: string, reporter: Multiplexer): Promise<void>;
|
setup?(config: FullConfig, configDir: string, reporter: Multiplexer): Promise<void>;
|
||||||
|
babelPlugins?(): Promise<[string, any?][]>;
|
||||||
begin?(suite: Suite): Promise<void>;
|
begin?(suite: Suite): Promise<void>;
|
||||||
end?(): Promise<void>;
|
end?(): Promise<void>;
|
||||||
teardown?(): Promise<void>;
|
teardown?(): Promise<void>;
|
||||||
|
|
@ -28,6 +29,7 @@ export interface TestRunnerPlugin {
|
||||||
export type TestRunnerPluginRegistration = {
|
export type TestRunnerPluginRegistration = {
|
||||||
factory: TestRunnerPlugin | (() => TestRunnerPlugin | Promise<TestRunnerPlugin>);
|
factory: TestRunnerPlugin | (() => TestRunnerPlugin | Promise<TestRunnerPlugin>);
|
||||||
instance?: TestRunnerPlugin;
|
instance?: TestRunnerPlugin;
|
||||||
|
babelPlugins?: [string, any?][];
|
||||||
};
|
};
|
||||||
|
|
||||||
export { webServer } from './webServerPlugin';
|
export { webServer } from './webServerPlugin';
|
||||||
|
|
|
||||||
|
|
@ -22,6 +22,7 @@ import { loadTestFile } from '../common/testLoader';
|
||||||
import type { FullConfigInternal } from '../common/config';
|
import type { FullConfigInternal } from '../common/config';
|
||||||
import { PoolBuilder } from '../common/poolBuilder';
|
import { PoolBuilder } from '../common/poolBuilder';
|
||||||
import { addToCompilationCache } from '../common/compilationCache';
|
import { addToCompilationCache } from '../common/compilationCache';
|
||||||
|
import { setBabelPlugins } from '../common/babelBundle';
|
||||||
|
|
||||||
export class InProcessLoaderHost {
|
export class InProcessLoaderHost {
|
||||||
private _config: FullConfigInternal;
|
private _config: FullConfigInternal;
|
||||||
|
|
@ -30,6 +31,10 @@ export class InProcessLoaderHost {
|
||||||
constructor(config: FullConfigInternal) {
|
constructor(config: FullConfigInternal) {
|
||||||
this._config = config;
|
this._config = config;
|
||||||
this._poolBuilder = PoolBuilder.createForLoader();
|
this._poolBuilder = PoolBuilder.createForLoader();
|
||||||
|
const babelTransformPlugins: [string, any?][] = [];
|
||||||
|
for (const plugin of config.plugins)
|
||||||
|
babelTransformPlugins.push(...plugin.babelPlugins || []);
|
||||||
|
setBabelPlugins(babelTransformPlugins);
|
||||||
}
|
}
|
||||||
|
|
||||||
async loadTestFile(file: string, testErrors: TestError[]): Promise<Suite> {
|
async loadTestFile(file: string, testErrors: TestError[]): Promise<Suite> {
|
||||||
|
|
|
||||||
|
|
@ -118,6 +118,7 @@ function createPluginSetupTask(plugin: TestRunnerPluginRegistration): Task<TestR
|
||||||
else
|
else
|
||||||
plugin.instance = plugin.factory;
|
plugin.instance = plugin.factory;
|
||||||
await plugin.instance?.setup?.(config.config, config.configDir, reporter);
|
await plugin.instance?.setup?.(config.config, config.configDir, reporter);
|
||||||
|
plugin.babelPlugins = await plugin.instance?.babelPlugins?.() || [];
|
||||||
return () => plugin.instance?.teardown?.();
|
return () => plugin.instance?.teardown?.();
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -306,3 +306,15 @@ export function envWithoutExperimentalLoaderOptions(): NodeJS.ProcessEnv {
|
||||||
result.NODE_OPTIONS = result.NODE_OPTIONS.replace(substring, '').trim() || undefined;
|
result.NODE_OPTIONS = result.NODE_OPTIONS.replace(substring, '').trim() || undefined;
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function js2ts(resolved: string): string | undefined {
|
||||||
|
const match = resolved.match(/(.*)(\.js|\.jsx|\.mjs)$/);
|
||||||
|
if (!match || fs.existsSync(resolved))
|
||||||
|
return;
|
||||||
|
const tsResolved = match[1] + match[2].replace('js', 'ts');
|
||||||
|
if (fs.existsSync(tsResolved))
|
||||||
|
return tsResolved;
|
||||||
|
const tsxResolved = match[1] + match[2].replace('js', 'tsx');
|
||||||
|
if (fs.existsSync(tsxResolved))
|
||||||
|
return tsxResolved;
|
||||||
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue