Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added an option to discard importing stss rules from imports #27

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 19 additions & 11 deletions lib/renderer/stss2scss.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ var reNewline = /\n|\r\n|\r|\f/,

/**
* Include all @imported STSS files.
*
*
* Replaces all `@import <file.stss> [, <file1.stss>]` statements by the content of those files.
*
*
* @param {String} stss STSS structured markup
* @param {Object} options Dictionary containing additional instructions
* @param {String} [options.file] Filename of the STSS file currently being parsed
Expand Down Expand Up @@ -64,14 +64,22 @@ function includeImports(stss, options) {
if (!file) {
throw parseError('STSS file to import not found or unreadable: "' + filename + '"', match, stss, options);
}

content = includeImports(fs.readFileSync(file, 'utf-8'), {
includePaths: importPaths,
file: filename
});

imported += content + "\n";

if (options.skipImportedRules && options.skipImportedRules === true) {
content.split(/\r?\n/).forEach(function(line) {
if (/^\s*\$(.*)?$/.test(line)) {
imported += line + "\n";
}
});
} else {
imported += content + "\n";
}

return content;
});
return imported;
Expand Down Expand Up @@ -100,7 +108,7 @@ module.exports = function (stss, options) {
} catch (err) {
return err;
}

// Replace hyphenated terms with camelCased variants
scss = stss.replace(reDeclCSS, function(match, key, value) {
if (key.search(/-/) !== -1) {
Expand Down Expand Up @@ -152,7 +160,7 @@ module.exports = function (stss, options) {
for (i = 0; i < ln; i++) {
value = values[i].trim();
isObject = /^\{[\s\S]+\}$/.test(value);

if (isObject) {
tempName = 'stss-array' + arrayIdx + '-obj' + i;
arrayDecl.push('-' + tempName + value);
Expand All @@ -179,7 +187,7 @@ module.exports = function (stss, options) {

/**
* Parse the error string into a new Error object.
*
*
* @param {String} msg Error message (as generated by node-sass)
* @param {String} stss SCSS source (that potentially contains the error if it is source-based)
* @param {Object} options Dictionary containing additional instructions
Expand Down Expand Up @@ -214,7 +222,7 @@ function parseError(msg, statement, stss, options) {

/**
* Get the line and line number that contains the provided statement.
*
*
* @param {String} text Source text
* @param {String} statement The statment
* @return {Object} Line information
Expand All @@ -225,7 +233,7 @@ function getLineInfo(text, statement) {
lines = text.split(/\r\n|\n|\r|\f/),
ln = lines.length,
i = -1;

while (!info && ++i < ln) {
if (reStmnt.test(lines[i])) {
info = {
Expand All @@ -236,4 +244,4 @@ function getLineInfo(text, statement) {
}

return info;
}
}
20 changes: 12 additions & 8 deletions stss.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ util.inherits(STSS, EventEmitter);
* @param {Function} options.success Callback that will be called upon successful conversion
* @param {Function} [options.error] Callback that will be called if conversion fails
* @param {String} [options.shFile] JSON file that contains additional shorthand notation to use during conversion
* @param {Boolean} [options.skipImportedRules] Whether or not stss rules from imported files should be included or not
* @param {Function} log Function that logs the current state of the process
*/
function process(stss, options, log) {
Expand Down Expand Up @@ -76,7 +77,7 @@ function process(stss, options, log) {
callback(null, tss);
},
], function(err, tss) {
if (err) {
if (err) {
options.error && options.error(err);
} else {
options.success(tss);
Expand All @@ -95,6 +96,7 @@ function process(stss, options, log) {
* @param {Function} options.success Callback that will be called upon successful conversion
* @param {Function} [options.error] Callback that will be called if conversion fails
* @param {String} [options.shFile] JSON file that contains additional shorthand notation to use during conversion
* @param {Boolean} [options.skipImportedRules] Whether or not stss rules from imported files should be included or not
* @param {Function} log Function that logs the current state of the process
* @return {String} TSS structured markup
* @throws {Error} If an error occured during any of the four rendering passes
Expand All @@ -103,15 +105,15 @@ function processSync(stss, options, log) {
var scss = stss2scss(stss, options);
if (scss instanceof Error) { throw scss; }
log('scss', scss);

var css = scss2css(scss, options);
if (css instanceof Error) { throw css; }
log('css', css);

var json = css2json(css, options);
if (json instanceof Error) { throw json; }
log('json', JSON.stringify(json, null, 2));

var tss = json2tss(json, options);
if (tss instanceof Error) { throw tss; }
log('tss', tss);
Expand Down Expand Up @@ -140,14 +142,15 @@ function parseOptions(options) {
* Likewise, either options.outFile or options.success should be defined, or both.
*
* This function executes asynchronously.
*
*
* @param {Object} options Dictionary containing instructions
* @param {String} [options.data] STSS data, required if options.file is not passed
* @param {String} [options.file] STSS file that is to be converted
* @param {String} [options.outFile] File that is created/overwritten with the generated TSS output
* @param {Function} [options.success] Callback that will be called upon successful conversion
* @param {Function} [options.error] Callback that will be called if conversion fails
* @param {String} [options.shFile] JSON file that contains additional shorthand notation to use during conversion
* @param {Boolean} [options.skipImportedRules] Whether or not stss rules from imported files should be included or not
*/
STSS.prototype.render = function(options) {
var success = options.success || function() {},
Expand Down Expand Up @@ -195,14 +198,15 @@ STSS.prototype.render = function(options) {
* Likewise, either options.outFile or options.success should be defined, or both.
*
* This function executes synchronously.
*
*
* @param {Object} options Dictionary containing instructions
* @param {String} [options.data] STSS data, required if options.file is not passed
* @param {String} [options.file] STSS file that is to be converted
* @param {String} [options.outFile] File that is created/overwritten with the generated TSS output
* @param {Function} [options.success] Callback that will be called upon successful conversion
* @param {Function} [options.error] Callback that will be called if conversion fails
* @param {String} [options.shFile] JSON file that contains additional shorthands to use during conversion
* @param {Boolean} [options.skipImportedRules] Whether or not stss rules from imported files should be included or not
*/
STSS.prototype.renderSync = function(options) {
var success = options.success || function() {},
Expand Down Expand Up @@ -240,7 +244,7 @@ STSS.prototype.renderSync = function(options) {
} else if (options.file) {
// Save the basename for potential use in error messages
options.filename = path.basename(options.file);

if (fs.existsSync(options.file)) {
try {
data = fs.readFileSync(options.file, {encoding: 'utf8'});
Expand All @@ -264,7 +268,7 @@ STSS.prototype.renderSync = function(options) {

/**
* Log the conversion step that was just completed.
*
*
* @param {String} conversion Type of conversion just completed
* @param {String} output Output of the phase that was just finished
* @fires conversionStep
Expand Down