Skip to content

Commit

Permalink
build: make electron renderer init scripts profilable (#23892)
Browse files Browse the repository at this point in the history
The devtools profiler is not attached at the point we run out init scripts (or our apps preload scripts), we do not really want to change when we run these init scripts but for when a dev is doing performance work it makes sense to give them an option to make the devtools profiler actually work on both our init scripts and their preload script.  This PR adds that logic behind an environment variable ELECTRON_PROFILE_INIT_SCRIPTS.

Co-authored-by: Samuel Attard <samuel.r.attard@gmail.com>
  • Loading branch information
trop[bot] and MarshallOfSound committed Jun 2, 2020
1 parent 26c6c81 commit dd7c9fb
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 3 deletions.
16 changes: 16 additions & 0 deletions build/webpack/run-compiler.js
@@ -1,3 +1,4 @@
const fs = require('fs');
const path = require('path')
const webpack = require('webpack')

Expand All @@ -9,6 +10,9 @@ config.output = {
filename: path.basename(outPath)
}

const { wrapInitWithProfilingTimeout } = config;
delete config.wrapInitWithProfilingTimeout;

webpack(config, (err, stats) => {
if (err) {
console.error(err)
Expand All @@ -17,6 +21,18 @@ webpack(config, (err, stats) => {
console.error(stats.toString('normal'))
process.exit(1)
} else {
if (wrapInitWithProfilingTimeout) {
const contents = fs.readFileSync(outPath, 'utf8');
const newContents = `function ___electron_webpack_init__() {
${contents}
};
if ((globalThis.process || binding.process).argv.includes("--profile-electron-init")) {
setTimeout(___electron_webpack_init__, 0);
} else {
___electron_webpack_init__();
}`;
fs.writeFileSync(outPath, newContents);
}
process.exit(0)
}
})
4 changes: 3 additions & 1 deletion build/webpack/webpack.config.base.js
Expand Up @@ -90,7 +90,8 @@ module.exports = ({
alwaysHasNode,
loadElectronFromAlternateTarget,
targetDeletesNodeGlobals,
target
target,
wrapInitWithProfilingTimeout
}) => {
let entry = path.resolve(electronRoot, 'lib', target, 'init.ts')
if (!fs.existsSync(entry)) {
Expand All @@ -105,6 +106,7 @@ module.exports = ({
output: {
filename: `${target}.bundle.js`
},
wrapInitWithProfilingTimeout,
resolve: {
alias: {
...alias,
Expand Down
3 changes: 2 additions & 1 deletion build/webpack/webpack.config.renderer.js
@@ -1,5 +1,6 @@
module.exports = require('./webpack.config.base')({
target: 'renderer',
alwaysHasNode: true,
targetDeletesNodeGlobals: true
targetDeletesNodeGlobals: true,
wrapInitWithProfilingTimeout: true
})
3 changes: 2 additions & 1 deletion build/webpack/webpack.config.sandboxed_renderer.js
@@ -1,4 +1,5 @@
module.exports = require('./webpack.config.base')({
target: 'sandboxed_renderer',
alwaysHasNode: false
alwaysHasNode: false,
wrapInitWithProfilingTimeout: true,
})
1 change: 1 addition & 0 deletions build/webpack/webpack.gni
Expand Up @@ -16,6 +16,7 @@ template("webpack_build") {
inputs = [
invoker.config_file,
"//electron/build/webpack/webpack.config.base.js",
"//electron/build/webpack/run-compiler.js",
"//electron/tsconfig.json",
"//electron/yarn.lock",
"//electron/typings/internal-ambient.d.ts",
Expand Down
5 changes: 5 additions & 0 deletions shell/browser/electron_browser_client.cc
Expand Up @@ -765,6 +765,11 @@ void ElectronBrowserClient::AppendExtraCommandLineSwitches(
command_line->AppendSwitchPath(switches::kAppPath, app_path);
}

std::unique_ptr<base::Environment> env(base::Environment::Create());
if (env->HasVar("ELECTRON_PROFILE_INIT_SCRIPTS")) {
command_line->AppendSwitch("profile-electron-init");
}

content::WebContents* web_contents =
GetWebContentsFromProcessID(process_id);
if (web_contents) {
Expand Down

0 comments on commit dd7c9fb

Please sign in to comment.