Skip to content

Commit

Permalink
refactor: type-safe module imports / requires
Browse files Browse the repository at this point in the history
  • Loading branch information
miniak committed Feb 2, 2024
1 parent 9bfa16a commit 35af25f
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 6 deletions.
6 changes: 4 additions & 2 deletions lib/browser/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import * as fs from 'fs';
import * as path from 'path';

Check failure on line 3 in lib/browser/init.ts

View check run for this annotation

trop / Backportable? - 27-x-y

lib/browser/init.ts#L3

Patch Conflict

import type * as defaultMenuModule from '@electron/internal/browser/default-menu';
import type * as url from 'url';
import type * as v8 from 'v8';

const Module = require('module') as NodeJS.ModuleInternal;

Expand Down Expand Up @@ -132,7 +134,7 @@ if (packageJson.desktopName != null) {
// Set v8 flags, deliberately lazy load so that apps that do not use this
// feature do not pay the price
if (packageJson.v8Flags != null) {
require('v8').setFlagsFromString(packageJson.v8Flags);
(require('v8') as typeof v8).setFlagsFromString(packageJson.v8Flags);
}

app.setAppPath(packagePath);
Expand Down Expand Up @@ -199,7 +201,7 @@ if (packagePath) {
// Finally load app's main.js and transfer control to C++.
if ((packageJson.type === 'module' && !mainStartupScript.endsWith('.cjs')) || mainStartupScript.endsWith('.mjs')) {
const { loadESM } = __non_webpack_require__('internal/process/esm_loader');
const main = require('url').pathToFileURL(path.join(packagePath, mainStartupScript));
const main = (require('url') as typeof url).pathToFileURL(path.join(packagePath, mainStartupScript));
loadESM(async (esmLoader: any) => {
try {
await esmLoader.import(main.toString(), undefined, Object.create(null));
Expand Down
5 changes: 3 additions & 2 deletions lib/common/init.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import * as util from 'util';
import type * as stream from 'stream';

const timers = require('timers');
import timers = require('timers');

type AnyFn = (...args: any[]) => any

Expand Down Expand Up @@ -62,7 +63,7 @@ if (process.type === 'browser' ||

if (process.platform === 'win32') {
// Always returns EOF for stdin stream.
const { Readable } = require('stream');
const { Readable } = require('stream') as typeof stream;
const stdin = new Readable();
stdin.push(null);
Object.defineProperty(process, 'stdin', {
Expand Down
4 changes: 3 additions & 1 deletion lib/node/asar-fs-wrapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ import { Buffer } from 'buffer';
import { constants } from 'fs';
import * as path from 'path';
import * as util from 'util';

import type * as Crypto from 'crypto';
import type * as os from 'os';

const asar = process._linkedBinding('electron_common_asar');

Expand Down Expand Up @@ -255,7 +257,7 @@ export const wrapFsWithAsar = (fs: Record<string, any>) => {
if (!process.env.ELECTRON_LOG_ASAR_READS) return;
if (!logFDs.has(asarPath)) {
const logFilename = `${path.basename(asarPath, '.asar')}-access-log.txt`;
const logPath = path.join(require('os').tmpdir(), logFilename);
const logPath = path.join((require('os') as typeof os).tmpdir(), logFilename);
logFDs.set(asarPath, fs.openSync(logPath, 'a'));
}
fs.writeSync(logFDs.get(asarPath), `${offset}: ${filePath}\n`);
Expand Down
2 changes: 1 addition & 1 deletion lib/sandboxed_renderer/init.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import * as events from 'events';
import { setImmediate, clearImmediate } from 'timers';
import { IPC_MESSAGES } from '@electron/internal/common/ipc-messages';

import type * as ipcRendererUtilsModule from '@electron/internal/renderer/ipc-renderer-internal-utils';
Expand Down Expand Up @@ -126,7 +127,6 @@ function runPreloadScript (preloadSrc: string) {

// eval in window scope
const preloadFn = binding.createPreloadScript(preloadWrapperSrc);
const { setImmediate, clearImmediate } = require('timers');
const exports = {};

Check failure on line 130 in lib/sandboxed_renderer/init.ts

View check run for this annotation

trop / Backportable? - 27-x-y

lib/sandboxed_renderer/init.ts#L130

Patch Conflict
Raw output
++<<<<<<< HEAD
 +  const { setImmediate, clearImmediate } = require('timers');
++=======
+   const exports = {};
++>>>>>>> refactor: type-safe module imports / requires

preloadFn(preloadRequire, preloadProcess, Buffer, global, setImmediate, clearImmediate, exports, { exports });
Expand Down

0 comments on commit 35af25f

Please sign in to comment.