From 47427e87eca933da13f7bcaa44ef5124a5d425d3 Mon Sep 17 00:00:00 2001 From: Pavel Feldman Date: Sun, 5 Mar 2023 20:01:35 -0800 Subject: [PATCH] chore: do not use module.exports in the injected esm files (#21412) Follow up to #17145 --- packages/playwright-core/src/server/dom.ts | 2 +- .../src/server/injected/consoleApi.ts | 2 +- .../src/server/injected/injectedScript.ts | 4 +--- .../src/server/injected/recorder.ts | 4 ++-- .../src/server/injected/utilityScript.ts | 2 -- .../playwright-core/src/server/javascript.ts | 2 +- packages/trace-viewer/tsconfig.json | 4 ++++ utils/generate_injected.js | 24 ++++--------------- 8 files changed, 15 insertions(+), 29 deletions(-) diff --git a/packages/playwright-core/src/server/dom.ts b/packages/playwright-core/src/server/dom.ts index 214e8cdd47..2161cec308 100644 --- a/packages/playwright-core/src/server/dom.ts +++ b/packages/playwright-core/src/server/dom.ts @@ -103,7 +103,7 @@ export class FrameExecutionContext extends js.ExecutionContext { (() => { const module = {}; ${injectedScriptSource.source} - return new module.exports( + return new (module.exports.InjectedScript())( globalThis, ${isUnderTest()}, "${sdkLanguage}", diff --git a/packages/playwright-core/src/server/injected/consoleApi.ts b/packages/playwright-core/src/server/injected/consoleApi.ts index fd7f751655..fbdb44e8d5 100644 --- a/packages/playwright-core/src/server/injected/consoleApi.ts +++ b/packages/playwright-core/src/server/injected/consoleApi.ts @@ -128,4 +128,4 @@ class ConsoleAPI { } } -module.exports = ConsoleAPI; +export default ConsoleAPI; diff --git a/packages/playwright-core/src/server/injected/injectedScript.ts b/packages/playwright-core/src/server/injected/injectedScript.ts index b2708b7543..1a361e3add 100644 --- a/packages/playwright-core/src/server/injected/injectedScript.ts +++ b/packages/playwright-core/src/server/injected/injectedScript.ts @@ -380,7 +380,7 @@ export class InjectedScript { (() => { const module = {}; ${source} - return module.exports; + return module.exports.default(); })()`); return new constrFunction(this, params); } @@ -1504,5 +1504,3 @@ function deepEquals(a: any, b: any): boolean { return false; } - -module.exports = InjectedScript; diff --git a/packages/playwright-core/src/server/injected/recorder.ts b/packages/playwright-core/src/server/injected/recorder.ts index f7edf68113..217ecb7027 100644 --- a/packages/playwright-core/src/server/injected/recorder.ts +++ b/packages/playwright-core/src/server/injected/recorder.ts @@ -487,7 +487,7 @@ interface Embedder { __pw_refreshOverlay(): void; } -class PollingRecorder implements RecorderDelegate { +export class PollingRecorder implements RecorderDelegate { private _recorder: Recorder; private _embedder: Embedder; private _pollRecorderModeTimer: NodeJS.Timeout | undefined; @@ -535,4 +535,4 @@ class PollingRecorder implements RecorderDelegate { } } -module.exports = PollingRecorder; +export default PollingRecorder; diff --git a/packages/playwright-core/src/server/injected/utilityScript.ts b/packages/playwright-core/src/server/injected/utilityScript.ts index 08231ba594..34932f82c3 100644 --- a/packages/playwright-core/src/server/injected/utilityScript.ts +++ b/packages/playwright-core/src/server/injected/utilityScript.ts @@ -70,5 +70,3 @@ export class UtilityScript { return safeJson(value); } } - -module.exports = UtilityScript; diff --git a/packages/playwright-core/src/server/javascript.ts b/packages/playwright-core/src/server/javascript.ts index 7ba6601f70..f67a3f6772 100644 --- a/packages/playwright-core/src/server/javascript.ts +++ b/packages/playwright-core/src/server/javascript.ts @@ -122,7 +122,7 @@ export class ExecutionContext extends SdkObject { (() => { const module = {}; ${utilityScriptSource.source} - return new module.exports(); + return new (module.exports.UtilityScript())(); })();`; this._utilityScriptPromise = this._raceAgainstContextDestroyed(this._delegate.rawEvaluateHandle(source).then(objectId => new JSHandle(this, 'object', undefined, objectId))); } diff --git a/packages/trace-viewer/tsconfig.json b/packages/trace-viewer/tsconfig.json index f37fc00edd..1de869ca1c 100644 --- a/packages/trace-viewer/tsconfig.json +++ b/packages/trace-viewer/tsconfig.json @@ -22,6 +22,10 @@ "@recorder/*": ["../recorder/src/*"], "@trace/*": ["../trace/src/*"], "@web/*": ["../web/src/*"], + // Resolving type dependencies will start processing types in @playwright/test + // which in turn will start parsing the files and hence stumble upon the core + // imports. + "playwright-core/lib/*": ["../playwright-core/src/*"], }, "useUnknownInCatchVariables": false, }, diff --git a/utils/generate_injected.js b/utils/generate_injected.js index 3656fb3c53..652f59d0a3 100644 --- a/utils/generate_injected.js +++ b/utils/generate_injected.js @@ -29,23 +29,9 @@ const injectedScripts = [ path.join(ROOT, 'packages', 'playwright-core', 'src', 'server', 'injected', 'recorder.ts'), ]; -const modulePrefix = `"use strict"; -let __export = (target, all) => { - for (var name in all) - target[name] = all[name]; -}; -let __commonJS = cb => function __require() { - let fn; - for (const name in cb) { - fn = cb[name]; - break; - } - const exports = {}; - fn(exports); - return exports; -}; -let __toESM = mod => ({ ...mod, 'default': mod }); -let __toCommonJS = mod => ({ ...mod, __esModule: true }); +const modulePrefix = ` +var __export = (target, all) => {for (var name in all) target[name] = all[name];}; +var __toCommonJS = mod => ({ ...mod, __esModule: true }); `; async function replaceEsbuildHeader(content, outFileJs) { @@ -53,10 +39,10 @@ async function replaceEsbuildHeader(content, outFileJs) { if (sourcesStart === -1) throw new Error(`Did not find start of bundled code in ${outFileJs}`); - const preambule = content.substring(0, sourcesStart); + const preamble = content.substring(0, sourcesStart); // Replace standard esbuild definition with our own which do not depend on builtins. // See https://github.com/microsoft/playwright/issues/17029 - if (preambule.indexOf('__toESM') !== -1 || preambule.indexOf('__toCommonJS') !== -1) { + if (preamble.indexOf('__toCommonJS') !== -1) { content = modulePrefix + content.substring(sourcesStart); await fs.promises.writeFile(outFileJs, content); }