From cfa17eaa5e9d51c54e3a8795aae513b3140cf7e2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Reimund=20J=C3=A4rnfors?= Date: Wed, 1 Feb 2023 13:42:14 +0100 Subject: [PATCH] Added support for @use rules. --- src/compile.js | 4 ++-- src/declaration.js | 2 +- src/declarationStore.js | 5 +++++ src/processor.js | 13 ++++++++++--- src/value.js | 8 ++++---- 5 files changed, 22 insertions(+), 10 deletions(-) diff --git a/src/compile.js b/src/compile.js index cb95c96..a795857 100644 --- a/src/compile.js +++ b/src/compile.js @@ -12,9 +12,9 @@ function unwrapValue(value) { } var Compile = { - fromString: function(value) { + fromString: function(value, useRules) { var wrappedValue = wrapValue(value); - var s = sass.renderSync({ data: wrappedValue }); + var s = sass.renderSync({ data: useRules.join(';\n') + ';\n' + wrappedValue }); var compiled = String(s.css); var minifiedCompiled = cssmin(compiled); return unwrapValue(minifiedCompiled); diff --git a/src/declaration.js b/src/declaration.js index 61888e1..d41b320 100644 --- a/src/declaration.js +++ b/src/declaration.js @@ -23,7 +23,7 @@ Declaration.prototype = { var replacedValue = declarationStore.replaceVariables(assignedValue); this.variable = new Variable(assignedVariable); - this.value = new Value(replacedValue); + this.value = new Value(replacedValue, declarationStore.useRules); this.global = hasGlobalFlag(replacedValue); declarationStore.addDeclaration(this); diff --git a/src/declarationStore.js b/src/declarationStore.js index 2376180..982977b 100644 --- a/src/declarationStore.js +++ b/src/declarationStore.js @@ -2,6 +2,7 @@ function DeclarationStore() { this.declarations = []; + this.useRules = []; } DeclarationStore.prototype = { @@ -9,6 +10,10 @@ DeclarationStore.prototype = { this.declarations.push(declaration); }, + addUseRule: function(useRule) { + this.useRules.push(useRule); + }, + replaceVariables: function(scssString) { var replacedString = scssString; diff --git a/src/processor.js b/src/processor.js index d50332f..565ffce 100644 --- a/src/processor.js +++ b/src/processor.js @@ -31,7 +31,8 @@ function makeObject(declarations, options) { function filterLines(line) { return EMPTY_LINES.every(function(lineValue) { - return line !== lineValue && line.slice(0, 2) !== COMMENT_DELIMETER && line.indexOf('@import') < 0; + return line !== lineValue && line.slice(0, 2) !== COMMENT_DELIMETER + && line.indexOf('@import') < 0 && line.indexOf('@use') < 0; }); } @@ -93,8 +94,14 @@ function declarationsFromString(path, declarationStore, options) { data = extractScope(data, options.scope); } - var lines = String(data).split(LINE_DELIMITER).map(normalizeLines).filter(filterLines); - return lines.map(function(line) { + var lines = String(data).split(LINE_DELIMITER).map(normalizeLines); + var useRules = lines.filter(function(line) { return line.match(/^@use .+/); }); + + useRules.forEach(function(useRule) { + declarationStore.addUseRule(useRule); + }); + + return lines.filter(filterLines).map(function(line) { return new Declaration(line, declarationStore); }); } diff --git a/src/value.js b/src/value.js index 666d944..1e266b7 100644 --- a/src/value.js +++ b/src/value.js @@ -7,14 +7,14 @@ function transforms(value) { return utilities.removeInlineComments(utilities.removeFlags(value)); } -function Value(scssString) { - this._parse(scssString); +function Value(scssString, useRules) { + this._parse(scssString, useRules); } Value.prototype = { - _parse: function(scssString) { + _parse: function(scssString, useRules) { var transformed = transforms(scssString); - var compiled = compile.fromString(transformed); + var compiled = compile.fromString(transformed, useRules); this.value = compiled.trim(); } };