Skip to content

Commit e4a182b

Browse files
committed
patch(playwright): exposes __name as closure variable in browser evaluated functions
1 parent 7101ee6 commit e4a182b

File tree

5 files changed

+22
-13
lines changed

5 files changed

+22
-13
lines changed

packages/playwright-cloudflare/utils/build_injected.js

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
import fs from 'fs';
21
import path from 'path';
32
import { fileURLToPath } from 'url';
43
import { build } from 'vite';
4+
import { writeFile } from './utils.js';
55

66
const basedir = path.dirname(fileURLToPath(import.meta.url));
77

@@ -20,25 +20,24 @@ const basedir = path.dirname(fileURLToPath(import.meta.url));
2020
target: 'esnext',
2121
lib: {
2222
name: 'frameSnapshotStreamer',
23-
entry: path.join(basedir, '../../playwright-core/src/server/trace/recorder/snapshotterInjected.ts'),
23+
entry: [
24+
path.join(basedir, '../../playwright-core/src/server/trace/recorder/snapshotterInjected.ts'),
25+
path.join(basedir, '../../playwright-core/src/server/isomorphic/utilityScriptSerializers.ts'),
26+
],
2427
formats: ['es'],
2528
},
2629
terserOptions: {
2730
format: {
28-
// we need to ensure no comments are preserved
31+
// we need to ensure no comments are preserved
2932
comments: false
3033
}
3134
},
3235
rollupOptions: {
3336
output: {
3437
dir: path.join(basedir, '../src/injected'),
35-
entryFileNames: `snapshotterInjected.js`,
38+
entryFileNames: `[name].js`,
3639
},
3740
},
3841
},
3942
});
40-
41-
const { frameSnapshotStreamer } = await import('../src/injected/snapshotterInjected.js');
42-
const body = `export const frameSnapshotStreamer = ${JSON.stringify(frameSnapshotStreamer.toString())};`;
43-
fs.writeFileSync(path.join(basedir, '../src/injected/snapshotterInjected.js'), body);
4443
})();

packages/playwright-cloudflare/vite.config.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,8 @@ export default defineConfig({
4848
'./transport': path.resolve(__dirname, './src/cloudflare/webSocketTransport'),
4949
'../transport': path.resolve(__dirname, './src/cloudflare/webSocketTransport'),
5050

51-
// import function injected as string, otherwise snapshotter serializes the function which can have
52-
// function declared outside (e.g. `__name(...)`)
5351
'./snapshotterInjected': path.resolve(__dirname, './src/injected/snapshotterInjected'),
52+
'./isomorphic/utilityScriptSerializers': path.resolve(__dirname, './src/injected/utilityScriptSerializers'),
5453

5554
// It's not needed and this way we don't need to build and import utilsBundleImpl and babelBundleImpl
5655
'./transform': path.resolve(__dirname, './src/mocks/transform'),

packages/playwright-core/src/server/browserContext.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -615,7 +615,12 @@ export abstract class BrowserContext extends SdkObject {
615615
for (const originState of state.origins) {
616616
const frame = page.mainFrame();
617617
await frame.goto(metadata, originState.origin);
618-
await frame.evaluateExpression(`(${storageScript.restore})(${utilityScriptSerializers.source}, (${ensureBuiltins})(globalThis), ${JSON.stringify(originState)})`, { world: 'utility' });
618+
// function is most likely bundled with wrangler, which uses esbuild with keepNames enabled.
619+
// See: https://github.com/cloudflare/workers-sdk/issues/7107
620+
const restoreScript = `((__name => (${storageScript.restore}))(t => t))`;
621+
const utilityScriptSerializersScript = `((__name => (${storageScript.restore}))(t => t))`;
622+
const ensureBuiltinsScript = `((__name => (${ensureBuiltins}))(t => t))`;
623+
await frame.evaluateExpression(`(${restoreScript})(${utilityScriptSerializersScript}, (${ensureBuiltinsScript})(globalThis), ${JSON.stringify(originState)})`, { world: 'utility' });
619624
}
620625
await page.close(internalMetadata);
621626
}

packages/playwright-core/src/server/screenshotter.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -263,7 +263,10 @@ export class Screenshotter {
263263
if (disableAnimations)
264264
progress.log(' disabled all CSS animations');
265265
const syncAnimations = this._page._delegate.shouldToggleStyleSheetToSyncAnimations();
266-
await this._page.safeNonStallingEvaluateInAllFrames('(' + inPagePrepareForScreenshots.toString() + `)(${JSON.stringify(screenshotStyle)}, ${hideCaret}, ${disableAnimations}, ${syncAnimations})`, 'utility');
266+
// function is most likely bundled with wrangler, which uses esbuild with keepNames enabled.
267+
// See: https://github.com/cloudflare/workers-sdk/issues/7107
268+
const script = `((__name => (${inPagePrepareForScreenshots.toString()}))(t => t))`;
269+
await this._page.safeNonStallingEvaluateInAllFrames('(' + script + `)(${JSON.stringify(screenshotStyle)}, ${hideCaret}, ${disableAnimations}, ${syncAnimations})`, 'utility');
267270
if (!process.env.PW_TEST_SCREENSHOT_NO_FONTS_READY) {
268271
progress.log('waiting for fonts to load...');
269272
await frame.nonStallingEvaluateInExistingContext('document.fonts.ready', 'utility').catch(() => {});

packages/playwright-core/src/server/trace/recorder/snapshotter.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,10 @@ export class Snapshotter {
8888
];
8989

9090
const { javaScriptEnabled } = this._context._options;
91-
const initScript = `(${frameSnapshotStreamer})("${this._snapshotStreamer}", ${javaScriptEnabled || javaScriptEnabled === undefined})`;
91+
// function is most likely bundled with wrangler, which uses esbuild with keepNames enabled.
92+
// See: https://github.com/cloudflare/workers-sdk/issues/7107
93+
let initScript = `((__name => (${frameSnapshotStreamer}))(t => t))`;
94+
initScript = `(${initScript})("${this._snapshotStreamer}", ${javaScriptEnabled || javaScriptEnabled === undefined})`;
9295
await this._context.addInitScript(initScript);
9396
await this._runInAllFrames(initScript);
9497
}

0 commit comments

Comments
 (0)