node-sass-variables
v0.0.6
Published
Exports variables from Sass/Scss-files for usage in node.js
Maintainers
Readme
node-sass-variables
Unlike many other approaches to access Sass/Scss variables in node.js, this approach tries to reach this goal with a minimum of code. The extraction is actually done with a neat, little trick, by first accessing all variables with PostCSS, then generating some extra Sass code to invoke a custom Sass function with the collected variables. In the custom Sass function the final parsed variables are made accessible. No parsing, no fiddling, no rocket science!
Requirements
- node.js >= 22.12 (the
sasspackage requires >= 20.19, and the ESM-onlycolorpackage is loaded viarequire(esm))
Installation
npm install node-sass-variablesSynchronous use
const { getSassVariablesStringSync, getSassVariablesSync } = require('node-sass-variables');
console.log(getSassVariablesStringSync('$foo: 42;'));
console.log(getSassVariablesSync('path/to/my/scss-file.scss'));Asynchronous use
const { getSassVariablesAsync, getSassVariablesStringAsync } = require('node-sass-variables');
getSassVariablesStringAsync('$foo: 42;').then(console.log);
getSassVariablesAsync('path/to/my/scss-file.scss').then(console.log);TypeScript
The package ships its own type definitions (dist/index.d.ts), so the functions above are fully typed in TypeScript projects as well:
import { getSassVariablesSync, SassVariables } from 'node-sass-variables';
const vars: SassVariables = getSassVariablesSync('path/to/my/scss-file.scss');Options
getSassVariablesAsync(filepath, postCssConfig, sassConfig, safeKeyWord)
getSassVariablesStringAsync(sass, postCssConfig, sassConfig, safeKeyWord)
getSassVariablesStringSync(sass, postCssConfig, sassConfig, safeKeyWord)
getSassVariablesSync(filepath, postCssConfig, sassConfig, safeKeyWord)sass (string, mandatory) or filepath (string, mandatory)
This is Sass/Scss content or the path to the Sass/Scss-file.
postCssConfig (object, optional, default = {})
This is some additional configuration for PostCSS if needed.
sassConfig (object, optional, default = {})
This is some additional configuration for Sass if needed.
safeKeyWord (string, optional, default = __nirvana__)
This is a keyword, which is used internally by this module to pass the variables to a custom Sass function. It is assumed highly unlikely that someone uses the default keyword in Sass as a variable name or function name. For the rare case that there might be a collision, the value can be overridden.
External files
Variables from files which are imported with @use or @import are useable, but not automatically exported, because only the variables declared in the Sass/Scss content or the filepath are exported.
This module tries its best to determine, where external files might be located and tries to import them. If this fails, it might be necessary to provide your own loadPaths or importers to the sassConfig.
Development
The source lives in src/index.ts. The build pipeline is:
npm run build # tsc: transpiles src/ to CommonJS + type definitions in dist/
npm run typecheck # tsc --noEmit over src and test
npm test # builds first, then runs the vitest suite (with coverage)The tests run against the built output in dist/, exactly like a consumer would use it. Besides the ported unit tests, test/behavior/snapshot.test.ts compares the output of a broad input battery against test/behavior/expected-snapshot.json, which was recorded with the legacy implementation (plain JS, dependencies as locked in the old package-lock.json). This guarantees that the TypeScript rewrite reproduces the legacy behavior exactly.
License
This software is brought to you with :heart: love :heart: from Dortmund and offered and distributed under the ISC license. See LICENSE.txt and Wikipedia for more information.
