From 7c79dfc686b139f673c5579f02030dd5c65a9d87 Mon Sep 17 00:00:00 2001 From: Sergey Belov Date: Wed, 20 Apr 2016 18:29:54 +0300 Subject: [PATCH] WIP fileCache --- package.json | 1 + techs/stylus.js | 50 ++++++++++++++++++++++++++++++++++++------------- 2 files changed, 38 insertions(+), 13 deletions(-) diff --git a/package.json b/package.json index 0f8e4d4..23b5e83 100644 --- a/package.json +++ b/package.json @@ -47,6 +47,7 @@ "autoprefixer": "6.3.4", "csswring": "4.2.2", "es6-promise": "3.1.2", + "lodash": "^4.7.0", "nib": "1.1.0", "postcss": "5.0.19", "postcss-import": "7.1.3", diff --git a/techs/stylus.js b/techs/stylus.js index 32398c7..0fa7f02 100644 --- a/techs/stylus.js +++ b/techs/stylus.js @@ -1,7 +1,8 @@ // Support node 0.10: `postcss` uses promises require('es6-promise').polyfill(); -var path = require('path'), +var _ = require('lodash'), + path = require('path'), vow = require('vow'), enb = require('enb'), vfs = enb.asyncFS || require('enb/lib/fs/async-fs'), @@ -119,18 +120,39 @@ module.exports = buildFlow.create() .builder(function (sourceFiles) { var node = this.node, filename = node.resolvePath(path.basename(this._target)), - stylesImports = this._prepareImports(sourceFiles); - - return this._processStylus(filename, stylesImports) - .spread(function (css, sourcemap) { - return this._processCss(filename, css, sourcemap); - }, this) - .then(function (result) { - return this._writeMap(filename + '.map', result.map) - .then(function () { - return result.css; - }); - }, this); + fileCache = node.getSharedResources().fileCache; + + return _(sourceFiles) + .map(function (sourceFile) { + var cacheKey = sourceFile.fullname + '.stylus.css'; + return fileCache.get(cacheKey, sourceFile.mtime) + .then(function (content) { + if (content !== null) { + console.log('**** Using %s from cache', cacheKey); + return content; + } + + var stylesImports = this._prepareImports([sourceFile]); + return this._processStylus(filename, stylesImports) + .spread(function (css, sourcemap) { + return this._processCss(filename, css, sourcemap); + }, this) + .then(function (result) { + return fileCache.put(cacheKey, result) + .then(function () { + return result; + }); + }); + }, this); + }.bind(this)) + .thru(vow.all) + .value() + .then(function (results) { + // TODO: merge source maps + // TODO: save source maps + //return this._writeMap(filename + '.map', result.map) + return _.map(results, 'css').join(EOL); + }); }) .methods(/** @lends StylusTech.prototype */{ @@ -271,6 +293,7 @@ module.exports = buildFlow.create() var stylus = require('stylus'); + console.time('stylus init'); var renderer = stylus(stylesImports) .set('prefix', this._prefix) .set('filename', filename) @@ -308,6 +331,7 @@ module.exports = buildFlow.create() this._importPaths.forEach(function (importPath) { renderer.import(importPath); }); + console.timeEnd('stylus init'); var defer = vow.defer();