Cache compiled LESS files

This commit is contained in:
Kevin Sawicki
2013-09-16 12:29:16 -07:00
parent 134510dfe1
commit ad381087dd
3 changed files with 28 additions and 11 deletions

View File

@@ -20,6 +20,7 @@
"jasmine-focused": "~0.12.0",
"mkdirp": "0.3.5",
"less": "git://github.com/nathansobo/less.js.git",
"less-cache": "0.5.0",
"nak": "0.2.18",
"nslog": "0.1.0",
"oniguruma": "0.20.0",

View File

@@ -0,0 +1,21 @@
_ = require 'underscore'
LessCache = require 'less-cache'
module.exports =
class LessCompileCache
_.extend @prototype, require('subscriber')
@cacheDir: '/tmp/atom-compile-cache/less'
constructor: ->
importPaths =
@cache = new LessCache
cacheDir: @constructor.cacheDir
importPaths: @getImportPaths()
@subscribe atom.themes, 'reloaded', => @cache.setImportPaths(@getImportPaths())
getImportPaths: -> atom.themes.getImportPaths().concat(config.lessSearchPaths)
read: (stylesheetPath) -> @cache.readFileSync(stylesheetPath)
destroy: -> @unsubscribe()

View File

@@ -3,7 +3,6 @@ path = require 'path'
telepath = require 'telepath'
$ = require 'jquery'
_ = require 'underscore'
less = require 'less'
remote = require 'remote'
ipc = require 'ipc'
WindowEventHandler = require 'window-event-handler'
@@ -14,6 +13,7 @@ require 'space-pen-extensions'
deserializers = {}
deferredDeserializers = {}
defaultWindowDimensions = {width: 800, height: 600}
lessCache = null
### Internal ###
@@ -80,6 +80,7 @@ window.unloadEditorWindow = ->
rootView.remove()
project.destroy()
windowEventHandler?.unsubscribe()
lessCache?.destroy()
window.rootView = null
window.project = null
@@ -153,18 +154,12 @@ window.loadStylesheet = (stylesheetPath) ->
fsUtils.read(stylesheetPath)
window.loadLessStylesheet = (lessStylesheetPath) ->
importPaths = atom.themes.getImportPaths()
parser = new less.Parser
syncImport: true
paths: importPaths.concat(config.lessSearchPaths)
filename: lessStylesheetPath
unless lessCache?
LessCompileCache = require 'less-compile-cache'
lessCache = new LessCompileCache()
try
content = null
parser.parse fsUtils.read(lessStylesheetPath), (e, tree) ->
throw e if e?
content = tree.toCSS()
content
lessCache.read(lessStylesheetPath)
catch e
console.error """
Error compiling less stylesheet: #{lessStylesheetPath}