gulp-shopify-sass
Advanced tools
Comparing version 0.3.3 to 0.4.0
18
index.js
@@ -88,15 +88,11 @@ 'use strict'; | ||
// TODO: add includePaths feature in options and relavent processer | ||
// Ensure file's parent directory in the include path | ||
// if (opts.includePaths) { | ||
// if (typeof opts.includePaths === 'string') { | ||
// opts.includePaths = [opts.includePaths]; | ||
// } | ||
// } else { | ||
// opts.includePaths = []; | ||
// } | ||
if (opts.includePaths) { | ||
if (typeof opts.includePaths === 'string') { | ||
opts.includePaths = [opts.includePaths]; | ||
} | ||
} else { | ||
opts.includePaths = []; | ||
} | ||
// opts.includePaths.unshift(path.dirname(file.path)); | ||
// TDDO: enable sync option once async render is done. Only support renderSync for now | ||
@@ -103,0 +99,0 @@ // if (sync === true) { |
@@ -12,2 +12,4 @@ /* | ||
'use strict'; | ||
var fs = require('fs'); | ||
@@ -103,12 +105,12 @@ var path = require('path'); | ||
// @param: dir full path and name | ||
// @param: dirs dirs where to search the file | ||
// @param: file @import file to search | ||
// @return: string | boolean | ||
function analyseDir (dir) { | ||
function analyseDirs (dirs, file) { | ||
// possible extension array | ||
const dirReg = new RegExp(/(.+\/)(.+)$/, 'g'); | ||
const filenameReg = new RegExp(/^(\_)?(?:(.*(?=.scss))(.*)|(.*))$/, 'i'); | ||
const dirReg = new RegExp(/(.*\/)?(.+)$/g); | ||
const filenameReg = new RegExp(/^(\_)?(?:(.*(?=.scss))(.*)|(.*))$/i); | ||
// split whole path into path the filename | ||
let pathAndName = dirReg.exec(dir); | ||
let pathAndName = dirReg.exec(file); | ||
const filepath = pathAndName[1] || ''; | ||
@@ -127,4 +129,4 @@ let filename = pathAndName[2] || ''; | ||
let prefix = filenameFrags[1] ? [filenameFrags[1]] : ['', '_']; | ||
let suffix = filenameFrags[3] ? [filenameFrags[3]] : ['.scss', '.scss.liquid']; | ||
let prefixs = filenameFrags[1] ? [filenameFrags[1]] : ['', '_']; | ||
let suffixs = filenameFrags[3] ? [filenameFrags[3]] : ['.scss', '.scss.liquid']; | ||
@@ -138,11 +140,14 @@ // remove original name, all index will reduce 1 | ||
// check all possible combimations | ||
for (let i in prefix) { | ||
for (let j in suffix) { | ||
filenameFrags[0] = prefix[i]; | ||
filenameFrags[2] = suffix[j]; | ||
filename = filenameFrags.join(''); | ||
pathAndName = path.join(filepath, filename); | ||
let dir, fullPath; | ||
for (let i_dir in dirs) { | ||
for (let i_prefix in prefixs) { | ||
for (let i_suffix in suffixs) { | ||
filenameFrags[0] = prefixs[i_prefix]; | ||
filenameFrags[2] = suffixs[i_suffix]; | ||
filename = filenameFrags.join(''); | ||
fullPath = path.join(dirs[i_dir], filepath, filename); | ||
if (checkFileExist(pathAndName)) { | ||
return pathAndName; | ||
if (checkFileExist(fullPath)) { | ||
return fullPath; | ||
} | ||
} | ||
@@ -152,3 +157,3 @@ } | ||
return new Error('File to import: "' + dir + '" not found.'); | ||
return new Error('Unable to import "' + file + '": file not found.'); | ||
} | ||
@@ -161,6 +166,10 @@ | ||
var contents = options.data; | ||
var dirname = path.dirname(options.file); | ||
var imports = {}; | ||
var match; | ||
var dirs = options.includePaths.slice(0); | ||
// Add the current file path to where to search | ||
if (typeof options.file !== 'undefined') | ||
dirs.unshift(path.dirname(options.file)); | ||
while(match = rex.exec(contents)) { | ||
@@ -170,6 +179,5 @@ | ||
// [2] double quotes | ||
var importFile = path.join(dirname, (match[1] || match[2])); | ||
var importFile = match[1] || match[2]; | ||
const fileExistCheck = analyseDirs(dirs, importFile); | ||
const fileExistCheck = analyseDir(importFile); | ||
// if file exists, replace it | ||
@@ -188,6 +196,10 @@ if(fileExistCheck) { | ||
data: file.contents.toString(), | ||
file: file.path | ||
file: file.path, | ||
includePaths: dirs | ||
}); | ||
contents = contents.replace(new RegExp(imp, 'g'), result); | ||
contents = contents.replace(new RegExp(imp, 'g'), function () { | ||
// http://stackoverflow.com/a/28103073 | ||
return result | ||
}); | ||
} | ||
@@ -263,2 +275,2 @@ | ||
// throw assign(new Error(), JSON.parse(result.error)); | ||
}; | ||
}; |
{ | ||
"name": "gulp-shopify-sass", | ||
"version": "0.3.3", | ||
"version": "0.4.0", | ||
"description": "Concatenate Sass files defined by the @import order", | ||
@@ -5,0 +5,0 @@ "main": "./index.js", |
22333
507