chore: do not use module.exports in the injected esm files (#21412)

Follow up to #17145
This commit is contained in:
Pavel Feldman 2023-03-05 20:01:35 -08:00 committed by GitHub
parent 5735752c65
commit 47427e87ec
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 15 additions and 29 deletions

View File

@ -103,7 +103,7 @@ export class FrameExecutionContext extends js.ExecutionContext {
(() => { (() => {
const module = {}; const module = {};
${injectedScriptSource.source} ${injectedScriptSource.source}
return new module.exports( return new (module.exports.InjectedScript())(
globalThis, globalThis,
${isUnderTest()}, ${isUnderTest()},
"${sdkLanguage}", "${sdkLanguage}",

View File

@ -128,4 +128,4 @@ class ConsoleAPI {
} }
} }
module.exports = ConsoleAPI; export default ConsoleAPI;

View File

@ -380,7 +380,7 @@ export class InjectedScript {
(() => { (() => {
const module = {}; const module = {};
${source} ${source}
return module.exports; return module.exports.default();
})()`); })()`);
return new constrFunction(this, params); return new constrFunction(this, params);
} }
@ -1504,5 +1504,3 @@ function deepEquals(a: any, b: any): boolean {
return false; return false;
} }
module.exports = InjectedScript;

View File

@ -487,7 +487,7 @@ interface Embedder {
__pw_refreshOverlay(): void; __pw_refreshOverlay(): void;
} }
class PollingRecorder implements RecorderDelegate { export class PollingRecorder implements RecorderDelegate {
private _recorder: Recorder; private _recorder: Recorder;
private _embedder: Embedder; private _embedder: Embedder;
private _pollRecorderModeTimer: NodeJS.Timeout | undefined; private _pollRecorderModeTimer: NodeJS.Timeout | undefined;
@ -535,4 +535,4 @@ class PollingRecorder implements RecorderDelegate {
} }
} }
module.exports = PollingRecorder; export default PollingRecorder;

View File

@ -70,5 +70,3 @@ export class UtilityScript {
return safeJson(value); return safeJson(value);
} }
} }
module.exports = UtilityScript;

View File

@ -122,7 +122,7 @@ export class ExecutionContext extends SdkObject {
(() => { (() => {
const module = {}; const module = {};
${utilityScriptSource.source} ${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))); this._utilityScriptPromise = this._raceAgainstContextDestroyed(this._delegate.rawEvaluateHandle(source).then(objectId => new JSHandle(this, 'object', undefined, objectId)));
} }

View File

@ -22,6 +22,10 @@
"@recorder/*": ["../recorder/src/*"], "@recorder/*": ["../recorder/src/*"],
"@trace/*": ["../trace/src/*"], "@trace/*": ["../trace/src/*"],
"@web/*": ["../web/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, "useUnknownInCatchVariables": false,
}, },

View File

@ -29,23 +29,9 @@ const injectedScripts = [
path.join(ROOT, 'packages', 'playwright-core', 'src', 'server', 'injected', 'recorder.ts'), path.join(ROOT, 'packages', 'playwright-core', 'src', 'server', 'injected', 'recorder.ts'),
]; ];
const modulePrefix = `"use strict"; const modulePrefix = `
let __export = (target, all) => { var __export = (target, all) => {for (var name in all) target[name] = all[name];};
for (var name in all) var __toCommonJS = mod => ({ ...mod, __esModule: true });
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 });
`; `;
async function replaceEsbuildHeader(content, outFileJs) { async function replaceEsbuildHeader(content, outFileJs) {
@ -53,10 +39,10 @@ async function replaceEsbuildHeader(content, outFileJs) {
if (sourcesStart === -1) if (sourcesStart === -1)
throw new Error(`Did not find start of bundled code in ${outFileJs}`); 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. // Replace standard esbuild definition with our own which do not depend on builtins.
// See https://github.com/microsoft/playwright/issues/17029 // 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); content = modulePrefix + content.substring(sourcesStart);
await fs.promises.writeFile(outFileJs, content); await fs.promises.writeFile(outFileJs, content);
} }