Use the snapshot result to require core modules

This commit is contained in:
Antonio Scandurra
2016-11-11 16:39:58 +01:00
parent 6ccdc421cb
commit 77b8089e3a
7 changed files with 70 additions and 120 deletions

View File

@@ -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 => {

View File

@@ -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')
)
}
})

View File

@@ -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

View File

@@ -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)

View File

@@ -1,3 +1,7 @@
if (typeof snapshotResult !== 'undefined') {
snapshotResult.setGlobals(global, process, {}, require)
}
const startTime = Date.now()
const electron = require('electron')

View File

@@ -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, "\\\\")

View File

@@ -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()
})()