diff --git a/package.json b/package.json index 891376183..4c0e8ba97 100644 --- a/package.json +++ b/package.json @@ -24,7 +24,7 @@ "atom-keymap": "^3.1.2", "bootstrap": "git+https://github.com/atom/bootstrap.git#6af81906189f1747fd6c93479e3d998ebe041372", "clear-cut": "0.4.0", - "coffee-cash": "0.5.0", + "coffee-cash": "0.6.0", "coffee-script": "1.8.0", "coffeestack": "^1.1", "color": "^0.7.3", diff --git a/src/6to5.coffee b/src/6to5.coffee index e24c63768..3ceb81480 100644 --- a/src/6to5.coffee +++ b/src/6to5.coffee @@ -139,15 +139,20 @@ loadFile = (module, filePath) -> js = getCachedJavaScript(cachePath) ? transpile(sourceCode, filePath, cachePath) module._compile(js, filePath) -register = (newCacheDir) -> - cacheDir = newCacheDir +register = -> Object.defineProperty(require.extensions, '.js', { writable: false value: loadFile }) +setCacheDirectory = (newCacheDir) -> + if cacheDir isnt newCacheDir + cacheDir = newCacheDir + jsCacheDir = null + module.exports = register: register + setCacheDirectory: setCacheDirectory getCacheMisses: -> stats.misses getCacheHits: -> stats.hits diff --git a/src/compile-cache.coffee b/src/compile-cache.coffee new file mode 100644 index 000000000..d5e9f0ccf --- /dev/null +++ b/src/compile-cache.coffee @@ -0,0 +1,26 @@ +path = require 'path' + +CSON = require 'season' +CoffeeCache = require 'coffee-cash' +to5 = require './6to5' + +cacheDir = path.join(process.env.ATOM_HOME, 'compile-cache') +# Use separate compile cache when sudo'ing as root to avoid permission issues +if process.env.USER is 'root' and process.env.SUDO_USER and process.env.SUDO_USER isnt process.env.USER + cacheDir = path.join(cacheDir, 'root') + +CoffeeCache.setCacheDirectory(path.join(cacheDir, 'coffee')) +CSON.setCacheDir(path.join(cacheDir, 'cson')) +to5.setCacheDirectory(path.join(cacheDir, 'js')) + +# This file is required directly by apm so that files can be cached during +# package install so that the first package load in Atom doesn't have to +# compile anything. +exports.addPathToCache = (filePath) -> + switch path.extname(filePath) + when '.coffee' + CoffeeCache.addPathToCache(filePath) + when '.cson' + CSON.readFileSync(filePath) + when '.js' + to5.addPathToCache(filePath) diff --git a/static/index.js b/static/index.js index 88634ee63..ebb921100 100644 --- a/static/index.js +++ b/static/index.js @@ -13,7 +13,9 @@ window.onload = function() { } // Setup caching of .coffee files - require('coffee-cash').register(path.join(cacheDir, 'coffee')); + var CoffeeCache = require('coffee-cash'); + CoffeeCache.setCacheDirectory(path.join(cacheDir, 'coffee')); + CoffeeCache.register(); // Set up caching of source maps for generated stack traces require('coffeestack').setCacheDirectory(path.join(cacheDir, 'coffee', 'source-maps')); @@ -22,7 +24,9 @@ window.onload = function() { require('season').setCacheDir(path.join(cacheDir, 'cson')); // Setup caching of .js files that start with "use 6to5" - require('../src/6to5').register(path.join(cacheDir, 'js')); + var to5 = require('../src/6to5'); + to5.setCacheDirectory(path.join(cacheDir, 'js')); + to5.register(); } // Ensure ATOM_HOME is always set before anything else is required