refactor: remove electron.asar and embed JS in binary (#18577)
* refactor: remove electron.asar and embed JS in binary * chore: update DEPS to merged node sha * chore: remove unneeded eslint ignore
This commit is contained in:
parent
901cdb22e3
commit
24b3d66767
16 changed files with 44 additions and 62 deletions
|
@ -106,7 +106,7 @@ if (process.resourcesPath) {
|
|||
for (packagePath of searchPaths) {
|
||||
try {
|
||||
packagePath = path.join(process.resourcesPath, packagePath)
|
||||
packageJson = __non_webpack_require__(path.join(packagePath, 'package.json')) // eslint-disable-line
|
||||
packageJson = Module._load(path.join(packagePath, 'package.json'))
|
||||
break
|
||||
} catch {
|
||||
continue
|
||||
|
@ -197,6 +197,7 @@ Promise.all([
|
|||
|
||||
if (packagePath) {
|
||||
// Finally load app's main.js and transfer control to C++.
|
||||
process._firstFileName = Module._resolveFilename(path.join(packagePath, mainStartupScript), null, false)
|
||||
Module._load(path.join(packagePath, mainStartupScript), Module, true)
|
||||
} else {
|
||||
console.error('Failed to locate a valid package to load (app, app.asar or default_app.asar)')
|
||||
|
|
|
@ -5,12 +5,6 @@ const Module = require('module')
|
|||
// Clear Node's global search paths.
|
||||
Module.globalPaths.length = 0
|
||||
|
||||
// Clear current bundles search paths.
|
||||
const currentNodeModule = Module._cache[__filename]
|
||||
if (currentNodeModule) {
|
||||
currentNodeModule.paths = []
|
||||
}
|
||||
|
||||
// Prevent Node from adding paths outside this app to search paths.
|
||||
const resourcesPathWithTrailingSlash = process.resourcesPath + path.sep
|
||||
const originalNodeModulePaths = Module._nodeModulePaths
|
||||
|
|
|
@ -27,7 +27,7 @@ class CallbacksRegistry {
|
|||
const location = match[1]
|
||||
if (location.includes('(native)')) continue
|
||||
if (location.includes('(<anonymous>)')) continue
|
||||
if (location.includes('electron.asar')) continue
|
||||
if (location.includes('electron/js2c')) continue
|
||||
|
||||
const ref = /([^/^)]*)\)?$/gi.exec(location)
|
||||
filenameAndLine = ref[1]
|
||||
|
|
|
@ -128,8 +128,9 @@ if (contextIsolation) {
|
|||
|
||||
if (nodeIntegration) {
|
||||
// Export node bindings to global.
|
||||
global.require = __non_webpack_require__ // eslint-disable-line
|
||||
global.module = Module._cache[__filename]
|
||||
const { makeRequireFunction } = __non_webpack_require__('internal/modules/cjs/helpers') // eslint-disable-line
|
||||
global.module = new Module('electron/js2c/renderer_init')
|
||||
global.require = makeRequireFunction(global.module)
|
||||
|
||||
// Set the __filename to the path of html file if it is file: protocol.
|
||||
if (window.location.protocol === 'file:') {
|
||||
|
@ -139,7 +140,7 @@ if (nodeIntegration) {
|
|||
if (process.platform === 'win32') {
|
||||
if (pathname[0] === '/') pathname = pathname.substr(1)
|
||||
|
||||
const isWindowsNetworkSharePath = location.hostname.length > 0 && __filename.startsWith('\\')
|
||||
const isWindowsNetworkSharePath = location.hostname.length > 0 && process.resourcesPath.startsWith('\\')
|
||||
if (isWindowsNetworkSharePath) {
|
||||
pathname = `//${location.host}/${pathname}`
|
||||
}
|
||||
|
@ -152,14 +153,15 @@ if (nodeIntegration) {
|
|||
global.module.filename = global.__filename
|
||||
|
||||
// Also search for module under the html file.
|
||||
global.module.paths = global.module.paths.concat(Module._nodeModulePaths(global.__dirname))
|
||||
global.module.paths = Module._nodeModulePaths(global.__dirname)
|
||||
} else {
|
||||
global.__filename = __filename
|
||||
global.__dirname = __dirname
|
||||
// For backwards compatibility we fake these two paths here
|
||||
global.__filename = path.join(process.resourcesPath, 'electron.asar', 'renderer', 'init.js')
|
||||
global.__dirname = path.join(process.resourcesPath, 'electron.asar', 'renderer')
|
||||
|
||||
if (appPath) {
|
||||
// Search for module under the app directory
|
||||
global.module.paths = global.module.paths.concat(Module._nodeModulePaths(appPath))
|
||||
global.module.paths = Module._nodeModulePaths(appPath)
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -203,7 +205,7 @@ for (const preloadScript of preloadScripts) {
|
|||
if (!isParentDir(getAppPath(), fs.realpathSync(preloadScript))) {
|
||||
throw new Error('Preload scripts outside of app path are not allowed')
|
||||
}
|
||||
__non_webpack_require__(preloadScript) // eslint-disable-line
|
||||
Module._load(preloadScript)
|
||||
} catch (error) {
|
||||
console.error(`Unable to load preload script: ${preloadScript}`)
|
||||
console.error(`${error}`)
|
||||
|
|
|
@ -14,8 +14,9 @@ require('../common/reset-search-paths')
|
|||
require('@electron/internal/common/init')
|
||||
|
||||
// Export node bindings to global.
|
||||
global.require = __non_webpack_require__ // eslint-disable-line
|
||||
global.module = Module._cache[__filename]
|
||||
const { makeRequireFunction } = __non_webpack_require__('internal/modules/cjs/helpers') // eslint-disable-line
|
||||
global.module = new Module('electron/js2c/worker_init')
|
||||
global.require = makeRequireFunction(global.module)
|
||||
|
||||
// Set the __filename to the path of html file if it is file: protocol.
|
||||
if (self.location.protocol === 'file:') {
|
||||
|
@ -27,8 +28,9 @@ if (self.location.protocol === 'file:') {
|
|||
global.module.filename = global.__filename
|
||||
|
||||
// Also search for module under the html file.
|
||||
global.module.paths = global.module.paths.concat(Module._nodeModulePaths(global.__dirname))
|
||||
global.module.paths = Module._nodeModulePaths(global.__dirname)
|
||||
} else {
|
||||
global.__filename = __filename
|
||||
global.__dirname = __dirname
|
||||
// For backwards compatibility we fake these two paths here
|
||||
global.__filename = path.join(process.resourcesPath, 'electron.asar', 'worker', 'init.js')
|
||||
global.__dirname = path.join(process.resourcesPath, 'electron.asar', 'worker')
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue