Skip to content

Commit

Permalink
fix: custom sass hook
Browse files Browse the repository at this point in the history
  • Loading branch information
NathanWalker committed Aug 3, 2016
1 parent 6cbc4ad commit 69166e0
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
*.css
*.js
!hooks/before-prepare/*.js
*.map
platforms
node_modules
Expand Down
78 changes: 78 additions & 0 deletions hooks/before-prepare/nativescript-dev-sass.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
var fs = require('fs');
var path = require('path');
var sass = require('node-sass');
var glob = require('glob');

function convert(logger, projectDir, options) {
return new Promise(function (resolve, reject) {
options = options || {};

// Customized by Nathan Walker
// Allows normal SASS imports to work as expected
var sassFilesPath = path.join(projectDir, 'app/*.scss');
var sassImportPath = path.join(projectDir, 'app/');
console.log("SASS Import Path", sassImportPath);

var sassFiles = glob.sync(sassFilesPath).filter(function(fileName){
return fileName.indexOf("App_Resources") === -1;
});

if(sassFiles.length === 0){
//No sass files in project; skip parsing
resolve();
} else {
var i = 0;
var loopSassFilesAsync = function(sassFiles){
parseSass(sassFiles[i], sassImportPath, function(e){
if(e !== undefined){
//Error in the LESS parser; Reject promise
reject(Error(sassFiles[i] + ' SASS CSS pre-processing failed. Error: ' + e));
}

i++; //Increment loop counter

if(i < sassFiles.length){
loopSassFilesAsync(sassFiles);
} else {
//All files have been processed; Resolve promise
resolve();
}
});
}

loopSassFilesAsync(sassFiles);
}
});
}

function parseSass(filePath, importPath, callback){
var sassFileContent = fs.readFileSync(filePath, { encoding: 'utf8'});
var cssFilePath = filePath.replace('.scss', '.css');

sass.render({
data: sassFileContent,
includePaths: [importPath],
outFile: cssFilePath,
outputStyle: 'compressed'
}, function (e, output) {
if(e) {
//Callback with error
callback(e);
}

if(output === null){
//No CSS content in converted scss file; No need to write file
callback();
} else {
fs.writeFile(cssFilePath, output.css, 'utf8', function(){
//File done writing
callback();
});
}
});
}

module.exports = function ($logger, $projectData, $usbLiveSyncService) {
return convert($logger, $projectData.projectDir);
}

1 change: 1 addition & 0 deletions hooks/before-prepare/nativescript-dev-typescript.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = require("nativescript-dev-typescript/lib/before-prepare.js");

0 comments on commit 69166e0

Please sign in to comment.