sass-site/source/documentation/cli/dart-sass.html.md.erb
2019-12-18 16:09:00 -08:00

472 lines
14 KiB
Plaintext
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

---
title: Dart Sass Command-Line Interface
table_of_contents: true
---
## Usage
The Dart Sass executable can be invoked in one of two modes.
### One-to-One Mode
```
sass <input.scss> [output.css]
```
One-to-one mode compiles a single input file (`input.scss`) to a single output
location (`output.css`). If no output location is passed, the compiled CSS is
printed to the terminal.
The input file is parsed as [SCSS][] if its extension is `.scss`, as the
[indented syntax][] if its extension is `.sass`, or as [plain CSS][] if its
extension is `.css`. If it doesn't have one of these three extensions, or if it
comes from standard input, it's parsed as SCSS by default. This can be
controlled with the [`--indented` flag][].
[SCSS]: ../syntax#scss
[indented syntax]: ../syntax#the-indented-syntax
[plain CSS]: ../at-rules/import#importing-css
[`--indented` flag]: #indented
The special string `-` can be passed in place of the input file to tell Sass to
read the input file from [standard input][]. Sass will default to parsing it as
SCSS unless the [`--indented` flag][] is passed.
[standard input]: https://en.wikipedia.org/wiki/Standard_streams#Standard_input_(stdin)
### Many-to-many Mode
<% impl_status dart: '1.4.0' %>
```
sass [<input.css>:<output.css>] [<input/>:<output/>]...
```
Many-to-many mode compiles one or more input files to one or more output files.
The inputs are separated from the outputs with colons. It can also compile all
Sass files in a directory to CSS files with the same names in another directory.
```shellsession
# Compiles style.scss to style.css.
$ sass style.scss:style.css
# Compiles light.scss and dark.scss to light.css and dark.css.
$ sass light.scss:light.css dark.scss:dark.css
# Compiles all Sass files in themes/ to CSS files in public/css/.
$ sass themes:public/css
```
When compiling whole directories, Sass will ignore [partial files][] whose names
begin with `_`. You can use partials to separate out your stylesheets without
creating a bunch of unnecessary output files.
[partial files]: ../at-rules/import#partials
## Options
### Input and Output
These options control how Sass loads its input files and how it produces output
files.
#### `--stdin`
This flag is an alternative way of telling Sass that it should read its input
file from [standard input][]. When it's passed, no input file may be passed.
```shellsession
$ echo "h1 {font-size: 40px}" | sass --stdin h1.css
$ echo "h1 {font-size: 40px}" | sass --stdin
h1 {
font-size: 40px;
}
```
The `--stdin` flag may not be used with [many-to-many mode][].
[many-to-many mode]: #many-to-many-mode
#### `--indented`
This flag tells Sass to parse the input file as the [indented syntax][]. If it's
used in [many-to-many mode][], all input files are parsed as the indented
syntax, although files they [use][] will have their syntax determined as usual.
The inverse, `--no-indented`, can be used to force all input files to be parsed
as [SCSS][] instead.
[use]: ../at-rules/use
The `--indented` flag is mostly useful when the input file is coming from
[standard input][], so its syntax can't be automatically determined.
```shellsession
$ echo -e 'h1\n font-size: 40px' | sass --indented -
h1 {
font-size: 40px;
}
```
#### `--load-path`
This option (abbreviated `-I`) adds an additional [load path][] for Sass to look
for stylesheets. It can be passed multiple times to provide multiple load paths.
Earlier load paths will take precedence over later ones.
[load path]: ../at-rules/use#load-paths
```shellsession
$ sass --load-path=node_modules/bootstrap/dist/css style.scss style.css
```
#### `--style`
This option (abbreviated `-s`) controls the output style of the resulting CSS.
Dart Sass supports two output styles:
* `expanded` (the default) writes each selector and declaration on its own line.
* `compressed` removes as many extra characters as possible, and writes the
entire stylesheet on a single line.
```shellsession
$ sass --style=expanded style.scss
h1 {
font-size: 40px;
}
$ sass --style=compressed style.scss
h1{font-size:40px}
```
#### `--no-charset`
<% impl_status dart: '1.19.0' %>
This flag tells Sass never to emit a `@charset` declaration or a UTF-8
[byte-order mark][]. By default, or if `--charset` is passed, Sass will insert
either a `@charset` declaration (in expanded output mode) or a byte-order mark
(in compressed output mode) if the stylesheet contains any non-ASCII characters.
[byte-order mark]: https://en.wikipedia.org/wiki/Byte_order_mark#UTF-8
```shellsession
$ echo 'h1::before {content: "👭"}' | sass --no-charset
h1::before {
content: "👭";
}
$ echo 'h1::before {content: "👭"}' | sass --charset
@charset "UTF-8";
h1::before {
content: "👭";
}
```
#### `--error-css`
<% impl_status dart: '1.20.0' %>
This flag tells Sass whether to emit a CSS file when an error occurs during
compilation. This CSS file describes the error in a comment *and* in the
`content` property of `body::before`, so that you can see the error message in
the browser without needing to switch back to the terminal.
By default, error CSS is enabled if you're compiling to at least one file on
disk (as opposed to standard output). You can pass `--error-css` explicitly to
enable it even when you're compiling to standard out, or `--no-error-css` to
disable it everywhere. When it's disabled, the [`--update` flag][] and
[`--watch` flag][] will delete CSS files instead when an error occurs.
[`--watch` flag]: #watch
```shellsession
$ sass --error-css style.scss style.css
/* Error: Incompatible units em and px.
* ,
* 1 | $width: 15px + 2em;
* | ^^^^^^^^^^
* '
* test.scss 1:9 root stylesheet */
body::before {
font-family: "Source Code Pro", "SF Mono", Monaco, Inconsolata, "Fira Mono",
"Droid Sans Mono", monospace, monospace;
white-space: pre;
display: block;
padding: 1em;
margin-bottom: 1em;
border-bottom: 2px solid black;
content: "Error: Incompatible units em and px.\a \2577 \a 1 \2502 $width: 15px + 2em;\a \2502 ^^^^^^^^^^\a \2575 \a test.scss 1:9 root stylesheet";
}
Error: Incompatible units em and px.
1 │ $width: 15px + 2em;
│ ^^^^^^^^^^
test.scss 1:9 root stylesheet
```
#### `--update`
<% impl_status dart: '1.4.0' %>
If the `--update` flag is passed, Sass will only compile stylesheets whose
dependencies have been modified more recently than the corresponding CSS file
was generated. It will also print status messages when updating stylesheets.
```shellsession
$ sass --update themes:public/css
Compiled themes/light.scss to public/css/light.css.
```
### Source Maps
<% impl_status dart: '1.3.0' %>
<%= partial 'documentation/snippets/source-maps' %>
Dart Sass generates source maps by default for every CSS file it emits.
#### `--no-source-map`
If the `--no-source-map` flag is passed, Sass won't generate any source maps. it
cannot be passed along with other source map options.
```shellsession
$ sass --no-source-map style.scss style.css
```
#### `--source-map-urls`
This option controls how the source maps that Sass generates link back to the
Sass files that contributed to the generated CSS. Dart Sass supports two types
of URLs:
* `relative` (the default) uses relative URLs from the location of the source
map file to the locations of the Sass source file.
* `absolute` uses the absolute [`file:` URLs][] of the Sass source files. Note
that absolute URLs will only work on the same computer that the CSS was
compiled.
[`file:` URLs]: https://en.wikipedia.org/wiki/File_URI_scheme
```shellsession
# Generates a URL like "../sass/style.scss".
$ sass --source-map-urls=relative sass/style.scss css/style.css
# Generates a URL like "file:///home/style-wiz/sassy-app/sass/style.scss".
$ sass --source-map-urls=absolute sass/style.scss css/style.css
```
#### `--embed-sources`
This flag tells Sass to embed the entire contents of the Sass files that
contributed to the generated CSS in the source map. This may produce very large
source maps, but it guarantees that the source will be available on any computer
no matter how the CSS is served.
```shellsession
$ sass --embed-sources sass/style.scss css.style.css
```
#### `--embed-source-map`
This flag tells Sass to embed the contents of the source map file in the
generated CSS, rather than creating a separate file and linking to it from the
CSS.
```shellsession
$ sass --embed-source-map sass/style.scss css.style.css
```
### Other Options
#### `--watch`
<% impl_status dart: '1.6.0' %>
This flag acts like the [`--update` flag][], but after the first round
compilation is done Sass stays open and continues compiling stylesheets whenever
they or their dependencies change.
[`--update` flag]: #update
```shellsession
$ sass --watch themes:public/css
Compiled themes/light.scss to public/css/light.css.
# Then when you edit themes/dark.scss...
Compiled themes/dark.scss to public/css/dark.css.
```
#### `--poll`
<% impl_status dart: '1.8.0' %>
This flag, which may only be passed along with `--watch`, tells Sass to manually
check for changes to the source files every so often instead of relying on the
operating system to notify it when something changes. This may be necessary if
you're editing Sass on a remote drive where the operating system's notification
system doesn't work.
```shellsession
$ sass --watch --poll themes:public/css
Compiled themes/light.scss to public/css/light.css.
# Then when you edit themes/dark.scss...
Compiled themes/dark.scss to public/css/dark.css.
```
#### `--stop-on-error`
<% impl_status dart: '1.8.0' %>
This flag tells Sass to stop compiling immediately when an error is detected,
rather than trying to compile other Sass files that may not contain errors. It's
mostly useful in [many-to-many mode][].
```shellsession
$ sass --stop-on-error themes:public/css
Error: Expected expression.
42 │ h1 {font-face: }
│ ^
themes/light.scss 42:16 root stylesheet
```
#### `--interactive`
<% impl_status dart: '1.5.0' %>
This flag (abbreviated `-i`) tells Sass to run in interactive mode, where you
can write [SassScript expressions][] and see their results. Interactive mode
also supports [variables][] and [`@use` rules][].
[SassScript expressions]: ../syntax/structure#expressions
[variables]: ../variables
[`@use` rules]: ../at-rules/use
```shellsession
$ sass --interactive
>> 1px + 1in
97px
>> @use "sass:map"
>> $map: ("width": 100px, "height": 70px)
("width": 100px, "height": 70px)
>> map.get($map, "width")
100px
```
#### `--color`
This flag (abbreviated `-c`) tells Sass to emit [terminal colors][], and the
inverse `--no-color` tells it not to emit colors. By default, it will emit
colors if it looks like it's being run on a terminal that supports them.
[terminal colors]: https://en.wikipedia.org/wiki/ANSI_escape_code#Colors
<pre class="highlight plaintext"><code>$ sass --color style.scss style.css
Error: Incompatible units em and px.
<span style="color: blue">╷</span>
<span style="color: blue">1 │</span> $width: <span style="color: crimson">15px + 2em</span>
<span style="color: blue">│</span> <span style="color: crimson">^^^^^^^^^^</span>
<span style="color: blue">╵</span>
style.scss 1:9 root stylesheet
$ sass --no-color style.scss style.css
Error: Incompatible units em and px.
1 │ $width: 15px + 2em
│ ^^^^^^^^^^
style.scss 1:9 root stylesheet</code></pre>
#### `--no-unicode`
<% impl_status dart: '1.17.0' %>
This flag tells Sass only to emit ASCII characters to the terminal as part of
error messages. By default, or if `--unicode` is passed, Sass will emit non-ASCII
characters for these messages. This flag does not affect the CSS output.
```shellsession
$ sass --no-unicode style.scss style.css
Error: Incompatible units em and px.
,
1 | $width: 15px + 2em;
| ^^^^^^^^^^
'
test.scss 1:9 root stylesheet
$ sass --unicode style.scss style.css
Error: Incompatible units em and px.
1 │ $width: 15px + 2em;
│ ^^^^^^^^^^
test.scss 1:9 root stylesheet
```
#### `--quiet`
This flag (abbreviated `-q`) tells Sass not to emit any warnings when compiling.
By default, Sass emits warnings when deprecated features are used or when the
[`@warn` rule][] is encountered. It also silences the [`@debug` rule][].
[`@warn` rule]: ../at-rules/warn
[`@debug` rule]: ../at-rules/debug
```shellsession
$ sass --quiet style.scss style.css
```
#### `--trace`
This flag tells Sass to print the full Dart or JavaScript stack trace when an
error is encountered. It's used by the Sass team for debugging errors.
```shellsession
$ sass --trace style.scss style.css
Error: Expected expression.
42 │ h1 {font-face: }
│ ^
themes/light.scss 42:16 root stylesheet
package:sass/src/visitor/evaluate.dart 1846:7 _EvaluateVisitor._addExceptionSpan
package:sass/src/visitor/evaluate.dart 1128:12 _EvaluateVisitor.visitBinaryOperationExpression
package:sass/src/ast/sass/expression/binary_operation.dart 39:15 BinaryOperationExpression.accept
package:sass/src/visitor/evaluate.dart 1097:25 _EvaluateVisitor.visitVariableDeclaration
package:sass/src/ast/sass/statement/variable_declaration.dart 50:15 VariableDeclaration.accept
package:sass/src/visitor/evaluate.dart 335:13 _EvaluateVisitor.visitStylesheet
package:sass/src/visitor/evaluate.dart 323:5 _EvaluateVisitor.run
package:sass/src/visitor/evaluate.dart 81:10 evaluate
package:sass/src/executable/compile_stylesheet.dart 59:9 compileStylesheet
package:sass/src/executable.dart 62:15 main
```
#### `--help`
This flag (abbreviated `-h`) prints a summary of this documentation.
```shellsession
$ sass --help
Compile Sass to CSS.
Usage: sass <input.scss> [output.css]
sass <input.scss>:<output.css> <input/>:<output/>
...
```
#### `--version`
This flag prints the current version of Sass.
```shellsession
$ sass --version
<%= impl_version(:dart) %>
```