From 77b8089e3abefc245f0ce83fc8da21c8341d9035 Mon Sep 17 00:00:00 2001 From: Antonio Scandurra Date: Fri, 11 Nov 2016 16:39:58 +0100 Subject: [PATCH] Use the snapshot result to require core modules --- script/build | 2 +- script/lib/generate-startup-snapshot.js | 54 ++++++------ src/atom-environment.coffee | 8 +- src/initialize-application-window.coffee | 16 ++-- src/main-process/main.js | 4 + src/task.coffee | 4 +- static/index.js | 102 ++++++----------------- 7 files changed, 70 insertions(+), 120 deletions(-) diff --git a/script/build b/script/build index 3a5a545d4..b4bfee6f9 100755 --- a/script/build +++ b/script/build @@ -50,6 +50,7 @@ process.on('unhandledRejection', function (e) { }) cleanOutputDirectory() +downloadChromedriver() copyAssets() transpileBabelPaths() transpileCoffeeScriptPaths() @@ -60,7 +61,6 @@ prebuildLessCache() generateMetadata() generateStartupSnapshot() generateAPIDocs() -downloadChromedriver() dumpSymbols() .then(packageApplication) .then(packagedAppPath => { diff --git a/script/lib/generate-startup-snapshot.js b/script/lib/generate-startup-snapshot.js index 86d1585c1..f8a0b5e4d 100644 --- a/script/lib/generate-startup-snapshot.js +++ b/script/lib/generate-startup-snapshot.js @@ -11,36 +11,40 @@ module.exports = function () { 'atom', 'vm', 'events', 'os', 'assert', 'buffer', 'tty', 'net', 'constants', 'http', 'https' ]) + const baseDirPath = path.join(CONFIG.intermediateAppPath, 'static') const snapshotScriptContent = electronLink({ - baseDirPath: CONFIG.intermediateAppPath, - mainPath: path.join(CONFIG.intermediateAppPath, 'src', 'initialize-snapshot.js'), + baseDirPath, + mainPath: path.resolve(baseDirPath, '..', 'src', 'initialize-application-window.js'), shouldExcludeModule: (modulePath) => { - const relativePath = path.relative(CONFIG.intermediateAppPath, modulePath) + const relativePath = path.relative(baseDirPath, modulePath) return ( modulePath.endsWith('.node') || modulePath === 'buffer-offset-index' || coreModules.has(modulePath) || - (relativePath.startsWith('src' + path.sep) && relativePath.endsWith('-element.js')) || - relativePath == path.join('exports', 'atom.js') || - relativePath == path.join('src', 'config-schema.js') || - relativePath == path.join('src', 'electron-shims.js') || - relativePath == path.join('src', 'module-cache.js') || - relativePath == path.join('src', 'safe-clipboard.js') || - relativePath == path.join('node_modules', 'atom-keymap', 'lib', 'command-event.js') || - relativePath == path.join('node_modules', 'babel-core', 'index.js') || - relativePath == path.join('node_modules', 'coffee-script', 'lib', 'coffee-script', 'register.js') || - relativePath == path.join('node_modules', 'cson-parser', 'node_modules', 'coffee-script', 'lib', 'coffee-script', 'register.js') || - relativePath == path.join('node_modules', 'fs-plus', 'lib', 'fs-plus.js') || - relativePath == path.join('node_modules', 'git-utils', 'lib', 'git.js') || - relativePath == path.join('node_modules', 'less', 'lib', 'less', 'fs.js') || - relativePath == path.join('node_modules', 'less', 'node_modules', 'graceful-fs', 'graceful-fs.js') || - relativePath == path.join('node_modules', 'marker-index', 'dist', 'native', 'marker-index.js') || - relativePath == path.join('node_modules', 'mime', 'mime.js') || - relativePath == path.join('node_modules', 'oniguruma', 'lib', 'oniguruma.js') || - relativePath == path.join('node_modules', 'pathwatcher', 'lib', 'main.js') || - relativePath == path.join('node_modules', 'request', 'request.js') || - relativePath == path.join('node_modules', 'resolve', 'index.js') || - relativePath == path.join('node_modules', 'resolve', 'lib', 'core.js') || - relativePath == path.join('node_modules', 'text-buffer', 'node_modules', 'pathwatcher', 'lib', 'main.js') + (relativePath.startsWith(path.join('..', 'src')) && relativePath.endsWith('-element.js')) || + relativePath == path.join('..', 'exports', 'atom.js') || + relativePath == path.join('..', 'src', 'config-schema.js') || + relativePath == path.join('..', 'src', 'electron-shims.js') || + relativePath == path.join('..', 'src', 'module-cache.js') || + relativePath == path.join('..', 'src', 'safe-clipboard.js') || + relativePath == path.join('..', 'node_modules', 'atom-keymap', 'lib', 'command-event.js') || + relativePath == path.join('..', 'node_modules', 'atom-keymap', 'lib', 'helpers.js') || + relativePath == path.join('..', 'node_modules', 'babel-core', 'index.js') || + relativePath == path.join('..', 'node_modules', 'clear-cut', 'index.js') || + relativePath == path.join('..', 'node_modules', 'coffee-script', 'lib', 'coffee-script', 'register.js') || + relativePath == path.join('..', 'node_modules', 'cson-parser', 'node_modules', 'coffee-script', 'lib', 'coffee-script', 'register.js') || + relativePath == path.join('..', 'node_modules', 'fs-plus', 'lib', 'fs-plus.js') || + relativePath == path.join('..', 'node_modules', 'git-utils', 'lib', 'git.js') || + relativePath == path.join('..', 'node_modules', 'less', 'lib', 'less', 'index.js') || + relativePath == path.join('..', 'node_modules', 'less', 'lib', 'less', 'fs.js') || + relativePath == path.join('..', 'node_modules', 'less', 'node_modules', 'graceful-fs', 'graceful-fs.js') || + relativePath == path.join('..', 'node_modules', 'marker-index', 'dist', 'native', 'marker-index.js') || + relativePath == path.join('..', 'node_modules', 'mime', 'mime.js') || + relativePath == path.join('..', 'node_modules', 'oniguruma', 'lib', 'oniguruma.js') || + relativePath == path.join('..', 'node_modules', 'pathwatcher', 'lib', 'main.js') || + relativePath == path.join('..', 'node_modules', 'request', 'request.js') || + relativePath == path.join('..', 'node_modules', 'resolve', 'index.js') || + relativePath == path.join('..', 'node_modules', 'resolve', 'lib', 'core.js') || + relativePath == path.join('..', 'node_modules', 'text-buffer', 'node_modules', 'pathwatcher', 'lib', 'main.js') ) } }) diff --git a/src/atom-environment.coffee b/src/atom-environment.coffee index 32cfa331d..09c85afca 100644 --- a/src/atom-environment.coffee +++ b/src/atom-environment.coffee @@ -132,7 +132,7 @@ class AtomEnvironment extends Model # Call .loadOrCreate instead constructor: (params={}) -> - {@blobStore, @applicationDelegate, @window, @document, @clipboard, @configDirPath, @enablePersistence, onlyLoadBaseStyleSheets} = params + {@applicationDelegate, @window, @document, @clipboard, @configDirPath, @enablePersistence, onlyLoadBaseStyleSheets} = params @unloaded = false @loadTime = null @@ -738,7 +738,6 @@ class AtomEnvironment extends Model @storeWindowBackground() @packages.deactivatePackages() - @saveBlobStoreSync() @unloaded = true openInitialEmptyEditorIfNecessary: -> @@ -867,11 +866,6 @@ class AtomEnvironment extends Model showSaveDialogSync: (options={}) -> @applicationDelegate.showSaveDialog(options) - saveBlobStoreSync: -> - return unless @enablePersistence - - @blobStore.save() - saveState: (options) -> new Promise (resolve, reject) => if @enablePersistence and @project diff --git a/src/initialize-application-window.coffee b/src/initialize-application-window.coffee index 7d3a23db7..38133e414 100644 --- a/src/initialize-application-window.coffee +++ b/src/initialize-application-window.coffee @@ -1,5 +1,10 @@ +AtomEnvironment = require './atom-environment' +ApplicationDelegate = require './application-delegate' +Clipboard = require './clipboard' +TextEditor = require './text-editor' + # Like sands through the hourglass, so are the days of our lives. -module.exports = ({blobStore}) -> +module.exports = -> {updateProcessEnv} = require('./update-process-env') path = require 'path' require './window' @@ -16,23 +21,18 @@ module.exports = ({blobStore}) -> # Make React faster process.env.NODE_ENV ?= 'production' unless devMode - AtomEnvironment = require './atom-environment' - ApplicationDelegate = require './application-delegate' - Clipboard = require './clipboard' - TextEditor = require './text-editor' - clipboard = new Clipboard TextEditor.setClipboard(clipboard) window.atom = new AtomEnvironment({ - window, document, clipboard, blobStore, + window, document, clipboard, applicationDelegate: new ApplicationDelegate, configDirPath: process.env.ATOM_HOME, enablePersistence: true, env: process.env }) - atom.startEditorWindow().then -> + window.atom.startEditorWindow().then -> # Workaround for focus getting cleared upon window creation windowFocused = -> window.removeEventListener('focus', windowFocused) diff --git a/src/main-process/main.js b/src/main-process/main.js index 7ccd1a6c3..e77d1930f 100644 --- a/src/main-process/main.js +++ b/src/main-process/main.js @@ -1,3 +1,7 @@ +if (typeof snapshotResult !== 'undefined') { + snapshotResult.setGlobals(global, process, {}, require) +} + const startTime = Date.now() const electron = require('electron') diff --git a/src/task.coffee b/src/task.coffee index fc8c5bd6b..8555b6554 100644 --- a/src/task.coffee +++ b/src/task.coffee @@ -70,7 +70,9 @@ class Task compileCachePath = require('./compile-cache').getCacheDirectory() taskBootstrapRequire = "require('#{require.resolve('./task-bootstrap')}');" bootstrap = """ - #{compileCacheRequire}.setCacheDirectory('#{compileCachePath}'); + snapshotResult.setGlobals(global, process, global, require) + CompileCache = snapshotResult.customRequire('../src/compile-cache.js') + CompileCache.setCacheDirectory('#{compileCachePath}'); #{taskBootstrapRequire} """ bootstrap = bootstrap.replace(/\\/g, "\\\\") diff --git a/static/index.js b/static/index.js index b76477e03..0f900d213 100644 --- a/static/index.js +++ b/static/index.js @@ -1,92 +1,41 @@ (function () { - var path = require('path') - var FileSystemBlobStore = require('../src/file-system-blob-store') - var NativeCompileCache = require('../src/native-compile-cache') - - var loadSettings = null - var loadSettingsError = null - var blobStore = null - - window.onload = function () { - try { - var startTime = Date.now() + let loadSettings + const Module = require('module') + const Path = require('path') + const vm = require('vm') + if (typeof snapshotResult !== 'undefined') { + window.onload = function () { + process.resourcesPath = Path.normalize(process.resourcesPath) process.on('unhandledRejection', function (error, promise) { console.error('Unhandled promise rejection %o with error: %o', promise, error) }) - blobStore = FileSystemBlobStore.load( - path.join(process.env.ATOM_HOME, 'blob-store/') - ) - NativeCompileCache.setCacheStore(blobStore) - NativeCompileCache.setV8Version(process.versions.v8) - NativeCompileCache.install() + parseLoadSettings() + setupAtomHome() + require('../src/crash-reporter-start')({_version: loadSettings.appVersion}) - // Normalize to make sure drive letter case is consistent on Windows - process.resourcesPath = path.normalize(process.resourcesPath) - - if (loadSettingsError) { - throw loadSettingsError + const entryPointDirPath = __dirname + Module.prototype.require = function (path) { + const absoluteFilePath = Module._resolveFilename(path, this, false) + const relativeFilePath = Path.relative(entryPointDirPath, absoluteFilePath) + const cachedModule = snapshotResult.customRequire.cache[relativeFilePath] + return cachedModule ? cachedModule : Module._load(path, this, false) } - var devMode = loadSettings.devMode || !loadSettings.resourcePath.startsWith(process.resourcesPath + path.sep) + snapshotResult.setGlobals(global, process, window, require) - if (devMode) { - setupDeprecatedPackages() - } + const CSON = snapshotResult.customRequire("../node_modules/season/lib/cson.js") + CSON.setCacheDir(Path.join(process.env.ATOM_HOME, 'compile-cache', 'cson')) - if (loadSettings.profileStartup) { - profileStartup(loadSettings, Date.now() - startTime) - } else { - setupWindow(loadSettings) - setLoadTime(Date.now() - startTime) - } - } catch (error) { - handleSetupError(error) + const CompileCache = snapshotResult.customRequire('../src/compile-cache.js') + CompileCache.setAtomHomeDirectory(process.env.ATOM_HOME) + + const initialize = snapshotResult.customRequire('../src/initialize-application-window.js') + initialize() } } - function setLoadTime (loadTime) { - if (global.atom) { - global.atom.loadTime = loadTime - } - } - - function handleSetupError (error) { - var currentWindow = require('electron').remote.getCurrentWindow() - currentWindow.setSize(800, 600) - currentWindow.center() - currentWindow.show() - currentWindow.openDevTools() - console.error(error.stack || error) - } - - function setupWindow (loadSettings) { - var CompileCache = require('../src/compile-cache') - CompileCache.setAtomHomeDirectory(process.env.ATOM_HOME) - - var ModuleCache = require('../src/module-cache') - ModuleCache.register(loadSettings) - ModuleCache.add(loadSettings.resourcePath) - - // By explicitly passing the app version here, we could save the call - // of "require('remote').require('app').getVersion()". - var startCrashReporter = require('../src/crash-reporter-start') - startCrashReporter({_version: loadSettings.appVersion}) - - setupVmCompatibility() - setupCsonCache(CompileCache.getCacheDirectory()) - - var initialize = require(loadSettings.windowInitializationScript) - return initialize({blobStore: blobStore}).then(function () { - require('electron').ipcRenderer.send('window-command', 'window:loaded') - }) - } - - function setupCsonCache (cacheDir) { - require('season').setCacheDir(path.join(cacheDir, 'cson')) - } - function setupVmCompatibility () { var vm = require('vm') if (!vm.Script.createContext) { @@ -149,7 +98,4 @@ process.env.ATOM_HOME = loadSettings.atomHome } } - - parseLoadSettings() - setupAtomHome() })()