From dd15fda0f65c865d6d4128fd4704b5a4b74509a8 Mon Sep 17 00:00:00 2001 From: Xavier Lacot Date: Wed, 27 May 2015 00:39:15 +0200 Subject: [PATCH] added an option to discard importing the stss rules present in an imported file (basically, it only imports variables) --- lib/renderer/stss2scss.js | 30 +++++++++++++++++++----------- stss.js | 20 ++++++++++++-------- 2 files changed, 31 insertions(+), 19 deletions(-) diff --git a/lib/renderer/stss2scss.js b/lib/renderer/stss2scss.js index e90232d..cad595a 100644 --- a/lib/renderer/stss2scss.js +++ b/lib/renderer/stss2scss.js @@ -17,9 +17,9 @@ var reNewline = /\n|\r\n|\r|\f/, /** * Include all @imported STSS files. - * + * * Replaces all `@import [, ]` 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 @@ -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; @@ -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) { @@ -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); @@ -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 @@ -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 @@ -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 = { @@ -236,4 +244,4 @@ function getLineInfo(text, statement) { } return info; -} \ No newline at end of file +} diff --git a/stss.js b/stss.js index 1676471..a4d8948 100644 --- a/stss.js +++ b/stss.js @@ -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) { @@ -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); @@ -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 @@ -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); @@ -140,7 +142,7 @@ 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 @@ -148,6 +150,7 @@ function parseOptions(options) { * @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() {}, @@ -195,7 +198,7 @@ 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 @@ -203,6 +206,7 @@ STSS.prototype.render = function(options) { * @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() {}, @@ -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'}); @@ -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