Skip to content

Commit

Permalink
refactor: update the Sassdoc custom plugin for Webpack to cache the r…
Browse files Browse the repository at this point in the history
…esults from Sassdoc to memory; this prevents writing out the Sassdoc data to disk every time Webpack compiles (which was causing Pattern Lab to recompile every single time Webpack compiled)
  • Loading branch information
sghoweri committed Aug 24, 2018
1 parent fa72312 commit 4b07507
Showing 1 changed file with 36 additions and 3 deletions.
39 changes: 36 additions & 3 deletions packages/build-tools/plugins/sassdoc-webpack-plugin.js
Expand Up @@ -8,6 +8,8 @@ const fs = require('fs');
const path = require('path');
const sassdoc = require('sassdoc');
const yaml = require('js-yaml');
const crypto = require('crypto');
const BoltCache = require('../utils/cache');

class SassDocPlugin {
constructor(options, pluginOptions) {
Expand Down Expand Up @@ -36,9 +38,40 @@ class SassDocPlugin {

compiler.hooks.afterEmit.tapPromise('SassDocPlugin', compilation => {
return sassdoc.parse(self.options.src, self.options).then(function(data) {
fs.writeFile(self.options.dest, JSON.stringify(data), 'utf8', err => {
if (err) throw err;
});
function getHash(data) {
return crypto
.createHash('md5')
.update(JSON.stringify(data))
.digest('hex');
}

function writeSassdocFile(file, data) {
fs.writeFile(file, JSON.stringify(data), 'utf8', err => {
if (err) throw err;
});
}

// If cached Sassdoc data exists, compare hashes to only write to disk when absolutely necessary
if (BoltCache.get('sassdoc')) {
const newData = getHash(data);
const oldData = getHash(BoltCache.get('sassdoc'));

if (oldData !== newData) {
if (config.verbosity > 3) {
console.log(`Sassdoc data has changed -- writing new file.`);
}
BoltCache.set('sassdoc', data);
writeSassdocFile(self.options.dest, data);
} else {
if (config.verbosity > 3) {
console.log(`Sassdoc data hasn't changed...`);
}
}
} else {
// Otherwise write to disk + cache results the first time Sassdoc generates data
BoltCache.set('sassdoc', data);
writeSassdocFile(self.options.dest, data);
}
});
});
}
Expand Down

0 comments on commit 4b07507

Please sign in to comment.