What is sass-embedded?
The sass-embedded npm package provides a way to compile Sass (Syntactically Awesome Style Sheets) using the Embedded Sass protocol. This allows for faster compilation and better integration with JavaScript environments.
What are sass-embedded's main functionalities?
Compile Sass to CSS
This feature allows you to compile Sass code into CSS. The example demonstrates how to compile a simple Sass string into CSS using the `compileString` method.
const sass = require('sass-embedded');
const result = sass.compileString(`
$primary-color: #333;
body {
color: $primary-color;
}
`);
console.log(result.css);
Compile Sass from a file
This feature allows you to compile Sass from a file. The example shows how to read a Sass file, compile it, and then write the resulting CSS to an output file.
const sass = require('sass-embedded');
const fs = require('fs');
const result = sass.compile('path/to/your/file.scss');
fs.writeFileSync('path/to/output.css', result.css);
Custom Importers
This feature allows you to define custom importers to handle `@import` statements in Sass files. The example demonstrates how to create a custom importer that replaces the content of an import with a specific string.
const sass = require('sass-embedded');
const result = sass.compileString(`
@import 'custom';
body {
color: red;
}
`, {
importers: [
{
findFileUrl(url) {
if (url === 'custom') {
return new URL('data:text/plain,body { color: blue; }');
}
return null;
}
}
]
});
console.log(result.css);
Other packages similar to sass-embedded
node-sass
node-sass is a library that provides binding for Node.js to LibSass, the C version of the popular stylesheet preprocessor, Sass. It is similar to sass-embedded in that it allows you to compile Sass to CSS, but it uses a different underlying technology (LibSass vs. Embedded Sass). node-sass is known for its speed but can be more difficult to set up due to native dependencies.
sass
sass is the official JavaScript implementation of Sass, written in Dart and compiled to JavaScript. It is similar to sass-embedded in terms of functionality, providing a way to compile Sass to CSS. However, it does not use the Embedded Sass protocol and can be slower in some cases compared to sass-embedded.
Embedded Sass Host
This is a Node.js library that implements the host side of the Embedded Sass
protocol. It exposes a JS API for Sass that's backed by a native Dart
Sass executable. It's much faster than running Dart Sass compiled to
JavaScript while still providing a full JS API, with the ability to define
custom functions and importers.
Disclaimer: this is not an official Google product.