Skip to content

Commit 19290f0

Browse files
author
Dick Smith
committed
Fix for path resolution
Currently a _*.scss file could not be found and imported unless it was in the app/ directory or it had been found before the importing file (and the importing file would need to reference it as just it’s name and couldn’t include actual path info which would then throw off the code completion of the IDE). 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.
1 parent d387a0b commit 19290f0

File tree

1 file changed

+35
-33
lines changed

1 file changed

+35
-33
lines changed

lib/converter.js

+35-33
Original file line numberDiff line numberDiff line change
@@ -6,58 +6,60 @@ var sass = require('node-sass');
66
var glob = require('glob');
77

88
function convert(logger, projectDir, options) {
9-
return new Promise(function (resolve, reject) {
10-
options = options || {};
11-
12-
var sassFilesPath = path.join(projectDir, 'app/**/*.scss');
9+
return new Promise(function (resolve, reject) {
10+
options = options || {};
11+
12+
var sassFilesPath = path.join(projectDir, 'app/**/*.scss');
1313
var sassImportPaths = [
14-
path.join(projectDir, 'app/'),
14+
path.join(projectDir, 'app/'),
1515
path.join(projectDir, 'node_modules/')
16-
];
16+
];
1717
//console.log("SASS Import Path", sassImportPaths);
18-
19-
var sassFiles = glob.sync(sassFilesPath, { follow: true }).filter(function(filePath){
20-
var path = filePath;
21-
var parts = path.split('/');
18+
19+
var sassFiles = glob.sync(sassFilesPath, {follow: true}).filter(function (filePath) {
20+
if (!(sassImportPaths.find(function (element) {return path.dirname(filePath) === element}))) {
21+
sassImportPaths.push(path.dirname(filePath));
22+
}
23+
var parts = filePath.split('/');
2224
var filename = parts[parts.length - 1];
23-
return path.indexOf("App_Resources") === -1 && filename.indexOf("_") !== 0;
24-
});
25-
26-
if(sassFiles.length === 0){
25+
return filePath.indexOf("App_Resources") === -1 && filename.indexOf("_") !== 0;
26+
});
27+
28+
if (sassFiles.length === 0) {
2729
//No sass files in project; skip parsing
2830
resolve();
29-
} else {
31+
} else {
3032
var i = 0;
31-
var loopSassFilesAsync = function(sassFiles){
32-
parseSass(sassFiles[i], sassImportPaths, function(e){
33-
if(e !== undefined){
33+
var loopSassFilesAsync = function (sassFiles) {
34+
parseSass(sassFiles[i], sassImportPaths, function (e) {
35+
if (e !== undefined) {
3436
//Error in the LESS parser; Reject promise
35-
reject(Error(sassFiles[i] + ' SASS CSS pre-processing failed. Error: ' + e));
37+
reject(Error(sassFiles[i] + ' SASS CSS pre-processing failed. Error: ' + e));
3638
}
37-
39+
3840
i++; //Increment loop counter
39-
40-
if(i < sassFiles.length){
41+
42+
if (i < sassFiles.length) {
4143
loopSassFilesAsync(sassFiles);
4244
} else {
4345
//All files have been processed; Resolve promise
4446
resolve();
4547
}
4648
});
4749
}
48-
50+
4951
loopSassFilesAsync(sassFiles);
5052
}
51-
});
53+
});
5254
}
5355

54-
function parseSass(filePath, importPaths, callback){
55-
var sassFileContent = fs.readFileSync(filePath, { encoding: 'utf8'});
56+
function parseSass(filePath, importPaths, callback) {
57+
var sassFileContent = fs.readFileSync(filePath, {encoding: 'utf8'});
5658
var cssFilePath = filePath.replace('.scss', '.css');
5759

58-
if(sassFileContent.trim().length === 0) {
60+
if (sassFileContent.trim().length === 0) {
5961
// No SASS content write an empty file
60-
fs.writeFile(cssFilePath, '', 'utf8', function(){
62+
fs.writeFile(cssFilePath, '', 'utf8', function () {
6163
callback();
6264
});
6365
return;
@@ -69,18 +71,18 @@ function parseSass(filePath, importPaths, callback){
6971
outFile: cssFilePath,
7072
outputStyle: 'compressed'
7173
}, function (e, output) {
72-
if(e) {
74+
if (e) {
7375
//Callback with error
7476
callback(e);
75-
}
76-
77-
if(output && output.css){
77+
}
78+
79+
if (output && output.css) {
7880
output = output.css;
7981
} else {
8082
output = '';
8183
}
8284

83-
fs.writeFile(cssFilePath, output, 'utf8', function(){
85+
fs.writeFile(cssFilePath, output, 'utf8', function () {
8486
//File done writing
8587
callback();
8688
});

0 commit comments

Comments
 (0)