Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support esm entrypoint to utility process #40047

Merged
merged 1 commit into from
Sep 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 14 additions & 3 deletions lib/utility/init.ts
Original file line number Diff line number Diff line change
@@ -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');
Expand Down Expand Up @@ -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);
}
});
24 changes: 24 additions & 0 deletions spec/api-utility-process-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { BrowserWindow, MessageChannelMain, utilityProcess } from 'electron/main
import { ifit } from './lib/spec-helpers';
import { closeWindow } from './lib/window-helpers';
import { once } from 'node:events';
import { pathToFileURL } from 'node:url';

const fixturesPath = path.resolve(__dirname, 'fixtures', 'api', 'utility-process');
const isWindowsOnArm = process.platform === 'win32' && process.arch === 'arm64';
Expand Down Expand Up @@ -78,6 +79,12 @@ describe('utilityProcess module', () => {
expect(code).to.equal(1);
});

it('emits \'exit\' when there is uncaught exception in ESM', async () => {
const child = utilityProcess.fork(path.join(fixturesPath, 'exception.mjs'));
const [code] = await once(child, 'exit');
expect(code).to.equal(1);
});

it('emits \'exit\' when process.exit is called', async () => {
const exitCode = 2;
const child = utilityProcess.fork(path.join(fixturesPath, 'custom-exit.js'), [`--exitCode=${exitCode}`]);
Expand All @@ -97,6 +104,23 @@ describe('utilityProcess module', () => {
});
});

describe('esm', () => {
it('is launches an mjs file', async () => {
const fixtureFile = path.join(fixturesPath, 'esm.mjs');
const child = utilityProcess.fork(fixtureFile, [], {
stdio: 'pipe'
});
await once(child, 'spawn');
expect(child.stdout).to.not.be.null();
let log = '';
child.stdout!.on('data', (chunk) => {
log += chunk.toString('utf8');
});
await once(child, 'exit');
expect(log).to.equal(pathToFileURL(fixtureFile) + '\n');
});
});

describe('pid property', () => {
it('is valid when child process launches successfully', async () => {
const child = utilityProcess.fork(path.join(fixturesPath, 'empty.js'));
Expand Down
2 changes: 2 additions & 0 deletions spec/fixtures/api/utility-process/esm.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
console.log(import.meta.url);
process.exit(0);
1 change: 1 addition & 0 deletions spec/fixtures/api/utility-process/exception.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
nonExistingFunc(); // eslint-disable-line no-undef