2022-03-01 10:40:46 +08:00
|
|
|
import path from 'path';
|
2022-09-07 14:49:54 +08:00
|
|
|
import { fileURLToPath } from 'url';
|
2022-03-01 10:40:46 +08:00
|
|
|
import getPort from 'get-port';
|
2022-03-31 15:12:33 +08:00
|
|
|
import createService from '../../packages/ice/src/createService';
|
2022-09-07 14:49:54 +08:00
|
|
|
import type { Page } from './browser';
|
|
|
|
import Browser from './browser';
|
2022-03-01 10:40:46 +08:00
|
|
|
|
|
|
|
const __dirname = path.dirname(fileURLToPath(import.meta.url));
|
2022-01-27 14:32:38 +08:00
|
|
|
|
2022-11-15 10:37:30 +08:00
|
|
|
interface SetupBrowser {
|
2022-01-27 14:32:38 +08:00
|
|
|
(options: {
|
|
|
|
example: string;
|
|
|
|
outputDir?: string;
|
|
|
|
defaultHtml?: string;
|
2022-07-14 09:56:56 +08:00
|
|
|
disableJS?: boolean;
|
|
|
|
}): Promise<ReturnValue>;
|
2022-01-27 14:32:38 +08:00
|
|
|
}
|
|
|
|
|
2022-11-15 10:37:30 +08:00
|
|
|
interface ReturnValue {
|
|
|
|
page: Page;
|
2022-01-27 14:32:38 +08:00
|
|
|
browser: Browser;
|
|
|
|
}
|
|
|
|
|
|
|
|
// get builtIn plugins
|
2023-08-30 13:29:18 +08:00
|
|
|
export const buildFixture = async function (example: string, commandArgs?: Record<string, string | boolean>) {
|
2022-04-07 16:41:17 +08:00
|
|
|
const rootDir = path.join(__dirname, `../../examples/${example}`);
|
2022-11-14 15:59:22 +08:00
|
|
|
// process.env.DISABLE_FS_CACHE = 'true';
|
|
|
|
const service = await createService({
|
|
|
|
rootDir,
|
|
|
|
command: 'build',
|
|
|
|
commandArgs: {
|
|
|
|
...(commandArgs || {}),
|
|
|
|
},
|
|
|
|
});
|
2022-04-07 16:41:17 +08:00
|
|
|
await service.run();
|
2022-09-07 14:49:54 +08:00
|
|
|
};
|
2022-01-27 14:32:38 +08:00
|
|
|
|
2022-03-09 15:20:08 +08:00
|
|
|
export const setupBrowser: SetupBrowser = async (options) => {
|
2022-07-14 09:56:56 +08:00
|
|
|
const { example, outputDir = 'build', defaultHtml = 'index.html', disableJS = true } = options;
|
2022-01-27 14:32:38 +08:00
|
|
|
const rootDir = path.join(__dirname, `../../examples/${example}`);
|
|
|
|
const port = await getPort();
|
|
|
|
const browser = new Browser({ cwd: path.join(rootDir, outputDir), port });
|
2022-08-09 16:42:48 +08:00
|
|
|
await browser.start();
|
2022-09-07 14:49:54 +08:00
|
|
|
console.log();
|
|
|
|
// When preview html generate by build, the path will not match the router info,
|
|
|
|
// so hydrate will not found the route component.
|
2022-09-16 14:05:27 +08:00
|
|
|
const page = await browser.page(`http://127.0.0.1:${port}`, `/${defaultHtml}`, disableJS);
|
2022-01-27 14:32:38 +08:00
|
|
|
return {
|
|
|
|
browser,
|
|
|
|
page,
|
2022-09-07 14:49:54 +08:00
|
|
|
};
|
|
|
|
};
|