2022-04-21 16:30:17 -08: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';
|
|
|
|
import path from 'path';
|
|
|
|
import { glob } from 'playwright-core/lib/utilsBundle';
|
|
|
|
import { parse, types as t, traverse } from './babelBundle';
|
|
|
|
import type { Plugin } from 'vite';
|
|
|
|
import { componentInfo, collectComponentUsages } from './tsxTransform';
|
|
|
|
import type { ComponentInfo } from './tsxTransform';
|
|
|
|
|
|
|
|
const imports: Map<string, ComponentInfo> = new Map();
|
|
|
|
|
|
|
|
export function createVitePlugin(registerFunction: string) {
|
2022-04-21 20:07:43 -08:00
|
|
|
return (options?: { include: string }) => {
|
2022-04-21 16:30:17 -08:00
|
|
|
return vitePlugin({ ...(options || {}), registerFunction });
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2022-04-21 20:07:43 -08:00
|
|
|
function vitePlugin(options: { include?: string, registerFunction: string }): Plugin {
|
2022-04-21 16:30:17 -08:00
|
|
|
return {
|
|
|
|
name: 'playwright-gallery',
|
|
|
|
|
|
|
|
configResolved: async config => {
|
|
|
|
const files = await new Promise<string[]>((f, r) => {
|
|
|
|
glob(options.include || config.root + '/**/*.{test,spec}.[tj]s{x,}', {}, function(err, files) {
|
|
|
|
if (err)
|
|
|
|
r(err);
|
|
|
|
else
|
|
|
|
f(files);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
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-21 20:07:43 -08:00
|
|
|
transform: async (content, id) => {
|
|
|
|
if (!id.endsWith('playwright/index.ts') && !id.endsWith('playwright/index.tsx') && !id.endsWith('playwright/index.js'))
|
2022-04-21 16:30:17 -08:00
|
|
|
return;
|
|
|
|
|
|
|
|
const folder = path.dirname(id);
|
2022-04-21 20:07:43 -08:00
|
|
|
const lines = [content, ''];
|
2022-04-21 16:30:17 -08:00
|
|
|
lines.push(`import register from '${options.registerFunction}';`);
|
|
|
|
|
|
|
|
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');
|
|
|
|
},
|
|
|
|
};
|
|
|
|
}
|