2018-03-17 00:20:00 +01:00
|
|
|
---
|
|
|
|
title: Dart Sass
|
2019-03-04 23:03:41 +01:00
|
|
|
no_container: true
|
2018-10-24 00:06:39 +02:00
|
|
|
introduction: >
|
2018-03-17 00:20:00 +01:00
|
|
|
Dart Sass is the primary implementation of Sass, which means it gets new
|
|
|
|
features before any other implementation. It's fast, easy to install, and it
|
|
|
|
compiles to pure JavaScript which makes it easy to integrate into modern web
|
|
|
|
development workflows. Find out more or help out with its development on
|
2018-10-24 00:06:39 +02:00
|
|
|
[GitHub](https://github.com/sass/dart-sass).
|
|
|
|
---
|
2018-03-17 00:20:00 +01:00
|
|
|
|
2019-03-04 23:03:41 +01:00
|
|
|
.sl-l-grid.sl-l-grid--full.sl-l-large-grid--fit.sl-l-large-grid--gutters-large
|
|
|
|
.sl-l-grid__column
|
2018-03-17 00:20:00 +01:00
|
|
|
:markdown
|
2018-10-24 00:06:39 +02:00
|
|
|
## Command Line
|
2018-03-17 00:20:00 +01:00
|
|
|
|
|
|
|
Dart Sass's stand-alone command-line executable uses the blazing-fast Dart
|
|
|
|
VM to compile your stylesheets. To install Dart Sass on the command line,
|
2018-12-28 02:32:59 +01:00
|
|
|
check out the #{link_to "installation instructions", "/install"}. Once
|
2018-03-17 00:20:00 +01:00
|
|
|
you've got it running, you can use it compile files:
|
|
|
|
|
2019-03-05 02:46:32 +01:00
|
|
|
```sh
|
|
|
|
sass source/index.scss css/index.css
|
|
|
|
```
|
2018-03-17 00:20:00 +01:00
|
|
|
|
|
|
|
See `sass --help` for additional information on the command-line
|
|
|
|
interface.
|
|
|
|
|
2019-12-13 04:26:12 +01:00
|
|
|
### Dart Library
|
2018-03-17 00:20:00 +01:00
|
|
|
|
|
|
|
You can also use Dart Sass as a Dart library to get the speed of the Dart
|
|
|
|
VM plus the ability to define your own functions and importers. To add it
|
|
|
|
to an existing project:
|
|
|
|
|
2019-03-05 02:46:32 +01:00
|
|
|
1. [Install the Dart SDK][install]. Make sure its `bin` directory is [on
|
|
|
|
your `PATH`][path].
|
2018-03-17 00:20:00 +01:00
|
|
|
|
2019-03-05 02:46:32 +01:00
|
|
|
[install]: https://www.dartlang.org/install#automated-installation-and-updates
|
|
|
|
[path]: https://katiek2.github.io/path-doc/
|
2018-10-24 00:06:39 +02:00
|
|
|
|
2019-03-05 02:46:32 +01:00
|
|
|
2. Create a `pubspec.yaml` file like this:
|
2018-10-24 00:06:39 +02:00
|
|
|
|
2019-03-05 02:46:32 +01:00
|
|
|
```yaml
|
|
|
|
name: my_project
|
|
|
|
dev_dependencies:
|
|
|
|
sass: ^#{impl_version(:dart)}
|
|
|
|
```
|
2018-10-24 00:06:39 +02:00
|
|
|
|
2021-08-27 18:20:38 +02:00
|
|
|
3. Run `dart pub get`.
|
2018-10-24 00:06:39 +02:00
|
|
|
|
2019-03-05 02:46:32 +01:00
|
|
|
4. Create a `compile-sass.dart` file like this:
|
2018-10-24 00:06:39 +02:00
|
|
|
|
2019-03-05 02:46:32 +01:00
|
|
|
```dart
|
|
|
|
import 'dart:io';
|
|
|
|
import 'package:sass/sass.dart' as sass;
|
2018-03-17 00:20:00 +01:00
|
|
|
|
2019-03-05 02:46:32 +01:00
|
|
|
void main(List<String> arguments) {
|
2023-04-13 21:59:42 +02:00
|
|
|
var result = sass.compileToResult(arguments[0]);
|
|
|
|
new File(arguments[1]).writeAsStringSync(result.css);
|
2019-03-05 02:46:32 +01:00
|
|
|
}
|
|
|
|
```
|
2018-10-24 00:06:39 +02:00
|
|
|
|
2019-03-05 02:46:32 +01:00
|
|
|
5. You can now use this to compile files:
|
2018-10-24 00:06:39 +02:00
|
|
|
|
2019-03-05 02:46:32 +01:00
|
|
|
```sh
|
|
|
|
dart compile-sass.dart styles.scss styles.css
|
|
|
|
```
|
2018-10-24 00:06:39 +02:00
|
|
|
|
2019-03-05 02:46:32 +01:00
|
|
|
6. Learn more about [writing Dart code][dart] (it's easy!) and about
|
|
|
|
[Sass's Dart API][sass].
|
2018-03-17 00:20:00 +01:00
|
|
|
|
2019-03-05 02:46:32 +01:00
|
|
|
[dart]: https://www.dartlang.org/guides/language/language-tour
|
2021-08-27 18:20:38 +02:00
|
|
|
[sass]: https://pub.dev/documentation/sass/latest/sass/compileToResult.html
|
2018-03-17 00:20:00 +01:00
|
|
|
|
2019-03-04 23:03:41 +01:00
|
|
|
.sl-l-grid__column
|
2018-03-17 00:20:00 +01:00
|
|
|
:markdown
|
2018-10-24 00:06:39 +02:00
|
|
|
## JavaScript Library
|
2018-03-17 00:20:00 +01:00
|
|
|
|
|
|
|
Dart Sass is also distributed as the pure JavaScript [`sass`
|
|
|
|
package](https://npmjs.com/package/sass) on npm. The pure JS version is
|
|
|
|
slower than the stand-alone executable, but it's easy to integrate into
|
|
|
|
existing workflows and it allows you to define custom functions and
|
|
|
|
importers in JavaScript. You can add it to your project using
|
|
|
|
`npm install --save-dev sass` and `require()` it as a library:
|
|
|
|
|
2019-03-05 02:46:32 +01:00
|
|
|
```js
|
2023-04-13 21:59:42 +02:00
|
|
|
const sass = require('sass');
|
2018-03-17 00:20:00 +01:00
|
|
|
|
2023-04-13 21:59:42 +02:00
|
|
|
const result = sass.compile("style.scss");
|
|
|
|
console.log(result.css);
|
2018-03-17 00:20:00 +01:00
|
|
|
|
|
|
|
// OR
|
|
|
|
|
2023-04-13 21:59:42 +02:00
|
|
|
const result = await sass.compileAsync("style.scss");
|
|
|
|
console.log(result.css);
|
2019-03-05 02:46:32 +01:00
|
|
|
```
|
2018-10-24 00:06:39 +02:00
|
|
|
|
2023-04-13 21:59:42 +02:00
|
|
|
When installed via npm, Dart Sass supports a [brand new JavaScript API],
|
|
|
|
as well as a [legacy API] that's fully compatible with the old Node Sass
|
2023-04-15 00:52:06 +02:00
|
|
|
API. Note that when using the `sass` package, the synchronous API functions
|
2023-04-13 21:59:42 +02:00
|
|
|
are more than twice as fast as the asynchronous API, due to the overhead
|
|
|
|
of asynchronous callbacks.
|
2018-03-17 00:20:00 +01:00
|
|
|
|
2023-04-13 21:59:42 +02:00
|
|
|
[brand new JavaScript API]: https://sass-lang.com/documentation/js-api/
|
|
|
|
[legacy API]: https://sass-lang.com/documentation/js-api/#legacy-api
|