refactor: bundle the browser and renderer process electron code (#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
This commit is contained in:
Samuel Attard 2019-06-02 13:03:03 -07:00 committed by GitHub
parent a19e55a902
commit bc527f6b51
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
40 changed files with 1612 additions and 920 deletions

View file

@ -14,41 +14,39 @@ const allDocs = fs.readdirSync(path.resolve(__dirname, '../docs/api'))
)
const main = async () => {
const browserifyTargets = [
const webpackTargets = [
{
name: 'sandbox_browserify_deps',
entry: 'lib/sandboxed_renderer/init.js'
name: 'sandbox_bundle_deps',
config: 'webpack.config.sandboxed_renderer.js'
},
{
name: 'isolated_browserify_deps',
entry: 'lib/isolated_renderer/init.js'
name: 'isolated_bundle_deps',
config: 'webpack.config.isolated_renderer.js'
},
{
name: 'context_script_browserify_deps',
entry: 'lib/content_script/init.js'
name: 'content_script_bundle_deps',
config: 'webpack.config.content_script.js'
},
{
name: 'browser_bundle_deps',
config: 'webpack.config.browser.js'
},
{
name: 'renderer_bundle_deps',
config: 'webpack.config.renderer.js'
},
{
name: 'worker_bundle_deps',
config: 'webpack.config.worker.js'
}
]
await Promise.all(browserifyTargets.map(async browserifyTarget => {
await Promise.all(webpackTargets.map(async webpackTarget => {
const tmpDir = await fs.mkdtemp(path.resolve(os.tmpdir(), 'electron-filenames-'))
const child = cp.spawn('node', [
'node_modules/browserify/bin/cmd.js',
browserifyTarget.entry,
...(browserifyTarget.name === 'sandbox_browserify_deps' ? [
'-r',
'./lib/sandboxed_renderer/api/exports/electron.js:electron'
] : []),
'-t',
'aliasify',
'-p',
'[',
'tsify',
'-p',
'tsconfig.electron.json',
']',
'-o',
path.resolve(tmpDir, 'out.js'),
'--list'
'build/webpack/get-outputs.js',
`./${webpackTarget.config}`,
path.resolve(tmpDir, `${webpackTarget.name}.measure.js`)
], {
cwd: path.resolve(__dirname, '..')
})
@ -60,28 +58,25 @@ const main = async () => {
await new Promise((resolve, reject) => child.on('exit', (code) => {
if (code !== 0) {
console.error(output)
return reject(new Error(`Failed to list browserify dependencies for entry: ${browserifyTarget.name}`))
return reject(new Error(`Failed to list webpack dependencies for entry: ${webpackTarget.name}`))
}
resolve()
}))
browserifyTarget.dependencies = output
.split('\n')
webpackTarget.dependencies = JSON.parse(output)
// Remove whitespace
.map(line => line.trim())
// Ignore empty lines
.filter(line => line)
// Get the relative path
.map(line => path.relative(rootPath, line))
// Only care about files in //electron
.filter(line => !line.startsWith('..'))
// Only care about our own files
.filter(line => !line.startsWith('node_modules'))
// All webpack builds depend on the tsconfig and package json files
.concat(['tsconfig.json', 'tsconfig.electron.json', 'package.json'])
// Make the generated list easier to read
.sort()
// All browserify commands depend on the tsconfig and package json files
.concat(['tsconfig.json', 'tsconfig.electron.json', 'package.json'])
await fs.remove(tmpDir)
}))
@ -93,7 +88,7 @@ auto_filenames = {
${allDocs.map(doc => ` "${doc}",`).join('\n')}
]
${browserifyTargets.map(target => ` ${target.name} = [
${webpackTargets.map(target => ` ${target.name} = [
${target.dependencies.map(dep => ` "${dep}",`).join('\n')}
]`).join('\n\n')}
}