2022-04-22 03:30:17 +03:00
|
|
|
/**
|
|
|
|
* Copyright (c) Microsoft Corporation.
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
import fs from 'fs';
|
2022-05-04 00:25:56 +03:00
|
|
|
import type { Suite } from '../../types/testReporter';
|
2022-04-22 03:30:17 +03:00
|
|
|
import path from 'path';
|
2022-05-04 07:25:50 +03:00
|
|
|
import type { InlineConfig, Plugin, PreviewServer } from 'vite';
|
2022-05-04 00:25:56 +03:00
|
|
|
import type { TestRunnerPlugin } from '.';
|
2022-04-25 20:40:58 +03:00
|
|
|
import { parse, traverse, types as t } from '../babelBundle';
|
|
|
|
import type { ComponentInfo } from '../tsxTransform';
|
|
|
|
import { collectComponentUsages, componentInfo } from '../tsxTransform';
|
2022-05-04 00:25:56 +03:00
|
|
|
import type { FullConfig } from '../types';
|
2022-04-22 03:30:17 +03:00
|
|
|
|
2022-05-04 07:25:50 +03:00
|
|
|
let previewServer: PreviewServer;
|
2022-04-22 03:30:17 +03:00
|
|
|
|
2022-04-25 20:40:58 +03:00
|
|
|
export function createPlugin(
|
2022-05-04 07:25:50 +03:00
|
|
|
registerSourceFile: string,
|
2022-05-04 00:25:56 +03:00
|
|
|
frameworkPluginFactory: () => Plugin): TestRunnerPlugin {
|
2022-04-25 20:40:58 +03:00
|
|
|
let configDir: string;
|
|
|
|
return {
|
2022-04-28 21:43:39 +03:00
|
|
|
name: 'playwright-vite-plugin',
|
|
|
|
|
2022-05-04 00:25:56 +03:00
|
|
|
setup: async (config: FullConfig, configDirectory: string, suite: Suite) => {
|
2022-05-04 01:48:46 +03:00
|
|
|
const use = config.projects[0].use as any;
|
|
|
|
const viteConfig: InlineConfig = use.viteConfig || {};
|
|
|
|
const port = use.vitePort || 3100;
|
2022-05-04 00:25:56 +03:00
|
|
|
|
2022-04-25 20:40:58 +03:00
|
|
|
configDir = configDirectory;
|
|
|
|
|
2022-05-04 00:25:56 +03:00
|
|
|
process.env.PLAYWRIGHT_TEST_BASE_URL = `http://localhost:${port}/playwright/index.html`;
|
|
|
|
|
2022-04-25 20:40:58 +03:00
|
|
|
viteConfig.root = viteConfig.root || configDir;
|
2022-04-27 02:15:08 +03:00
|
|
|
viteConfig.plugins = viteConfig.plugins || [
|
|
|
|
frameworkPluginFactory()
|
|
|
|
];
|
2022-04-28 18:16:17 +03:00
|
|
|
const files = new Set<string>();
|
|
|
|
for (const project of suite.suites) {
|
|
|
|
for (const file of project.suites)
|
|
|
|
files.add(file.location!.file);
|
|
|
|
}
|
2022-05-04 07:25:50 +03:00
|
|
|
const registerSource = await fs.promises.readFile(registerSourceFile, 'utf-8');
|
|
|
|
viteConfig.plugins.push(vitePlugin(registerSource, [...files]));
|
2022-04-25 20:40:58 +03:00
|
|
|
viteConfig.configFile = viteConfig.configFile || false;
|
2022-05-04 07:25:50 +03:00
|
|
|
viteConfig.define = viteConfig.define || {};
|
|
|
|
viteConfig.define.__VUE_PROD_DEVTOOLS__ = true;
|
|
|
|
viteConfig.css = viteConfig.css || {};
|
|
|
|
viteConfig.css.devSourcemap = true;
|
|
|
|
viteConfig.preview = { port };
|
|
|
|
viteConfig.build = {
|
|
|
|
target: 'esnext',
|
|
|
|
minify: false,
|
|
|
|
rollupOptions: {
|
|
|
|
treeshake: false,
|
|
|
|
input: {
|
|
|
|
index: path.join(viteConfig.root, 'playwright', 'index.html')
|
|
|
|
},
|
|
|
|
},
|
|
|
|
sourcemap: true,
|
|
|
|
outDir: viteConfig?.build?.outDir || path.join(viteConfig.root, './dist-pw/')
|
|
|
|
};
|
|
|
|
const { build, preview } = require('vite');
|
|
|
|
await build(viteConfig);
|
|
|
|
previewServer = await preview(viteConfig);
|
2022-04-25 20:40:58 +03:00
|
|
|
},
|
|
|
|
|
|
|
|
teardown: async () => {
|
2022-05-04 07:25:50 +03:00
|
|
|
await new Promise<void>((f, r) => previewServer.httpServer.close(err => {
|
|
|
|
if (err)
|
|
|
|
r(err);
|
|
|
|
else
|
|
|
|
f();
|
|
|
|
}));
|
2022-04-25 20:40:58 +03:00
|
|
|
},
|
2022-04-22 03:30:17 +03:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2022-04-25 20:40:58 +03:00
|
|
|
const imports: Map<string, ComponentInfo> = new Map();
|
|
|
|
|
2022-05-04 07:25:50 +03:00
|
|
|
function vitePlugin(registerSource: string, files: string[]): Plugin {
|
2022-04-22 03:30:17 +03:00
|
|
|
return {
|
2022-04-25 20:40:58 +03:00
|
|
|
name: 'playwright:component-index',
|
2022-04-22 03:30:17 +03:00
|
|
|
|
|
|
|
configResolved: async config => {
|
|
|
|
|
|
|
|
for (const file of files) {
|
|
|
|
const text = await fs.promises.readFile(file, 'utf-8');
|
|
|
|
const ast = parse(text, { errorRecovery: true, plugins: ['typescript', 'jsx'], sourceType: 'module' });
|
|
|
|
const components = collectComponentUsages(ast);
|
|
|
|
|
|
|
|
traverse(ast, {
|
|
|
|
enter: p => {
|
|
|
|
if (t.isImportDeclaration(p.node)) {
|
|
|
|
const importNode = p.node;
|
|
|
|
if (!t.isStringLiteral(importNode.source))
|
|
|
|
return;
|
|
|
|
|
|
|
|
for (const specifier of importNode.specifiers) {
|
|
|
|
if (!components.names.has(specifier.local.name))
|
|
|
|
continue;
|
|
|
|
if (t.isImportNamespaceSpecifier(specifier))
|
|
|
|
continue;
|
|
|
|
const info = componentInfo(specifier, importNode.source.value, file);
|
|
|
|
imports.set(info.fullName, info);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2022-04-22 07:07:43 +03:00
|
|
|
transform: async (content, id) => {
|
|
|
|
if (!id.endsWith('playwright/index.ts') && !id.endsWith('playwright/index.tsx') && !id.endsWith('playwright/index.js'))
|
2022-04-22 03:30:17 +03:00
|
|
|
return;
|
|
|
|
|
|
|
|
const folder = path.dirname(id);
|
2022-04-22 07:07:43 +03:00
|
|
|
const lines = [content, ''];
|
2022-05-04 07:25:50 +03:00
|
|
|
lines.push(registerSource);
|
2022-04-22 03:30:17 +03:00
|
|
|
|
|
|
|
for (const [alias, value] of imports) {
|
|
|
|
const importPath = value.isModuleOrAlias ? value.importPath : './' + path.relative(folder, value.importPath).replace(/\\/g, '/');
|
|
|
|
if (value.importedName)
|
|
|
|
lines.push(`import { ${value.importedName} as ${alias} } from '${importPath}';`);
|
|
|
|
else
|
|
|
|
lines.push(`import ${alias} from '${importPath}';`);
|
|
|
|
}
|
|
|
|
|
|
|
|
lines.push(`register({ ${[...imports.keys()].join(',\n ')} });`);
|
|
|
|
return lines.join('\n');
|
|
|
|
},
|
|
|
|
};
|
|
|
|
}
|