danifornells / sass-vars-extractor

👾 A secure and performant utility to extract sass variables (and use their values from JavaScript).

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

sass-vars-extractor

JavaScript Style Guide npm version Build Status

👾 A secure and performant utility to extract sass variables (and use their values from JavaScript).

Installation

npm i sass-vars-extractor

API

extract(fileName, [includePaths])

fileName (string): main sass file to process

includePaths (array, default []): node-sass includePaths option

const SassVarsExtractor = require('sass-vars-extractor')

SassVarsExtractor.extract('main.scss')
  .then((extractedVars) => {
    console.log(extractedVars)
  }).catch((err) => {
    console.log(err)
  })

Examples

Extract all the sass variables and save to a JSON file

const SassVarsExtractor = require('sass-vars-extractor')
const jsonfile = require('jsonfile')
const src = 'styles/main.scss'
const dest = 'data/sass-vars.json'

SassVarsExtractor.extract(src)
  .then((extractedVars) => {
    jsonfile.writeFile(dest, extractedVars, {spaces: 2}, function (err) {
      if (err) console.error(err)
    })
  }).catch((err) => {
    console.log(sassError)
  })

How it does the job?

  • Creates a virtual main sass file that imports the entry point specified, the sass-json-export mixin, and some smart output to capture values.

  • By registering a node-sass importer function, evaluates each imported file content, and capture the potential var candidates using regular expressions.

  • Compiles the virtual sass file and parse the smart output to get the resolved vars and values.

About

👾 A secure and performant utility to extract sass variables (and use their values from JavaScript).


Languages

Language:JavaScript 97.1%Language:Shell 2.9%