From 4c6805bf7647efdf2d5d607e0b6dea1f98e9ed73 Mon Sep 17 00:00:00 2001 From: Antonio Scandurra Date: Thu, 9 Mar 2017 14:23:58 +0100 Subject: [PATCH] Store style sheet sources as auxiliary data in the snapshot --- script/config.js | 3 ++- script/lib/generate-startup-snapshot.js | 1 + script/lib/prebuild-less-cache.js | 23 +++++++++++++++++++++++ script/package.json | 2 +- script/tdd | 2 ++ src/less-compile-cache.coffee | 3 ++- src/theme-manager.coffee | 11 +++++++++-- 7 files changed, 40 insertions(+), 5 deletions(-) diff --git a/script/config.js b/script/config.js index 293af1136..2b9641fe2 100644 --- a/script/config.js +++ b/script/config.js @@ -26,7 +26,8 @@ module.exports = { repositoryRootPath, apmRootPath, scriptRootPath, buildOutputPath, docsOutputPath, intermediateAppPath, symbolsPath, electronDownloadPath, atomHomeDirPath, homeDirPath, - getApmBinPath, getNpmBinPath + getApmBinPath, getNpmBinPath, + snapshotAuxiliaryData: {} } function getChannel () { diff --git a/script/lib/generate-startup-snapshot.js b/script/lib/generate-startup-snapshot.js index be3f06839..3e1c7c0d2 100644 --- a/script/lib/generate-startup-snapshot.js +++ b/script/lib/generate-startup-snapshot.js @@ -15,6 +15,7 @@ module.exports = function (packagedAppPath) { baseDirPath, mainPath: path.resolve(baseDirPath, '..', 'src', 'initialize-application-window.js'), cachePath: path.join(CONFIG.atomHomeDirPath, 'snapshot-cache'), + auxiliaryData: CONFIG.snapshotAuxiliaryData, shouldExcludeModule: (modulePath) => { if (processedFiles > 0) { process.stdout.write('\r') diff --git a/script/lib/prebuild-less-cache.js b/script/lib/prebuild-less-cache.js index 1a1432fc9..bd1cf7338 100644 --- a/script/lib/prebuild-less-cache.js +++ b/script/lib/prebuild-less-cache.js @@ -28,6 +28,14 @@ module.exports = function () { } } + CONFIG.snapshotAuxiliaryData.lessSourcesByRelativeFilePath = {} + function saveIntoSnapshotAuxiliaryData (absoluteFilePath, contents) { + const relativeFilePath = path.relative(CONFIG.intermediateAppPath, absoluteFilePath) + if (!CONFIG.snapshotAuxiliaryData.lessSourcesByRelativeFilePath.hasOwnProperty(relativeFilePath)) { + CONFIG.snapshotAuxiliaryData.lessSourcesByRelativeFilePath[relativeFilePath] = contents + } + } + // Warm cache for every combination of the default UI and syntax themes, // because themes assign variables which may be used in any style sheet. for (let uiTheme of uiThemes) { @@ -52,6 +60,7 @@ module.exports = function () { lessSource = FALLBACK_VARIABLE_IMPORTS + lessSource } lessCache.cssForFile(lessFilePath, lessSource) + saveIntoSnapshotAuxiliaryData(lessFilePath, lessSource) } // Cache all styles in static; don't append variable imports @@ -69,10 +78,24 @@ module.exports = function () { // Cache styles for this UI theme const uiThemeMainPath = path.join(CONFIG.intermediateAppPath, 'node_modules', uiTheme, 'index.less') cacheCompiledCSS(uiThemeMainPath, true) + for (let lessFilePath of glob.sync(path.join(CONFIG.intermediateAppPath, 'node_modules', uiTheme, '**', '*.less'))) { + if (lessFilePath !== uiThemeMainPath) { + saveIntoSnapshotAuxiliaryData(lessFilePath, fs.readFileSync(lessFilePath, 'utf8')) + } + } // Cache styles for this syntax theme const syntaxThemeMainPath = path.join(CONFIG.intermediateAppPath, 'node_modules', syntaxTheme, 'index.less') cacheCompiledCSS(syntaxThemeMainPath, true) + for (let lessFilePath of glob.sync(path.join(CONFIG.intermediateAppPath, 'node_modules', syntaxTheme, '**', '*.less'))) { + if (lessFilePath !== syntaxThemeMainPath) { + saveIntoSnapshotAuxiliaryData(lessFilePath, fs.readFileSync(lessFilePath, 'utf8')) + } + } } } + + for (let lessFilePath of glob.sync(path.join(CONFIG.intermediateAppPath, 'node_modules', 'atom-ui', '**', '*.less'))) { + saveIntoSnapshotAuxiliaryData(lessFilePath, fs.readFileSync(lessFilePath, 'utf8')) + } } diff --git a/script/package.json b/script/package.json index c8106cc4d..f149189b0 100644 --- a/script/package.json +++ b/script/package.json @@ -8,7 +8,7 @@ "csslint": "1.0.2", "donna": "1.0.13", "electron-chromedriver": "~1.3", - "electron-link": "0.0.10", + "electron-link": "0.0.12", "electron-mksnapshot": "~1.3", "electron-packager": "7.3.0", "electron-winstaller": "2.5.1", diff --git a/script/tdd b/script/tdd index db78ef3e4..9c95be288 100755 --- a/script/tdd +++ b/script/tdd @@ -25,6 +25,7 @@ const generateMetadata = require('./lib/generate-metadata') const generateModuleCache = require('./lib/generate-module-cache') const generateStartupSnapshot = require('./lib/generate-startup-snapshot') const packageApplication = require('./lib/package-application') +const prebuildLessCache = require('./lib/prebuild-less-cache') const transpileBabelPaths = require('./lib/fast-transpile-babel-paths') const transpileCoffeeScriptPaths = require('./lib/fast-transpile-coffee-script-paths') @@ -39,4 +40,5 @@ transpileBabelPaths() transpileCoffeeScriptPaths() generateModuleCache() generateMetadata() +prebuildLessCache() packageApplication().then(generateStartupSnapshot) diff --git a/src/less-compile-cache.coffee b/src/less-compile-cache.coffee index c70f312ee..849ad0d55 100644 --- a/src/less-compile-cache.coffee +++ b/src/less-compile-cache.coffee @@ -6,7 +6,7 @@ module.exports = class LessCompileCache @cacheDir: path.join(process.env.ATOM_HOME, 'compile-cache', 'less') - constructor: ({resourcePath, importPaths}) -> + constructor: ({resourcePath, importPaths, lessSourcesByRelativeFilePath}) -> @lessSearchPaths = [ path.join(resourcePath, 'static', 'variables') path.join(resourcePath, 'static') @@ -21,6 +21,7 @@ class LessCompileCache cacheDir: @constructor.cacheDir importPaths: importPaths resourcePath: resourcePath + lessSourcesByRelativeFilePath: lessSourcesByRelativeFilePath fallbackDir: path.join(resourcePath, 'less-compile-cache') setImportPaths: (importPaths=[]) -> diff --git a/src/theme-manager.coffee b/src/theme-manager.coffee index 9e7c20380..6163ad3fc 100644 --- a/src/theme-manager.coffee +++ b/src/theme-manager.coffee @@ -18,6 +18,12 @@ class ThemeManager @packageManager.onDidActivateInitialPackages => @onDidChangeActiveThemes => @packageManager.reloadActivePackageStyleSheets() + @lessSourcesByRelativeFilePath = null + if typeof snapshotAuxiliaryData is 'undefined' + @lessSourcesByRelativeFilePath = {} + else + @lessSourcesByRelativeFilePath = snapshotAuxiliaryData.lessSourcesByRelativeFilePath + initialize: ({@resourcePath, @configDirPath, @safeMode}) -> ### @@ -196,7 +202,7 @@ class ThemeManager loadLessStylesheet: (lessStylesheetPath, importFallbackVariables=false) -> unless @lessCache? LessCompileCache = require './less-compile-cache' - @lessCache = new LessCompileCache({@resourcePath, importPaths: @getImportPaths()}) + @lessCache = new LessCompileCache({@resourcePath, @lessSourcesByRelativeFilePath, importPaths: @getImportPaths()}) try if importFallbackVariables @@ -204,7 +210,8 @@ class ThemeManager @import "variables/ui-variables"; @import "variables/syntax-variables"; """ - less = fs.readFileSync(lessStylesheetPath, 'utf8') + relativeFilePath = path.relative(@resourcePath, lessStylesheetPath) + less = @lessSourcesByRelativeFilePath[relativeFilePath] ? fs.readFileSync(lessStylesheetPath, 'utf8') @lessCache.cssForFile(lessStylesheetPath, [baseVarImports, less].join('\n')) else @lessCache.read(lessStylesheetPath)