UNPKG

sass-all-variable-loader

Version:

Loads sass files and extracts all variable declarations including from the imported sass files.

71 lines (70 loc) 2.43 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); var camelCase = require("camelcase"); var fs = require("fs"); var path = require("path"); // @ts-ignore var sassExtract = require("sass-extract"); var nodeModules = path.resolve(process.cwd(), 'node_modules'); var scssExtension = '.scss'; var importer = function (url, prev) { var file; if (url.match(/^~/)) { file = path.resolve(nodeModules, url.replace(/^[~\/\\]+/, '')); } else { file = path.resolve(path.dirname(prev), url); } if (file.slice(-scssExtension.length) !== scssExtension) { file += scssExtension; } var candidates = [ file, path.resolve(path.dirname(file), '_' + path.basename(file)), ]; file = candidates.find(function (fn) { return fs.existsSync(fn); }); return { file: file }; }; var getVarValue = function (variable, camel) { switch (variable.type) { case 'SassNumber': return "" + variable.value + variable.unit; case 'SassNull': return null; case 'SassColor': var color = variable.value; return color.a !== 1 ? "rgba(" + color.r + ", " + color.g + ", " + color.b + ", " + color.a + ")" : color.hex; case 'SassList': return variable.value .map(function (o) { return getVarValue(o, camel); }) .join(variable.separator); case 'SassMap': return toValues(variable.value, camel); case 'SassString': case 'SassBoolean': default: return variable.value; } }; var toValues = function (variables, camel) { if (camel === void 0) { camel = false; } return Object.keys(variables) .reduce(function (result, k) { var name = camel ? camelCase(k.replace(/^\$/, '')) : k; result[name] = getVarValue(variables[k], camel); return result; }, {}); }; exports.extractVariables = function (file, _a) { var _b = _a === void 0 ? {} : _a, camel = _b.camelCase, _c = _b.includePaths, includePaths = _c === void 0 ? [] : _c; var rendered = sassExtract.renderSync({ file: file, includePaths: includePaths, importer: importer, }); return toValues(rendered.vars.global, camel); };