Skip to content

Commit bc527f6

Browse files
refactor: bundle the browser and renderer process electron code (electron#18553)
* refactor: bundle the browser and renderer process electron code * Bundles browser/init and renderer/init * Improves load performance of main process by ~40% * Improves load performance of renderer process by ~30% * Prevents users from importing our "requiring" our internal logic such as ipc-main-internal. This makes those message buses safer as they are less accessible, there is still some more work to be done though to lock down those buses completely. * The electron.asar file now only contains 2 files, as a future improvement maybe we can use atom_natives to ship these two files embedded in the binary * This also removes our dependency on browserify which had some strange edge cases that caused us to have to hack around require-order and stopped us using certain ES6/7 features we should have been able to use (async / await in some files in the sandboxed renderer init script) TLDR: Things are faster and better :) * fix: I really do not want to talk about it * chore: add performance improvements from debugging * fix: resolve the provided path so webpack thinks it is absolute * chore: fixup per PR review * fix: use webpacks ProvidePlugin to keep global, process and Buffer alive after deletion from global scope for use in internal code * fix: bundle worker/init as well to make node-in-workers work * chore: update wording as per feedback * chore: make the timers hack work when yarn is not used
1 parent a19e55a commit bc527f6

40 files changed

+1611
-919
lines changed

BUILD.gn

Lines changed: 40 additions & 132 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@ import("//tools/grit/repack.gni")
1010
import("//tools/v8_context_snapshot/v8_context_snapshot.gni")
1111
import("//v8/gni/snapshot_toolchain.gni")
1212
import("build/asar.gni")
13-
import("build/js_wrap.gni")
1413
import("build/npm.gni")
1514
import("build/tsc.gni")
15+
import("build/webpack/webpack.gni")
1616
import("buildflags/buildflags.gni")
1717
import("electron_paks.gni")
1818
import("filenames.auto.gni")
@@ -71,135 +71,70 @@ npm_action("build_electron_definitions") {
7171
]
7272
}
7373

