Skip to content

Commit

Permalink
Added target path and formats
Browse files Browse the repository at this point in the history
  • Loading branch information
wojciechsura committed Mar 31, 2017
1 parent 93087b4 commit 0d2c023
Show file tree
Hide file tree
Showing 2 changed files with 126 additions and 37 deletions.
136 changes: 108 additions & 28 deletions extension.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ var CompileSassExtension = function() {

// Private functions ------------------------------------------------------

// Processes result of css file generation.
function handleResult(outputPath, result) {

if (result.status == 0) {
Expand Down Expand Up @@ -43,27 +44,93 @@ var CompileSassExtension = function() {
}
}

// Generates target path for scss/sass file basing on its path
// and easysass.targetDir setting. If the setting specifies
// relative path, current workspace folder is used as root.
function generateTargetPath(path) {

var configuration = vscode.workspace.getConfiguration('easysass');

var targetDir = pathModule.dirname(path);
var filename = pathModule.basename(path);
if (configuration.targetDir != undefined && configuration.targetDir.length > 0) {

if (pathModule.isAbsolute(configuration.targetDir)) {
targetDir = configuration.targetDir;
} else {
var folder = vscode.workspace.rootPath;
if (folder == undefined) {
throw "Path specified in easysass.targetDir is relative, but there is no open folder in VS Code!";
}

targetDir = pathModule.join(folder, configuration.targetDir);
}
}

return {
targetDir: targetDir,
filename: filename
};
}

// Compiles single scss/sass file.
function compileFile(path) {

outputChannel.clear();

var configuration = vscode.workspace.getConfiguration('easysass');

if (configuration.generateExpanded) {
compileSass(path, { style: compileSass.Sass.style.expanded }, function(result) {

handleResult(replaceExt(path, '.css'), result);
});
}
var outputPathData = generateTargetPath(path);

if (configuration.generateMinified) {
compileSass(path, { style: compileSass.Sass.style.compressed}, function(result) {
// Iterate through formats from configuration

handleResult(replaceExt(path, '.min.css'), result);
});
if (configuration.formats.length == 0) {
throw "No formats are specified. Define easysass.formats setting (or remove to use defaults)";
}

for (var i = 0; i < configuration.formats.length; i++) {

var format = configuration.formats[i];

// Evaluate style for sass generator
var style;
switch (format.format) {
case "nested":
style = compileSass.Sass.style.nested;
break;
case "compact":
style = compileSass.Sass.style.compact;
break;
case "expanded":
style = compileSass.Sass.style.expanded;
break;
case "compressed":
style = compileSass.Sass.style.compressed;
break;
default:
throw "Invalid format specified for easysass.formats[" + i + "]. Look at setting's hint for available formats.";
}

// Check target extension
if (format.extension == undefined || format.extension.length == 0)
throw "No extension specified for easysass.formats[" + i + "].";

var targetPath = pathModule.join(outputPathData.targetDir, replaceExt(outputPathData.filename, format.extension));

// Using closure to properly pass local variables to callback
(function(path_, targetPath_, style_) {

// Run the compilation process
compileSass(path_, { style: style_ }, function(result) {

handleResult(targetPath_, result);
});

})(path, targetPath, style);
}
}

// Checks, if the file matches the exclude regular expression
function checkExclude(filename) {

var configuration = vscode.workspace.getConfiguration('easysass');
Expand All @@ -76,38 +143,51 @@ var CompileSassExtension = function() {

OnSave: function (document) {

var configuration = vscode.workspace.getConfiguration('easysass');
var filename = pathModule.basename(document.fileName);
try {

if (configuration.compileAfterSave) {

if (document.fileName.toLowerCase().endsWith('.scss') ||
document.fileName.toLowerCase().endsWith('.sass')) {
var configuration = vscode.workspace.getConfiguration('easysass');
var filename = pathModule.basename(document.fileName);

if (!checkExclude(filename)) {
compileFile(document.fileName);
} else {
outputChannel.appendLine("File " + document.fileName + " is excluded from building to CSS. Check easysass.excludeRegex setting.");
if (configuration.compileAfterSave) {

if (document.fileName.toLowerCase().endsWith('.scss') ||
document.fileName.toLowerCase().endsWith('.sass')) {

if (!checkExclude(filename)) {
compileFile(document.fileName);
} else {
outputChannel.appendLine("File " + document.fileName + " is excluded from building to CSS. Check easysass.excludeRegex setting.");
}
}
}

}
catch (e) {
vscode.window.showErrorMessage('EasySass: could not generate CSS file: ' + e);
}
},
CompileAll: function() {

var configuration = vscode.workspace.getConfiguration('easysass');

vscode.workspace.findFiles("**/*.s[ac]ss").then(function(files) {
for (var i = 0; i < files.length; i++) {

var filename = pathModule.basename(files[i].fsPath);
if (checkExclude(filename)) {

outputChannel.appendLine("File " + filename + " is excluded from building to CSS. Check easysass.excludeRegex setting.");
continue;
try {
for (var i = 0; i < files.length; i++) {

var filename = pathModule.basename(files[i].fsPath);
if (checkExclude(filename)) {

outputChannel.appendLine("File " + filename + " is excluded from building to CSS. Check easysass.excludeRegex setting.");
continue;
}

compileFile(files[i].fsPath);
}

compileFile(files[i].fsPath);
}
catch (e) {
vscode.window.showErrorMessage('EasySass: could not generate CSS file: ' + e);
}
});
}
};
Expand Down
27 changes: 18 additions & 9 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,20 +33,29 @@
"default": true,
"description": "Automatically compile SASS/SCSS file after saving"
},
"easysass.generateExpanded": {
"type": "boolean",
"default": true,
"description": "Generate human-readable CSS from the SASS/SCSS file."
},
"easysass.generateMinified": {
"type": "boolean",
"default": true,
"description": "Generate minified CSS from the SASS/SCSS file."
"easysass.formats": {
"type": "array",
"default": [
{
"format": "expanded",
"extension": ".css"
},
{
"format": "compressed",
"extension": ".min.css"
}
],
"description": "Define format(s) for outputted css files. Use \"nested\", \"expanded\", \"compact\" or \"compressed\" as a format."
},
"easysass.excludeRegex": {
"type": "string",
"default": "",
"description": "Regular expression for filenames (excluding path) excluded from building. Leave empty to disable."
},
"easysass.targetDir": {
"type": "string",
"default": "",
"description": "Target directory for generated files. If relative, will be based on currently opened folder in VS Code."
}
}
},
Expand Down

0 comments on commit 0d2c023

Please sign in to comment.