gulp-scss
Version:
Gulp plugin for sass stylesheet compiler by standard approach
106 lines (91 loc) • 3.58 kB
JavaScript
(function() {
var applySourceMap, compile, dargs, extend, gutil, inject, mapConverter, path, q, testing, through;
inject = require("node-kissdi").inject;
dargs = require("dargs");
through = require("through2");
gutil = require("gulp-util");
extend = require("extend");
path = require("path");
q = require("q");
mapConverter = require("convert-source-map");
testing = process.env.testing;
applySourceMap = require("vinyl-sourcemaps-apply");
compile = inject([
"exec", "mkdirp", "fs", function(exec, mkdirp, fs) {
return function(opts) {
return function(file, enc, cb) {
var options, passedArgs, tmpDir;
if (file.isNull()) {
return cb(null, file);
}
options = {
"bundleExec": false,
"sourcemap": "auto",
"tmpPath": ".gulp-scss-cache"
};
options = extend(options, opts);
passedArgs = dargs(options, {
"excludes": ["bundleExec", "tmpPath"]
});
tmpDir = path.join(file.cwd, options.tmpPath, path.relative(file.cwd, file.base), path.dirname(file.relative));
return q.nfcall(mkdirp, tmpDir).then(function() {
var command, defer, proc;
defer = q.defer();
command = [];
if (options.bundleExec) {
command = command.concat("bundle", "exec");
}
command.push("scss");
command = command.concat(passedArgs, file.path.replace(/\ /g, "\\ "), path.join(tmpDir, gutil.replaceExtension(path.basename(file.relative), ".css")).replace(/\ /g, "\\ "));
proc = exec(command.splice(0, 1)[0], command, {
"stdio": "inherit",
"shell": true
});
proc.on("error", defer.reject);
proc.on("close", function(code) {
if (code === 0) {
return defer.resolve();
} else {
return defer.reject("The command exited with code:" + code);
}
});
return defer.promise;
}).then(function() {
file.path = gutil.replaceExtension(file.path, ".css");
return q.nfcall(fs.readFile, path.join(tmpDir, gutil.replaceExtension(path.basename(file.relative), ".css")));
}).then(function(contents) {
var sourcemap;
contents = contents.toString("utf8");
sourcemap = mapConverter.fromMapFileSource(contents, tmpDir);
if (sourcemap) {
sourcemap = sourcemap.sourcemap;
if (options.sourcemap === "auto") {
sourcemap.sources = sourcemap.sources.map(function(file) {
return path.relative(process.cwd(), path.resolve(tmpDir, file));
});
}
applySourceMap(file, sourcemap);
}
file.contents = new Buffer(mapConverter.removeMapFileComments(contents).trim());
return cb(null, file);
})["catch"](function(e) {
var error;
error = new gutil.PluginError("gulp-scss", "Compilation failed.: " + e + "\n\nStack Trace:\n" + e.stack);
cb(error);
throw error;
});
};
};
}
], {
"exec": require("child_process").spawn,
"mkdirp": require("mkdirp"),
"fs": require("fs")
});
module.exports = function(options) {
return through.obj(compile.invoke()(options));
};
if (testing) {
module.exports.__compile__ = compile;
}
}).call(this);