Skip to content

Commit

Permalink
chore(html): support HMR for local dev (#33511)
Browse files Browse the repository at this point in the history
  • Loading branch information
Skn0tt authored Nov 12, 2024
1 parent 1b65f26 commit 1148843
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 14 deletions.
9 changes: 8 additions & 1 deletion packages/html-reporter/src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,14 @@ class ZipReport implements LoadedReport {
private _json!: HTMLReport;

async load() {
const zipReader = new zipjs.ZipReader(new zipjs.Data64URIReader(window.playwrightReportBase64!), { useWebWorkers: false });
const zipURI = await new Promise<string>(resolve => {
if (window.playwrightReportBase64)
return resolve(window.playwrightReportBase64);
window.addEventListener('message', event => event.source === window.opener && resolve(event.data), { once: true });
window.opener.postMessage('ready', '*');
});

const zipReader = new zipjs.ZipReader(new zipjs.Data64URIReader(zipURI), { useWebWorkers: false });
for (const entry of await zipReader.getEntries())
this._entries.set(entry.filename, entry);
this._json = await this.entry('report.json') as HTMLReport;
Expand Down
50 changes: 37 additions & 13 deletions packages/playwright/src/reporters/html.ts
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,33 @@ class HtmlBuilder {

this._addDataFile('report.json', htmlReport);

let singleTestId: string | undefined;
if (htmlReport.stats.total === 1) {
const testFile: TestFile = data.values().next().value.testFile;
singleTestId = testFile.tests[0].testId;
}

if (process.env.PW_HMR === '1') {
const redirectFile = path.join(this._reportFolder, 'index.html');

await this._writeReportData(redirectFile);

async function redirect() {
const hmrURL = new URL('http://localhost:44224'); // dev server, port is harcoded in build.js
const popup = window.open(hmrURL);
window.addEventListener('message', evt => {
if (evt.source === popup && evt.data === 'ready') {
popup!.postMessage((window as any).playwrightReportBase64, hmrURL.origin);
window.close();
}
}, { once: true });
}

fs.appendFileSync(redirectFile, `<script>(${redirect.toString()})()</script>`);

return { ok, singleTestId };
}

// Copy app.
const appFolder = path.join(require.resolve('playwright-core'), '..', 'lib', 'vite', 'htmlReport');
await copyFileAndMakeWritable(path.join(appFolder, 'index.html'), path.join(this._reportFolder, 'index.html'));
Expand All @@ -332,25 +359,22 @@ class HtmlBuilder {
}
}

// Inline report data.
const indexFile = path.join(this._reportFolder, 'index.html');
fs.appendFileSync(indexFile, '<script>\nwindow.playwrightReportBase64 = "data:application/zip;base64,');
await this._writeReportData(path.join(this._reportFolder, 'index.html'));


return { ok, singleTestId };
}

private async _writeReportData(filePath: string) {
fs.appendFileSync(filePath, '<script>\nwindow.playwrightReportBase64 = "data:application/zip;base64,');
await new Promise(f => {
this._dataZipFile!.end(undefined, () => {
this._dataZipFile!.outputStream
.pipe(new Base64Encoder())
.pipe(fs.createWriteStream(indexFile, { flags: 'a' })).on('close', f);
.pipe(fs.createWriteStream(filePath, { flags: 'a' })).on('close', f);
});
});
fs.appendFileSync(indexFile, '";</script>');

let singleTestId: string | undefined;
if (htmlReport.stats.total === 1) {
const testFile: TestFile = data.values().next().value.testFile;
singleTestId = testFile.tests[0].testId;
}

return { ok, singleTestId };
fs.appendFileSync(filePath, '";</script>');
}

private _addDataFile(fileName: string, data: any) {
Expand Down
7 changes: 7 additions & 0 deletions utils/build/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,13 @@ if (watchMode) {
cwd: path.join(__dirname, '..', '..', 'packages', 'trace-viewer'),
concurrent: true,
});
steps.push({
command: 'npx',
args: ['vite', '--port', '44224'],
shell: true,
cwd: path.join(__dirname, '..', '..', 'packages', 'html-reporter'),
concurrent: true,
});
}

// Generate injected.
Expand Down

0 comments on commit 1148843

Please sign in to comment.