refactor: type-safe module imports / requires (#41192)
This commit is contained in:
parent
3ec04fd449
commit
dac29f9949
4 changed files with 11 additions and 6 deletions
|
@ -3,6 +3,8 @@ import * as fs from 'fs';
|
||||||
import * as path from 'path';
|
import * as path from 'path';
|
||||||
|
|
||||||
import type * as defaultMenuModule from '@electron/internal/browser/default-menu';
|
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;
|
const Module = require('module') as NodeJS.ModuleInternal;
|
||||||
|
|
||||||
|
@ -132,7 +134,7 @@ if (packageJson.desktopName != null) {
|
||||||
// Set v8 flags, deliberately lazy load so that apps that do not use this
|
// Set v8 flags, deliberately lazy load so that apps that do not use this
|
||||||
// feature do not pay the price
|
// feature do not pay the price
|
||||||
if (packageJson.v8Flags != null) {
|
if (packageJson.v8Flags != null) {
|
||||||
require('v8').setFlagsFromString(packageJson.v8Flags);
|
(require('v8') as typeof v8).setFlagsFromString(packageJson.v8Flags);
|
||||||
}
|
}
|
||||||
|
|
||||||
app.setAppPath(packagePath);
|
app.setAppPath(packagePath);
|
||||||
|
@ -199,7 +201,7 @@ if (packagePath) {
|
||||||
// Finally load app's main.js and transfer control to C++.
|
// Finally load app's main.js and transfer control to C++.
|
||||||
if ((packageJson.type === 'module' && !mainStartupScript.endsWith('.cjs')) || mainStartupScript.endsWith('.mjs')) {
|
if ((packageJson.type === 'module' && !mainStartupScript.endsWith('.cjs')) || mainStartupScript.endsWith('.mjs')) {
|
||||||
const { loadESM } = __non_webpack_require__('internal/process/esm_loader');
|
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) => {
|
loadESM(async (esmLoader: any) => {
|
||||||
try {
|
try {
|
||||||
await esmLoader.import(main.toString(), undefined, Object.create(null));
|
await esmLoader.import(main.toString(), undefined, Object.create(null));
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
import * as util from 'util';
|
import * as util from 'util';
|
||||||
|
import type * as stream from 'stream';
|
||||||
|
|
||||||
const timers = require('timers');
|
import timers = require('timers');
|
||||||
|
|
||||||
type AnyFn = (...args: any[]) => any
|
type AnyFn = (...args: any[]) => any
|
||||||
|
|
||||||
|
@ -62,7 +63,7 @@ if (process.type === 'browser' ||
|
||||||
|
|
||||||
if (process.platform === 'win32') {
|
if (process.platform === 'win32') {
|
||||||
// Always returns EOF for stdin stream.
|
// Always returns EOF for stdin stream.
|
||||||
const { Readable } = require('stream');
|
const { Readable } = require('stream') as typeof stream;
|
||||||
const stdin = new Readable();
|
const stdin = new Readable();
|
||||||
stdin.push(null);
|
stdin.push(null);
|
||||||
Object.defineProperty(process, 'stdin', {
|
Object.defineProperty(process, 'stdin', {
|
||||||
|
|
|
@ -2,7 +2,9 @@ import { Buffer } from 'buffer';
|
||||||
import { constants } from 'fs';
|
import { constants } from 'fs';
|
||||||
import * as path from 'path';
|
import * as path from 'path';
|
||||||
import * as util from 'util';
|
import * as util from 'util';
|
||||||
|
|
||||||
import type * as Crypto from 'crypto';
|
import type * as Crypto from 'crypto';
|
||||||
|
import type * as os from 'os';
|
||||||
|
|
||||||
const asar = process._linkedBinding('electron_common_asar');
|
const asar = process._linkedBinding('electron_common_asar');
|
||||||
|
|
||||||
|
@ -255,7 +257,7 @@ export const wrapFsWithAsar = (fs: Record<string, any>) => {
|
||||||
if (!process.env.ELECTRON_LOG_ASAR_READS) return;
|
if (!process.env.ELECTRON_LOG_ASAR_READS) return;
|
||||||
if (!logFDs.has(asarPath)) {
|
if (!logFDs.has(asarPath)) {
|
||||||
const logFilename = `${path.basename(asarPath, '.asar')}-access-log.txt`;
|
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'));
|
logFDs.set(asarPath, fs.openSync(logPath, 'a'));
|
||||||
}
|
}
|
||||||
fs.writeSync(logFDs.get(asarPath), `${offset}: ${filePath}\n`);
|
fs.writeSync(logFDs.get(asarPath), `${offset}: ${filePath}\n`);
|
||||||
|
|
|
@ -1,4 +1,5 @@
|
||||||
import * as events from 'events';
|
import * as events from 'events';
|
||||||
|
import { setImmediate, clearImmediate } from 'timers';
|
||||||
import { IPC_MESSAGES } from '@electron/internal/common/ipc-messages';
|
import { IPC_MESSAGES } from '@electron/internal/common/ipc-messages';
|
||||||
|
|
||||||
import type * as ipcRendererUtilsModule from '@electron/internal/renderer/ipc-renderer-internal-utils';
|
import type * as ipcRendererUtilsModule from '@electron/internal/renderer/ipc-renderer-internal-utils';
|
||||||
|
@ -126,7 +127,6 @@ function runPreloadScript (preloadSrc: string) {
|
||||||
|
|
||||||
// eval in window scope
|
// eval in window scope
|
||||||
const preloadFn = binding.createPreloadScript(preloadWrapperSrc);
|
const preloadFn = binding.createPreloadScript(preloadWrapperSrc);
|
||||||
const { setImmediate, clearImmediate } = require('timers');
|
|
||||||
const exports = {};
|
const exports = {};
|
||||||
|
|
||||||
preloadFn(preloadRequire, preloadProcess, Buffer, global, setImmediate, clearImmediate, exports, { exports });
|
preloadFn(preloadRequire, preloadProcess, Buffer, global, setImmediate, clearImmediate, exports, { exports });
|
||||||
|
|
Loading…
Reference in a new issue