👾 A secure and performant utility to extract sass variables (and use their values from JavaScript).
npm i sass-vars-extractor
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)
})
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)
})
-
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.