# ![node-sass-variables logo](https://codeberg.org/stefan25376/node-sass-variables/raw/branch/main/assets/node-sass-variables-128.png) 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!

[![node-sass-variables on npmjs.com](https://img.shields.io/npm/v/node-sass-variables?logo=npm&logoColor=white)](https://www.npmjs.com/package/node-sass-variables)
[![node-sass-variables on Codeberg](https://img.shields.io/badge/dynamic/json?color=2185D0&logo=codeberg&logoColor=white&label=codeberg&query=%24.version&url=https%3A%2F%2Fcodeberg.org%2Fapi%2Fv1%2Frepos%2Fstefan25376%2Fnode-sass-variables%2Fraw%2Fpackage.json)](https://codeberg.org/stefan25376/node-sass-variables)
[![node-sass-variables on jsDelivr](https://data.jsdelivr.com/v1/package/npm/node-sass-variables/badge?style=rounded)](https://www.jsdelivr.com/package/npm/node-sass-variables)

---

## Requirements

- node.js >= 22.12 (the `sass` package requires >= 20.19, and the ESM-only `color` package is loaded via `require(esm)`)

## Installation

```sh
npm install node-sass-variables
```

## Synchronous use

```js
const { getSassVariablesStringSync, getSassVariablesSync } = require('node-sass-variables');

console.log(getSassVariablesStringSync('$foo: 42;'));

console.log(getSassVariablesSync('path/to/my/scss-file.scss'));
```

## Asynchronous use

```js
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:

```ts
import { getSassVariablesSync, SassVariables } from 'node-sass-variables';

const vars: SassVariables = getSassVariablesSync('path/to/my/scss-file.scss');
```

## Options

```js
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:

```sh
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](https://en.wikipedia.org/wiki/ISC_license) for more information.
