From 19290f0eb5baaa2fc84b6d7f76ab5ca6b4e35d4c Mon Sep 17 00:00:00 2001 From: Dick Smith Date: Tue, 14 Mar 2017 15:38:20 -0400 Subject: [PATCH] =?UTF-8?q?Fix=20for=20path=20resolution=20Currently=20a?= =?UTF-8?q?=20=5F*.scss=20file=20could=20not=20be=20found=20and=20imported?= =?UTF-8?q?=20unless=20it=20was=20in=20the=20app/=20directory=20or=20it=20?= =?UTF-8?q?had=20been=20found=20before=20the=20importing=20file=20(and=20t?= =?UTF-8?q?he=20importing=20file=20would=20need=20to=20reference=20it=20as?= =?UTF-8?q?=20just=20it=E2=80=99s=20name=20and=20couldn=E2=80=99t=20includ?= =?UTF-8?q?e=20actual=20path=20info=20which=20would=20then=20throw=20off?= =?UTF-8?q?=20the=20code=20completion=20of=20the=20IDE).?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This change adds to the sassImportPaths as it globs the sassFiles if that files path is not yet present in the sassImportPaths. This allows for a more flexible file structure. --- lib/converter.js | 68 +++++++++++++++++++++++++----------------------- 1 file changed, 35 insertions(+), 33 deletions(-) diff --git a/lib/converter.js b/lib/converter.js index dcde277..ce0c21f 100644 --- a/lib/converter.js +++ b/lib/converter.js @@ -6,38 +6,40 @@ var sass = require('node-sass'); var glob = require('glob'); function convert(logger, projectDir, options) { - return new Promise(function (resolve, reject) { - options = options || {}; - - var sassFilesPath = path.join(projectDir, 'app/**/*.scss'); + return new Promise(function (resolve, reject) { + options = options || {}; + + var sassFilesPath = path.join(projectDir, 'app/**/*.scss'); var sassImportPaths = [ - path.join(projectDir, 'app/'), + path.join(projectDir, 'app/'), path.join(projectDir, 'node_modules/') - ]; + ]; //console.log("SASS Import Path", sassImportPaths); - - var sassFiles = glob.sync(sassFilesPath, { follow: true }).filter(function(filePath){ - var path = filePath; - var parts = path.split('/'); + + var sassFiles = glob.sync(sassFilesPath, {follow: true}).filter(function (filePath) { + if (!(sassImportPaths.find(function (element) {return path.dirname(filePath) === element}))) { + sassImportPaths.push(path.dirname(filePath)); + } + var parts = filePath.split('/'); var filename = parts[parts.length - 1]; - return path.indexOf("App_Resources") === -1 && filename.indexOf("_") !== 0; - }); - - if(sassFiles.length === 0){ + return filePath.indexOf("App_Resources") === -1 && filename.indexOf("_") !== 0; + }); + + if (sassFiles.length === 0) { //No sass files in project; skip parsing resolve(); - } else { + } else { var i = 0; - var loopSassFilesAsync = function(sassFiles){ - parseSass(sassFiles[i], sassImportPaths, function(e){ - if(e !== undefined){ + var loopSassFilesAsync = function (sassFiles) { + parseSass(sassFiles[i], sassImportPaths, function (e) { + if (e !== undefined) { //Error in the LESS parser; Reject promise - reject(Error(sassFiles[i] + ' SASS CSS pre-processing failed. Error: ' + e)); + reject(Error(sassFiles[i] + ' SASS CSS pre-processing failed. Error: ' + e)); } - + i++; //Increment loop counter - - if(i < sassFiles.length){ + + if (i < sassFiles.length) { loopSassFilesAsync(sassFiles); } else { //All files have been processed; Resolve promise @@ -45,19 +47,19 @@ function convert(logger, projectDir, options) { } }); } - + loopSassFilesAsync(sassFiles); } - }); + }); } -function parseSass(filePath, importPaths, callback){ - var sassFileContent = fs.readFileSync(filePath, { encoding: 'utf8'}); +function parseSass(filePath, importPaths, callback) { + var sassFileContent = fs.readFileSync(filePath, {encoding: 'utf8'}); var cssFilePath = filePath.replace('.scss', '.css'); - if(sassFileContent.trim().length === 0) { + if (sassFileContent.trim().length === 0) { // No SASS content write an empty file - fs.writeFile(cssFilePath, '', 'utf8', function(){ + fs.writeFile(cssFilePath, '', 'utf8', function () { callback(); }); return; @@ -69,18 +71,18 @@ function parseSass(filePath, importPaths, callback){ outFile: cssFilePath, outputStyle: 'compressed' }, function (e, output) { - if(e) { + if (e) { //Callback with error callback(e); - } - - if(output && output.css){ + } + + if (output && output.css) { output = output.css; } else { output = ''; } - fs.writeFile(cssFilePath, output, 'utf8', function(){ + fs.writeFile(cssFilePath, output, 'utf8', function () { //File done writing callback(); });