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.
|
|
|
|
*/
|
|
|
|
|
2023-09-09 00:23:35 +03:00
|
|
|
import type { Suite } from 'playwright/types/testReporter';
|
|
|
|
import type { PlaywrightTestConfig as BasePlaywrightTestConfig, FullConfig } from 'playwright/test';
|
2023-04-18 02:19:21 +03:00
|
|
|
|
2023-08-25 02:19:57 +03:00
|
|
|
import type { InlineConfig, Plugin, ResolveFn, ResolvedConfig, UserConfig } from 'vite';
|
2023-09-09 00:23:35 +03:00
|
|
|
import type { TestRunnerPlugin } from '../../playwright/src/plugins';
|
2023-04-20 00:20:53 +03:00
|
|
|
import type { ComponentInfo } from './tsxTransform';
|
2023-04-18 02:19:21 +03:00
|
|
|
import type { AddressInfo } from 'net';
|
|
|
|
import type { PluginContext } from 'rollup';
|
2023-08-25 02:19:57 +03:00
|
|
|
import { debug } from 'playwright-core/lib/utilsBundle';
|
2023-04-18 02:19:21 +03:00
|
|
|
|
2022-05-06 22:02:07 +03:00
|
|
|
import fs from 'fs';
|
|
|
|
import path from 'path';
|
2023-09-09 00:23:35 +03:00
|
|
|
import { parse, traverse, types as t } from 'playwright/lib/transform/babelBundle';
|
|
|
|
import { stoppable } from 'playwright/lib/utilsBundle';
|
2022-06-03 03:37:43 +03:00
|
|
|
import { assert, calculateSha1 } from 'playwright-core/lib/utils';
|
2023-01-14 00:50:38 +03:00
|
|
|
import { getPlaywrightVersion } from 'playwright-core/lib/utils';
|
2023-09-09 00:23:35 +03:00
|
|
|
import { setExternalDependencies } from 'playwright/lib/transform/compilationCache';
|
2023-04-20 00:20:53 +03:00
|
|
|
import { collectComponentUsages, componentInfo } from './tsxTransform';
|
2023-08-25 02:19:57 +03:00
|
|
|
import { version as viteVersion, build, preview, mergeConfig } from 'vite';
|
|
|
|
|
|
|
|
const log = debug('pw:vite');
|
2022-04-22 03:30:17 +03:00
|
|
|
|
2022-08-08 18:54:56 +03:00
|
|
|
let stoppableServer: any;
|
2022-09-16 01:24:01 +03:00
|
|
|
const playwrightVersion = getPlaywrightVersion();
|
2022-04-22 03:30:17 +03:00
|
|
|
|
2022-10-31 22:58:07 +03:00
|
|
|
type CtConfig = BasePlaywrightTestConfig['use'] & {
|
2022-05-10 22:21:29 +03:00
|
|
|
ctPort?: number;
|
|
|
|
ctTemplateDir?: string;
|
|
|
|
ctCacheDir?: string;
|
2023-01-28 01:13:15 +03:00
|
|
|
ctViteConfig?: InlineConfig | (() => Promise<InlineConfig>);
|
2022-05-10 22:21:29 +03:00
|
|
|
};
|
|
|
|
|
2023-03-28 21:15:11 +03:00
|
|
|
const importReactRE = /(^|\n|;)import\s+(\*\s+as\s+)?React(,|\s+)/;
|
2022-07-06 23:44:06 +03:00
|
|
|
const compiledReactRE = /(const|var)\s+React\s*=/;
|
2022-05-10 22:21:29 +03:00
|
|
|
|
2022-05-06 22:02:07 +03:00
|
|
|
export function createPlugin(
|
2022-05-04 07:25:50 +03:00
|
|
|
registerSourceFile: string,
|
2023-04-28 06:26:02 +03:00
|
|
|
frameworkPluginFactory?: () => Promise<Plugin>): TestRunnerPlugin {
|
2022-05-06 22:02:07 +03:00
|
|
|
let configDir: string;
|
2023-01-24 04:44:23 +03:00
|
|
|
let config: FullConfig;
|
2022-04-25 20:40:58 +03:00
|
|
|
return {
|
2022-05-06 22:02:07 +03:00
|
|
|
name: 'playwright-vite-plugin',
|
2022-04-28 21:43:39 +03:00
|
|
|
|
2023-01-24 04:44:23 +03:00
|
|
|
setup: async (configObject: FullConfig, configDirectory: string) => {
|
|
|
|
config = configObject;
|
2022-05-06 22:02:07 +03:00
|
|
|
configDir = configDirectory;
|
2023-01-24 04:44:23 +03:00
|
|
|
},
|
|
|
|
|
|
|
|
begin: async (suite: Suite) => {
|
2023-08-25 02:19:57 +03:00
|
|
|
// We are going to have 3 config files:
|
|
|
|
// - the defaults that user config overrides (baseConfig)
|
|
|
|
// - the user config (userConfig)
|
|
|
|
// - frameworks overrides (frameworkOverrides);
|
|
|
|
|
2022-05-10 22:21:29 +03:00
|
|
|
const use = config.projects[0].use as CtConfig;
|
2023-10-18 23:53:58 +03:00
|
|
|
const baseURL = new URL(use.baseURL || 'http://localhost');
|
2023-08-25 02:19:57 +03:00
|
|
|
const relativeTemplateDir = use.ctTemplateDir || 'playwright';
|
|
|
|
|
|
|
|
// FIXME: use build plugin to determine html location to resolve this.
|
|
|
|
// TemplateDir must be relative, otherwise we can't move the final index.html into its target location post-build.
|
|
|
|
// This regressed in https://github.com/microsoft/playwright/pull/26526
|
|
|
|
const templateDir = path.join(configDir, relativeTemplateDir);
|
|
|
|
|
|
|
|
// Compose base config from the playwright config only.
|
2023-10-18 23:53:58 +03:00
|
|
|
const baseConfig: InlineConfig = {
|
2023-08-25 02:19:57 +03:00
|
|
|
root: configDir,
|
|
|
|
configFile: false,
|
|
|
|
define: {
|
|
|
|
__VUE_PROD_DEVTOOLS__: true,
|
|
|
|
},
|
|
|
|
css: {
|
|
|
|
devSourcemap: true,
|
|
|
|
},
|
|
|
|
build: {
|
|
|
|
outDir: use.ctCacheDir ? path.resolve(configDir, use.ctCacheDir) : path.resolve(templateDir, '.cache')
|
|
|
|
},
|
|
|
|
preview: {
|
2023-10-18 23:53:58 +03:00
|
|
|
https: baseURL.protocol.startsWith('https:'),
|
|
|
|
host: baseURL.hostname,
|
|
|
|
port: use.ctPort || Number(baseURL.port) || 3100
|
2023-08-25 02:19:57 +03:00
|
|
|
},
|
|
|
|
// Vite preview server will otherwise always return the index.html with 200.
|
|
|
|
appType: 'custom',
|
|
|
|
};
|
2022-05-06 22:02:07 +03:00
|
|
|
|
2023-08-25 02:19:57 +03:00
|
|
|
// Apply user config on top of the base config. This could have changed root and build.outDir.
|
|
|
|
const userConfig = typeof use.ctViteConfig === 'function' ? await use.ctViteConfig() : (use.ctViteConfig || {});
|
|
|
|
const baseAndUserConfig = mergeConfig(baseConfig, userConfig);
|
|
|
|
const buildInfoFile = path.join(baseAndUserConfig.build.outDir, 'metainfo.json');
|
2022-05-09 19:10:47 +03:00
|
|
|
|
2022-05-24 00:33:16 +03:00
|
|
|
let buildExists = false;
|
2022-05-09 19:10:47 +03:00
|
|
|
let buildInfo: BuildInfo;
|
2022-06-03 03:37:43 +03:00
|
|
|
|
|
|
|
const registerSource = await fs.promises.readFile(registerSourceFile, 'utf-8');
|
|
|
|
const registerSourceHash = calculateSha1(registerSource);
|
|
|
|
|
2022-05-09 19:10:47 +03:00
|
|
|
try {
|
|
|
|
buildInfo = JSON.parse(await fs.promises.readFile(buildInfoFile, 'utf-8')) as BuildInfo;
|
2022-09-16 01:24:01 +03:00
|
|
|
assert(buildInfo.version === playwrightVersion);
|
|
|
|
assert(buildInfo.viteVersion === viteVersion);
|
2022-06-03 03:37:43 +03:00
|
|
|
assert(buildInfo.registerSourceHash === registerSourceHash);
|
2022-05-24 00:33:16 +03:00
|
|
|
buildExists = true;
|
2022-05-09 19:10:47 +03:00
|
|
|
} catch (e) {
|
|
|
|
buildInfo = {
|
2022-09-16 01:24:01 +03:00
|
|
|
version: playwrightVersion,
|
|
|
|
viteVersion,
|
2022-06-03 03:37:43 +03:00
|
|
|
registerSourceHash,
|
2022-05-09 19:10:47 +03:00
|
|
|
components: [],
|
|
|
|
tests: {},
|
|
|
|
sources: {},
|
|
|
|
};
|
2022-05-06 22:02:07 +03:00
|
|
|
}
|
2023-08-25 02:19:57 +03:00
|
|
|
log('build exists:', buildExists);
|
2022-05-09 19:10:47 +03:00
|
|
|
|
|
|
|
const componentRegistry: ComponentRegistry = new Map();
|
|
|
|
// 1. Re-parse changed tests and collect required components.
|
|
|
|
const hasNewTests = await checkNewTests(suite, buildInfo, componentRegistry);
|
2023-08-25 02:19:57 +03:00
|
|
|
log('has new tests:', hasNewTests);
|
|
|
|
|
2022-05-09 19:10:47 +03:00
|
|
|
// 2. Check if the set of required components has changed.
|
|
|
|
const hasNewComponents = await checkNewComponents(buildInfo, componentRegistry);
|
2023-08-25 02:19:57 +03:00
|
|
|
log('has new components:', hasNewComponents);
|
|
|
|
|
2022-05-09 19:10:47 +03:00
|
|
|
// 3. Check component sources.
|
2022-05-24 00:37:56 +03:00
|
|
|
const sourcesDirty = !buildExists || hasNewComponents || await checkSources(buildInfo);
|
2023-08-25 02:19:57 +03:00
|
|
|
log('sourcesDirty:', sourcesDirty);
|
|
|
|
|
2023-02-10 05:39:20 +03:00
|
|
|
// 4. Update component info.
|
|
|
|
buildInfo.components = [...componentRegistry.values()];
|
2022-05-09 19:10:47 +03:00
|
|
|
|
2023-08-25 02:19:57 +03:00
|
|
|
const frameworkOverrides: UserConfig = { plugins: [] };
|
2022-05-25 05:43:28 +03:00
|
|
|
|
|
|
|
// React heuristic. If we see a component in a file with .js extension,
|
|
|
|
// consider it a potential JSX-in-JS scenario and enable JSX loader for all
|
|
|
|
// .js files.
|
|
|
|
if (hasJSComponents(buildInfo.components)) {
|
2023-08-25 02:19:57 +03:00
|
|
|
log('jsx-in-js detected');
|
|
|
|
frameworkOverrides.esbuild = {
|
2022-05-25 05:43:28 +03:00
|
|
|
loader: 'jsx',
|
|
|
|
include: /.*\.jsx?$/,
|
|
|
|
exclude: [],
|
|
|
|
};
|
2023-08-25 02:19:57 +03:00
|
|
|
frameworkOverrides.optimizeDeps = {
|
2022-05-25 05:43:28 +03:00
|
|
|
esbuildOptions: {
|
|
|
|
loader: { '.js': 'jsx' },
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
2023-08-25 02:19:57 +03:00
|
|
|
|
|
|
|
// We assume that any non-empty plugin list includes `vite-react` or similar.
|
|
|
|
if (frameworkPluginFactory && !baseAndUserConfig.plugins?.length)
|
|
|
|
frameworkOverrides.plugins = [await frameworkPluginFactory()];
|
2023-04-28 06:26:02 +03:00
|
|
|
|
2022-08-12 20:37:35 +03:00
|
|
|
// But only add out own plugin when we actually build / transform.
|
|
|
|
if (sourcesDirty)
|
2023-08-25 02:19:57 +03:00
|
|
|
frameworkOverrides.plugins!.push(vitePlugin(registerSource, templateDir, buildInfo, componentRegistry));
|
|
|
|
|
|
|
|
frameworkOverrides.build = {
|
2022-08-12 20:37:35 +03:00
|
|
|
target: 'esnext',
|
|
|
|
minify: false,
|
|
|
|
rollupOptions: {
|
|
|
|
treeshake: false,
|
|
|
|
input: {
|
|
|
|
index: path.join(templateDir, 'index.html')
|
2022-05-09 19:10:47 +03:00
|
|
|
},
|
2022-08-12 20:37:35 +03:00
|
|
|
},
|
|
|
|
sourcemap: true,
|
|
|
|
};
|
|
|
|
|
2023-08-25 02:19:57 +03:00
|
|
|
const finalConfig = mergeConfig(baseAndUserConfig, frameworkOverrides);
|
|
|
|
|
2022-10-31 22:58:07 +03:00
|
|
|
if (sourcesDirty) {
|
2023-08-25 02:19:57 +03:00
|
|
|
log('build');
|
|
|
|
await build(finalConfig);
|
|
|
|
await fs.promises.rename(`${finalConfig.build.outDir}/${relativeTemplateDir}/index.html`, `${finalConfig.build.outDir}/index.html`);
|
2022-10-31 22:58:07 +03:00
|
|
|
}
|
2022-08-12 20:37:35 +03:00
|
|
|
|
2023-08-25 02:19:57 +03:00
|
|
|
if (hasNewTests || hasNewComponents || sourcesDirty) {
|
|
|
|
log('write manifest');
|
2022-05-09 19:10:47 +03:00
|
|
|
await fs.promises.writeFile(buildInfoFile, JSON.stringify(buildInfo, undefined, 2));
|
2023-08-25 02:19:57 +03:00
|
|
|
}
|
2022-08-12 20:37:35 +03:00
|
|
|
|
2023-08-25 02:19:57 +03:00
|
|
|
for (const [filename, testInfo] of Object.entries(buildInfo.tests)) {
|
|
|
|
const deps = new Set<string>();
|
|
|
|
for (const componentName of testInfo.components) {
|
|
|
|
const component = componentRegistry.get(componentName);
|
|
|
|
component?.deps.forEach(d => deps.add(d));
|
|
|
|
}
|
|
|
|
setExternalDependencies(filename, [...deps]);
|
|
|
|
}
|
2023-02-10 19:33:25 +03:00
|
|
|
|
2023-08-25 02:19:57 +03:00
|
|
|
const previewServer = await preview(finalConfig);
|
2022-08-08 18:54:56 +03:00
|
|
|
stoppableServer = stoppable(previewServer.httpServer, 0);
|
2022-05-24 23:54:12 +03:00
|
|
|
const isAddressInfo = (x: any): x is AddressInfo => x?.address;
|
|
|
|
const address = previewServer.httpServer.address();
|
2022-12-29 04:04:23 +03:00
|
|
|
if (isAddressInfo(address)) {
|
2023-08-25 02:19:57 +03:00
|
|
|
const protocol = finalConfig.preview.https ? 'https:' : 'http:';
|
2023-09-27 02:32:51 +03:00
|
|
|
process.env.PLAYWRIGHT_TEST_BASE_URL = `${protocol}//${finalConfig.preview.host}:${address.port}`;
|
2022-12-29 04:04:23 +03:00
|
|
|
}
|
2022-04-25 20:40:58 +03:00
|
|
|
},
|
|
|
|
|
2023-02-07 02:52:14 +03:00
|
|
|
end: async () => {
|
2023-07-21 03:16:22 +03:00
|
|
|
if (stoppableServer)
|
|
|
|
await new Promise(f => stoppableServer.stop(f));
|
2022-04-25 20:40:58 +03:00
|
|
|
},
|
2022-04-22 03:30:17 +03:00
|
|
|
};
|
|
|
|
}
|
2022-05-06 00:26:56 +03:00
|
|
|
|
2022-05-09 19:10:47 +03:00
|
|
|
type BuildInfo = {
|
2022-09-16 01:24:01 +03:00
|
|
|
version: string,
|
|
|
|
viteVersion: string,
|
2022-06-03 03:37:43 +03:00
|
|
|
registerSourceHash: string,
|
2022-05-09 19:10:47 +03:00
|
|
|
sources: {
|
|
|
|
[key: string]: {
|
|
|
|
timestamp: number;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
components: ComponentInfo[];
|
|
|
|
tests: {
|
|
|
|
[key: string]: {
|
|
|
|
timestamp: number;
|
|
|
|
components: string[];
|
|
|
|
}
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
type ComponentRegistry = Map<string, ComponentInfo>;
|
|
|
|
|
|
|
|
async function checkSources(buildInfo: BuildInfo): Promise<boolean> {
|
|
|
|
for (const [source, sourceInfo] of Object.entries(buildInfo.sources)) {
|
|
|
|
try {
|
|
|
|
const timestamp = (await fs.promises.stat(source)).mtimeMs;
|
2023-08-25 02:19:57 +03:00
|
|
|
if (sourceInfo.timestamp !== timestamp) {
|
|
|
|
log('source has changed:', source);
|
2022-05-09 19:10:47 +03:00
|
|
|
return true;
|
2023-08-25 02:19:57 +03:00
|
|
|
}
|
2022-05-09 19:10:47 +03:00
|
|
|
} catch (e) {
|
2023-08-25 02:19:57 +03:00
|
|
|
log('check source failed:', e);
|
2022-05-09 19:10:47 +03:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
async function checkNewTests(suite: Suite, buildInfo: BuildInfo, componentRegistry: ComponentRegistry): Promise<boolean> {
|
|
|
|
const testFiles = new Set<string>();
|
|
|
|
for (const project of suite.suites) {
|
|
|
|
for (const file of project.suites)
|
|
|
|
testFiles.add(file.location!.file);
|
|
|
|
}
|
|
|
|
|
|
|
|
let hasNewTests = false;
|
|
|
|
for (const testFile of testFiles) {
|
|
|
|
const timestamp = (await fs.promises.stat(testFile)).mtimeMs;
|
|
|
|
if (buildInfo.tests[testFile]?.timestamp !== timestamp) {
|
|
|
|
const components = await parseTestFile(testFile);
|
2023-08-25 02:19:57 +03:00
|
|
|
log('changed test:', testFile);
|
2022-05-09 19:10:47 +03:00
|
|
|
for (const component of components)
|
|
|
|
componentRegistry.set(component.fullName, component);
|
2023-08-25 02:19:57 +03:00
|
|
|
buildInfo.tests[testFile] = { timestamp, components: components.map(c => c.fullName) };
|
2022-05-09 19:10:47 +03:00
|
|
|
hasNewTests = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return hasNewTests;
|
|
|
|
}
|
|
|
|
|
|
|
|
async function checkNewComponents(buildInfo: BuildInfo, componentRegistry: ComponentRegistry): Promise<boolean> {
|
|
|
|
const newComponents = [...componentRegistry.keys()];
|
2023-02-10 05:39:20 +03:00
|
|
|
const oldComponents = new Map(buildInfo.components.map(c => [c.fullName, c]));
|
2022-05-09 19:10:47 +03:00
|
|
|
|
|
|
|
let hasNewComponents = false;
|
|
|
|
for (const c of newComponents) {
|
|
|
|
if (!oldComponents.has(c)) {
|
|
|
|
hasNewComponents = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2023-02-10 05:39:20 +03:00
|
|
|
for (const c of oldComponents.values())
|
|
|
|
componentRegistry.set(c.fullName, c);
|
|
|
|
|
|
|
|
return hasNewComponents;
|
2022-05-09 19:10:47 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
async function parseTestFile(testFile: string): Promise<ComponentInfo[]> {
|
|
|
|
const text = await fs.promises.readFile(testFile, 'utf-8');
|
|
|
|
const ast = parse(text, { errorRecovery: true, plugins: ['typescript', 'jsx'], sourceType: 'module' });
|
|
|
|
const componentUsages = collectComponentUsages(ast);
|
2023-09-26 03:00:52 +03:00
|
|
|
const componentNames = componentUsages.names;
|
2022-05-09 19:10:47 +03:00
|
|
|
const result: ComponentInfo[] = [];
|
|
|
|
|
|
|
|
traverse(ast, {
|
|
|
|
enter: p => {
|
|
|
|
if (t.isImportDeclaration(p.node)) {
|
|
|
|
const importNode = p.node;
|
|
|
|
if (!t.isStringLiteral(importNode.source))
|
|
|
|
return;
|
2022-05-06 22:02:07 +03:00
|
|
|
|
2022-05-09 19:10:47 +03:00
|
|
|
for (const specifier of importNode.specifiers) {
|
2023-09-26 03:00:52 +03:00
|
|
|
const specifierName = specifier.local.name;
|
|
|
|
const componentName = componentNames.has(specifierName) ? specifierName : [...componentNames].find(c => c.startsWith(specifierName + '.'));
|
|
|
|
if (!componentName)
|
2022-05-09 19:10:47 +03:00
|
|
|
continue;
|
|
|
|
if (t.isImportNamespaceSpecifier(specifier))
|
|
|
|
continue;
|
2023-09-26 03:00:52 +03:00
|
|
|
result.push(componentInfo(specifier, importNode.source.value, testFile, componentName));
|
2022-05-09 19:10:47 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2023-08-21 19:30:56 +03:00
|
|
|
function vitePlugin(registerSource: string, templateDir: string, buildInfo: BuildInfo, componentRegistry: ComponentRegistry): Plugin {
|
2022-05-09 19:10:47 +03:00
|
|
|
buildInfo.sources = {};
|
2023-02-10 19:33:25 +03:00
|
|
|
let moduleResolver: ResolveFn;
|
2022-05-06 22:02:07 +03:00
|
|
|
return {
|
|
|
|
name: 'playwright:component-index',
|
|
|
|
|
2023-02-10 19:33:25 +03:00
|
|
|
configResolved(config: ResolvedConfig) {
|
|
|
|
moduleResolver = config.createResolver();
|
|
|
|
},
|
|
|
|
|
|
|
|
async transform(this: PluginContext, content, id) {
|
2022-05-09 19:10:47 +03:00
|
|
|
const queryIndex = id.indexOf('?');
|
|
|
|
const file = queryIndex !== -1 ? id.substring(0, queryIndex) : id;
|
|
|
|
if (!buildInfo.sources[file]) {
|
|
|
|
try {
|
|
|
|
const timestamp = (await fs.promises.stat(file)).mtimeMs;
|
|
|
|
buildInfo.sources[file] = { timestamp };
|
|
|
|
} catch {
|
|
|
|
// Silent if can't read the file.
|
|
|
|
}
|
2022-05-06 22:02:07 +03:00
|
|
|
}
|
|
|
|
|
2022-05-25 05:43:28 +03:00
|
|
|
// Vite React plugin will do this for .jsx files, but not .js files.
|
2022-07-06 23:44:06 +03:00
|
|
|
if (id.endsWith('.js') && content.includes('React.createElement') && !content.match(importReactRE) && !content.match(compiledReactRE)) {
|
2022-05-25 05:43:28 +03:00
|
|
|
const code = `import React from 'react';\n${content}`;
|
|
|
|
return { code, map: { mappings: '' } };
|
|
|
|
}
|
|
|
|
|
2023-08-21 19:30:56 +03:00
|
|
|
const indexTs = path.join(templateDir, 'index.ts');
|
|
|
|
const indexTsx = path.join(templateDir, 'index.tsx');
|
|
|
|
const indexJs = path.join(templateDir, 'index.js');
|
|
|
|
const indexJsx = path.join(templateDir, 'index.jsx');
|
2022-11-11 20:52:39 +03:00
|
|
|
const idResolved = path.resolve(id);
|
2023-03-18 04:08:28 +03:00
|
|
|
if (!idResolved.endsWith(indexTs) && !idResolved.endsWith(indexTsx) && !idResolved.endsWith(indexJs) && !idResolved.endsWith(indexJsx))
|
2022-05-06 22:02:07 +03:00
|
|
|
return;
|
|
|
|
|
|
|
|
const folder = path.dirname(id);
|
|
|
|
const lines = [content, ''];
|
|
|
|
lines.push(registerSource);
|
|
|
|
|
2022-05-09 19:10:47 +03:00
|
|
|
for (const [alias, value] of componentRegistry) {
|
2022-05-06 22:02:07 +03:00
|
|
|
const importPath = value.isModuleOrAlias ? value.importPath : './' + path.relative(folder, value.importPath).replace(/\\/g, '/');
|
|
|
|
if (value.importedName)
|
2023-09-26 03:00:52 +03:00
|
|
|
lines.push(`const ${alias} = () => import('${importPath}').then((mod) => mod.${value.importedName + (value.importedNameProperty || '')});`);
|
2022-05-06 22:02:07 +03:00
|
|
|
else
|
2023-09-26 03:00:52 +03:00
|
|
|
lines.push(`const ${alias} = () => import('${importPath}').then((mod) => mod.default${value.importedNameProperty || ''});`);
|
2022-05-06 22:02:07 +03:00
|
|
|
}
|
|
|
|
|
2023-03-18 04:08:28 +03:00
|
|
|
lines.push(`pwRegister({ ${[...componentRegistry.keys()].join(',\n ')} });`);
|
2022-05-07 04:29:49 +03:00
|
|
|
return {
|
|
|
|
code: lines.join('\n'),
|
|
|
|
map: { mappings: '' }
|
|
|
|
};
|
2022-05-06 22:02:07 +03:00
|
|
|
},
|
2023-02-10 19:33:25 +03:00
|
|
|
|
|
|
|
async writeBundle(this: PluginContext) {
|
|
|
|
for (const component of componentRegistry.values()) {
|
|
|
|
const id = (await moduleResolver(component.importPath));
|
|
|
|
if (!id)
|
|
|
|
continue;
|
|
|
|
const deps = new Set<string>();
|
|
|
|
collectViteModuleDependencies(this, id, deps);
|
2023-08-25 02:19:57 +03:00
|
|
|
component.deps = [...deps];
|
2023-02-10 19:33:25 +03:00
|
|
|
}
|
|
|
|
},
|
2022-05-06 22:02:07 +03:00
|
|
|
};
|
|
|
|
}
|
2022-05-25 05:43:28 +03:00
|
|
|
|
2023-02-10 19:33:25 +03:00
|
|
|
function collectViteModuleDependencies(context: PluginContext, id: string, deps: Set<string>) {
|
|
|
|
if (!path.isAbsolute(id))
|
|
|
|
return;
|
2023-02-15 01:55:49 +03:00
|
|
|
const normalizedId = path.normalize(id);
|
|
|
|
if (deps.has(normalizedId))
|
2023-02-10 19:33:25 +03:00
|
|
|
return;
|
2023-02-15 01:55:49 +03:00
|
|
|
deps.add(normalizedId);
|
2023-02-10 19:33:25 +03:00
|
|
|
const module = context.getModuleInfo(id);
|
|
|
|
for (const importedId of module?.importedIds || [])
|
|
|
|
collectViteModuleDependencies(context, importedId, deps);
|
|
|
|
for (const importedId of module?.dynamicallyImportedIds || [])
|
|
|
|
collectViteModuleDependencies(context, importedId, deps);
|
|
|
|
}
|
|
|
|
|
2022-05-25 05:43:28 +03:00
|
|
|
function hasJSComponents(components: ComponentInfo[]): boolean {
|
|
|
|
for (const component of components) {
|
|
|
|
const extname = path.extname(component.importPath);
|
|
|
|
if (extname === '.js' || !extname && fs.existsSync(component.importPath + '.js'))
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|