fix: support esm entrypoint to utility process (#40047)

This commit is contained in:
Samuel Attard 2023-09-29 14:38:37 -07:00 committed by GitHub
parent d6c8ff2e70
commit 371e83a8d2
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 41 additions and 3 deletions

View file

@ -1,5 +1,7 @@
import { pathToFileURL } from 'url';
import { ParentPort } from '@electron/internal/utility/parent-port';
const Module = require('module') as NodeJS.ModuleInternal;
const v8Util = process._linkedBinding('electron_common_v8_util');
const entryScript: string = v8Util.getHiddenValue(process, '_serviceStartupScript');
@ -34,5 +36,14 @@ parentPort.on('removeListener', (name: string) => {
});
// Finally load entry script.
process._firstFileName = Module._resolveFilename(entryScript, null, false);
Module._load(entryScript, Module, true);
const { loadESM } = __non_webpack_require__('internal/process/esm_loader');
const mainEntry = pathToFileURL(entryScript);
loadESM(async (esmLoader: any) => {
try {
await esmLoader.import(mainEntry.toString(), undefined, Object.create(null));
} catch (err) {
// @ts-ignore internalBinding is a secret internal global that we shouldn't
// really be using, so we ignore the type error instead of declaring it in types
internalBinding('errors').triggerUncaughtException(err);
}
});