Skip to content

Commit

Permalink
#77 fix caching for style dependencies
Browse files Browse the repository at this point in the history
  • Loading branch information
ernscht committed Jun 10, 2016
1 parent 5262db9 commit 22e83ea
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 25 deletions.
2 changes: 1 addition & 1 deletion app/templates/gulp/compile-css.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ var autoprefixer = require('autoprefixer');

module.exports = function (gulp, plugins) {
return function () {
var assets = utils.getSourceFiles('.css');
var assets = utils.getSourcePatterns('css');
var promises = [];
var browserCompatibility = utils.getBrowserCompatibility();

Expand Down
2 changes: 1 addition & 1 deletion app/templates/gulp/compile-js.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ var browserSync = utils.getBrowserSyncInstance();

module.exports = function (gulp, plugins) {
return function () {
var assets = utils.getSourceFiles('.js');
var assets = utils.getSourcePatterns('js');
var promises = [];

assets.forEach(function (asset) {
Expand Down
2 changes: 1 addition & 1 deletion app/templates/gulp/minify-css.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ var utils = require('./utils');

module.exports = function (gulp, plugins) {
return function () {
var assets = utils.getSourceFiles('.css');
var assets = utils.getSourcePatterns('css');

assets.forEach(function (asset) {
gulp
Expand Down
2 changes: 1 addition & 1 deletion app/templates/gulp/minify-js.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ var utils = require('./utils');

module.exports = function (gulp, plugins) {
return function () {
var assets = utils.getSourceFiles('.js');
var assets = utils.getSourcePatterns('js');

assets.forEach(function (asset) {
gulp
Expand Down
55 changes: 37 additions & 18 deletions app/templates/gulp/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ var path = require('path');
var gulp = require('gulp');
var plugins = require('gulp-load-plugins')();
var browserSync;
var assets = {};

function getBrowserCompatibility() {
return cfg.code.compatibility.browsers;
Expand All @@ -16,35 +17,52 @@ function getBrowserSyncInstance() {
return browserSync;
}

function getSourceFiles(ext) {
var assets = [];
function getSourcePatterns(ext) {

for (var key in cfg.assets) {
if (cfg.assets.hasOwnProperty(key) && ext === path.extname(key)) {
var asset = cfg.assets[key],
var type = typeof ext === 'string' && ( ext === 'js' || ext === 'css' ) ? ext : null;

if (!assets.hasOwnProperty('js') || !assets.hasOwnProperty('css')) {
updateSourcePatterns();
}

return type ? assets[type] : assets;
}

function updateSourcePatterns() {
var key, ext, type, asset, result, patternKey, patternPath;

assets = {
css: [],
js: []
};

for (key in cfg.assets) {
if (cfg.assets.hasOwnProperty(key)) {
ext = path.extname(key);
if (ext) {
type = ext.replace(/[^a-z]/g, '');
asset = cfg.assets[key];
result = {
name: key,
deps: [],
src: []
};

for (var fkey in asset) {
if (asset.hasOwnProperty(fkey)) {
var filepath = asset[fkey];
if (filepath.indexOf('+') === 0) {
result.deps.push(filepath.substr(1));
}
else {
result.src.push(filepath);
for (patternKey in asset) {
if (asset.hasOwnProperty(patternKey)) {
patternPath = asset[patternKey];
if (patternPath.indexOf('+') === 0) {
result.deps.push(patternPath.substr(1));
}
else {
result.src.push(patternPath);
}
}
}
assets[type].push(result);
}

assets.push(result);
}
}

return assets;
}

function getTask(task) {
Expand Down Expand Up @@ -79,7 +97,8 @@ function splitJsAssets(asset) {
module.exports = {
getBrowserCompatibility: getBrowserCompatibility,
getBrowserSyncInstance: getBrowserSyncInstance,
getSourceFiles: getSourceFiles,
getSourcePatterns: getSourcePatterns,
updateSourcePatterns: updateSourcePatterns,
getTask: getTask,
reloadConfig: reloadConfig<% if (options.js === 'TypeScript') { %>,
splitJsAssets: splitJsAssets<% } %>
Expand Down
22 changes: 19 additions & 3 deletions app/templates/gulp/watch-assets.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,34 @@
var cfg = require('../app/core/config');
var utils = require('./utils');
var globby = require('globby');
var browserSync = utils.getBrowserSyncInstance();

module.exports = function (gulp, plugins) {
return function () {
var isDependentStyleSource = function(file) {
var isDependent = false;
var cssAssets = utils.getSourcePatterns('css');
cssAssets.forEach(function (asset) {
globby.sync(asset.deps).forEach(function (path) {
if ( file.replace(/\\/g, '/').endsWith(path) ) {
isDependent = true;
}
});
});

return isDependent;
};
var clearCache = function (e) {
if (
'unlink' === e.event ||
'add' === e.event ||
e.path.endsWith('config.json')
e.path.endsWith('config.json') ||
isDependentStyleSource(e.path)
) {
// forget all
plugins.cached.caches = {};
var assets = utils.getSourceFiles('.css');
assets.forEach(function (asset) {
var cssAssets = utils.getSourcePatterns('css');
cssAssets.forEach(function (asset) {
if (plugins.remember.cacheFor(asset.name)) {
plugins.remember.forgetAll(asset.name);
}
Expand All @@ -26,6 +41,7 @@ module.exports = function (gulp, plugins) {
], function (e) {
cfg = utils.reloadConfig();
clearCache(e);
utils.updateSourcePatterns();
gulp.start('compile-css');
gulp.start('compile-js');
});
Expand Down

0 comments on commit 22e83ea

Please sign in to comment.