74-
npm_action("atom_browserify_sandbox_unwrapped") {
75-
script = "browserify"
74+
webpack_build("electron_browser_bundle") {
7675
deps = [
7776
":build_electron_definitions",
7877
]
7978

80-
inputs = auto_filenames.sandbox_browserify_deps
79+
inputs = auto_filenames.browser_bundle_deps
8180

82-
outputs = [
83-
"$target_gen_dir/js2c/sandbox_bundle_unwrapped.js",
84-
]
85-
86-
args = [
87-
"lib/sandboxed_renderer/init.js",
88-
"-r",
89-
"./lib/sandboxed_renderer/api/exports/electron.js:electron",
90-
"-t",
91-
"aliasify",
92-
"-p",
93-
"[",
94-
"tsify",
95-
"-p",
96-
"tsconfig.electron.json",
97-
"]",
98-
"--standalone",
99-
"sandboxed_preload",
100-
"-o",
101-
rebase_path(outputs[0]),
102-
]
81+
config_file = "//electron/build/webpack/webpack.config.browser.js"
82+
out_file = "$target_gen_dir/electron_asar/browser/init.js"
10383
}
10484

105-
npm_action("atom_browserify_isolated_unwrapped") {
106-
script = "browserify"
85+
webpack_build("electron_renderer_bundle") {
10786
deps = [
10887
":build_electron_definitions",
10988
]
11089

111-
inputs = auto_filenames.isolated_browserify_deps
90+
inputs = auto_filenames.renderer_bundle_deps
11291

113-
outputs = [
114-
"$target_gen_dir/js2c/isolated_bundle_unwrapped.js",
115-
]
116-
117-
args = [
118-
"lib/isolated_renderer/init.js",
119-
"-t",
120-
"aliasify",
121-
"-p",
122-
"[",
123-
"tsify",
124-
"-p",
125-
"tsconfig.electron.json",
126-
"]",
127-
"--standalone",
128-
"isolated_preload",
129-
"-o",
130-
rebase_path(outputs[0]),
131-
]
92+
config_file = "//electron/build/webpack/webpack.config.renderer.js"
93+
out_file = "$target_gen_dir/electron_asar/renderer/init.js"
13294
}
13395

134-
npm_action("atom_browserify_content_script_unwrapped") {
135-
script = "browserify"
96+
webpack_build("electron_worker_bundle") {
13697
deps = [
13798
":build_electron_definitions",
13899
]
139100

140-
inputs = auto_filenames.context_script_browserify_deps
141-
142-
outputs = [
143-
"$target_gen_dir/js2c/content_script_bundle_unwrapped.js",
144-
]
101+
inputs = auto_filenames.worker_bundle_deps
145102

146-
args = [
147-
"lib/content_script/init.js",
148-
"-t",
149-
"aliasify",
150-
"-p",
151-
"[",
152-
"tsify",
153-
"-p",
154-
"tsconfig.electron.json",
155-
"]",
156-
"--standalone",
157-
"content_script_preload",
158-
"-o",
159-
rebase_path(outputs[0]),
160-
]
103+
config_file = "//electron/build/webpack/webpack.config.worker.js"
104+
out_file = "$target_gen_dir/electron_asar/worker/init.js"
161105
}
162106

163-
js_wrap("atom_browserify_content_script") {
107+
webpack_build("electron_sandboxed_renderer_bundle") {
164108
deps = [
165-
":atom_browserify_content_script_unwrapped",
109+
":build_electron_definitions",
166110
]
167111

168-
inputs = [
169-
"$target_gen_dir/js2c/content_script_bundle_unwrapped.js",
170-
]
112+
inputs = auto_filenames.sandbox_bundle_deps
171113

172-
outputs = [
173-
"$target_gen_dir/js2c/content_script_bundle.js",
174-
]
114+
config_file = "//electron/build/webpack/webpack.config.sandboxed_renderer.js"
115+
out_file = "$target_gen_dir/js2c/sandbox_bundle.js"
175116
}
176117

177-
js_wrap("atom_browserify_isolated") {
118+
webpack_build("electron_isolated_renderer_bundle") {
178119
deps = [
179-
":atom_browserify_isolated_unwrapped",
120+
":build_electron_definitions",
180121
]
181122

182-
inputs = [
183-
"$target_gen_dir/js2c/isolated_bundle_unwrapped.js",
184-
]
123+
inputs = auto_filenames.isolated_bundle_deps
185124

186-
outputs = [
187-
"$target_gen_dir/js2c/isolated_bundle.js",
188-
]
125+
config_file = "//electron/build/webpack/webpack.config.isolated_renderer.js"
126+
out_file = "$target_gen_dir/js2c/isolated_bundle.js"
189127
}
190128

191-
js_wrap("atom_browserify_sandbox") {
129+
webpack_build("electron_content_script_bundle") {
192130
deps = [
193-
":atom_browserify_sandbox_unwrapped",
131+
":build_electron_definitions",
194132
]
195133

196-
inputs = [
197-
"$target_gen_dir/js2c/sandbox_bundle_unwrapped.js",
198-
]
134+
inputs = auto_filenames.content_script_bundle_deps
199135

200-
outputs = [
201-
"$target_gen_dir/js2c/sandbox_bundle.js",
202-
]
136+
config_file = "//electron/build/webpack/webpack.config.content_script.js"
137+
out_file = "$target_gen_dir/js2c/content_script_bundle.js"
203138
}
204139

205140
copy("atom_js2c_copy") {
@@ -214,19 +149,19 @@ copy("atom_js2c_copy") {
214149

215150
action("atom_js2c") {
216151
deps = [
217-
":atom_browserify_content_script",
218-
":atom_browserify_isolated",
219-
":atom_browserify_sandbox",
220152
":atom_js2c_copy",
153+
":electron_content_script_bundle",
154+
":electron_isolated_renderer_bundle",
155+
":electron_sandboxed_renderer_bundle",
221156
]
222157

223-
browserify_sources = [
158+
webpack_sources = [
224159
"$target_gen_dir/js2c/content_script_bundle.js",
225160
"$target_gen_dir/js2c/isolated_bundle.js",
226161
"$target_gen_dir/js2c/sandbox_bundle.js",
227162
]
228163

229-
sources = browserify_sources + [
164+
sources = webpack_sources + [
230165
"$target_gen_dir/js2c/asar.js",
231166
"$target_gen_dir/js2c/asar_init.js",
232167
]
@@ -242,46 +177,19 @@ action("atom_js2c") {
242177
rebase_path(sources, root_build_dir)
243178
}
244179

245-
target_gen_electron_js = "$target_gen_dir/js/electron"
246180
target_gen_default_app_js = "$target_gen_dir/js/default_app"
247181

248-
typescript_build("lib_js") {
249-
deps = [
250-
":build_electron_definitions",
251-
]
252-
type_root = rebase_path("$target_gen_dir/tsc/electron/typings")
253-
254-
sources = filenames.js_sources
255-
if (enable_desktop_capturer) {
256-
sources += [
257-
"lib/browser/desktop-capturer.js",
258-
"lib/renderer/api/desktop-capturer.js",
259-
]
260-
}
261-
if (enable_view_api) {
262-
sources += [
263-
"lib/browser/api/views/box-layout.js",
264-
"lib/browser/api/views/button.js",
265-
"lib/browser/api/views/label-button.js",
266-
"lib/browser/api/views/layout-manager.js",
267-
"lib/browser/api/views/md-text-button.js",
268-
"lib/browser/api/views/resize-area.js",
269-
"lib/browser/api/views/text-field.js",
270-
]
271-
}
272-
273-
output_gen_dir = target_gen_electron_js
274-
output_dir_name = "lib"
275-
tsconfig = "tsconfig.electron.json"
276-
}
277-
278182
asar("electron_asar") {
279183
deps = [
280-
":lib_js",
184+
":electron_browser_bundle",
185+
":electron_renderer_bundle",
186+
":electron_worker_bundle",
281187
]
282188

283-
root = "$target_gen_electron_js/electron/lib"
284-
sources = get_target_outputs(":lib_js")
189+
root = "$target_gen_dir/electron_asar"
190+
sources = get_target_outputs(":electron_browser_bundle") +
191+
get_target_outputs(":electron_renderer_bundle") +
192+
get_target_outputs(":electron_worker_bundle")
285193
outputs = [
286194
"$root_out_dir/resources/electron.asar",
287195
]

build/js_wrap.gni

Lines changed: 0 additions & 19 deletions
This file was deleted.

build/js_wrap.py

Lines changed: 0 additions & 19 deletions
This file was deleted.

build/webpack/get-outputs.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
process.env.PRINT_WEBPACK_GRAPH = true
2+
require('./run-compiler')

build/webpack/run-compiler.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
const path = require('path')
2+
const webpack = require('webpack')
3+
4+
const configPath = process.argv[2]
5+
const outPath = path.resolve(process.argv[3])
6+
const config = require(configPath)
7+
config.output = {
8+
path: path.dirname(outPath),
9+
filename: path.basename(outPath)
10+
}
11+
12+
webpack(config, (err, stats) => {
13+
if (err) {
14+
console.error(err)
15+
process.exit(1)
16+
} else if (stats.hasErrors()) {
17+
console.error(stats.toString('normal'))
18+
process.exit(1)
19+
} else {
20+
process.exit(0)
21+
}
22+
})

0 commit comments

Comments
 (0)