Skip to content
This repository has been archived by the owner on Feb 16, 2020. It is now read-only.

Commit

Permalink
Compiling sass
Browse files Browse the repository at this point in the history
  • Loading branch information
stevezhu committed Sep 2, 2014
1 parent c602b57 commit 0af63d0
Showing 1 changed file with 63 additions and 0 deletions.
63 changes: 63 additions & 0 deletions plugin/compile-sass.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
var _ = Npm.require('lodash'),
path = Npm.require('path'),
fs = Npm.require('fs'),
Future = Npm.require('fibers/future'),
sass = Npm.require('node-sass');

var readJSON = function(jsonPath) {
var json = fs.readFileSync(jsonPath);
try {
return JSON.parse(json);
} catch (err) {
console.error("Failed to parse json file: " + jsonPath, err);
return {};
}
};

var sourceHandler = function(compileStep) {
var fullInputPath = compileStep._fullInputPath;
// return if partial
if (path.basename(fullInputPath)[0] === '_') {
return;
}

// OPTIONS ========================================

var optionsFile = path.join(process.cwd(), 'sass.json');
var options = fs.existsSync(optionsFile) ? readJSON(optionsFile) : {};
options = _.extend(options, {
sourceComments: 'none',
includePaths: [],
stats: {}
});
options.file = fullInputPath;
if (!_.isArray(options.includePaths)) {
options.includePaths = [options.includePaths];
}
options.includePaths.push(path.dirname(fullInputPath));

// COMPILE ========================================

var css;
try {
css = sass.renderSync(options);
} catch (err) {
err = err.replace(fullInputPath + ':', '').split(':');
var errMessage = err[1].trim() + ': ' + err[2].trim(),
errLine = err[0];
compileStep.error({
message: 'Sass compiler ' + errMessage,
sourcePath: compileStep.inputPath,
line: errLine
});
return;
}

compileStep.addStylesheet({
path: compileStep.inputPath + ".css",
data: css
});
};

Plugin.registerSourceHandler("scss", {archMatching: 'web'}, sourceHandler);
Plugin.registerSourceHandler("sass", {archMatching: 'web'}, sourceHandler);

0 comments on commit 0af63d0

Please sign in to comment.