Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make this package implementation-agnostic #186

Merged
merged 2 commits into from
Sep 3, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 21 additions & 27 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@
[![Ember Observer Score](http://emberobserver.com/badges/ember-cli-sass.svg)](http://emberobserver.com/addons/ember-cli-sass)
[![Dependency Status](https://david-dm.org/aexmachina/ember-cli-sass.svg)](https://david-dm.org/aexmachina/ember-cli-sass)

ember-cli-sass uses libsass to preprocess your ember-cli app's files and provides support for source maps and include paths. It provides support for the common use case for Ember.js projects:
ember-cli-sass uses [Dart Sass][] or [LibSass][] to preprocess your ember-cli app's files and provides support for source maps and include paths. It provides support for the common use case for Ember.js projects:

[Dart Sass]: https://sass-lang.com/dart-sass
[LibSass]: https://sass-lang.com/libsass

- Source maps by default in development
- Support for [`outputPaths` configuration](http://ember-cli.com/user-guide/#configuring-output-paths)
Expand All @@ -16,36 +19,43 @@ ember-cli-sass uses libsass to preprocess your ember-cli app's files and provide

```
ember install ember-cli-sass
npm install --save-dev sass # or node-sass to use LibSass
```

### Addon Development

If you want to use ember-cli-sass in an addon and you want to distribute the compiled CSS it must be installed as a `dependency` so that `addon/styles/addon.scss` is compiled into `dist/assets/vendor.css`. This can be done using:

```bash
npm install --save ember-cli-sass
npm install --save ember-cli-sass sass
```

## Usage

By default this addon will compile `app/styles/app.scss` into `dist/assets/app.css` and produce
a source map for your delectation.

If you want more control then you can specify options using the
`sassOptions` config property in `ember-cli-build.js` (or in `Brocfile.js` if you are using an Ember CLI version older than 1.13):
To use this addon, you must pass a Sass implementation to the `sassOptions`
config property in `ember-cli-build.js` (or in `Brocfile.js` if you are using an
Ember CLI version older than 1.13):

```javascript
var sass = require('sass');

var app = new EmberApp({
sassOptions: {...}
sassOptions: {
implementation: sass
}
});
```

By default this addon will compile `app/styles/app.scss` into `dist/assets/app.css` and produce
a source map for your delectation.

If you want more control, you can pass additional options to `sassOptions`:

- `includePaths`: an array of include paths
- `onlyIncluded`: true/false whether to use only what is in `app/styles` and `includePaths`. This may helps with performance, particularly when using NPM linked modules
- `sourceMap`: controls whether to generate sourceMaps, defaults to `true` in development. The sourceMap file will be saved to `options.outputFile + '.map'`
- `extension`: specifies the file extension for the input files, defaults to `scss`. Set to `sass` if you want to use `.sass` instead.
- `passthrough`: an optional hash of [broccoli-funnel](https://github.com/broccolijs/broccoli-funnel) configuration for files from the styles tree to be passed through to `dist`
- `nodeSass`: Allows a different version of [node-sass](https://www.npmjs.com/package/node-sass) to be used (see below)
- See [broccoli-sass-source-maps](https://github.com/aexmachina/broccoli-sass-source-maps) for a list of other supported options.

### Processing multiple files
Expand All @@ -65,22 +75,6 @@ var app = new EmberApp({
});
```

### Choosing the version of node-sass

You can specify which version of node-sass to use with the [`nodeSass` option](https://github.com/aexmachina/broccoli-sass-source-maps#usage).

Add the version that you want to use to _your_ package.json and then provide that version of the module using the `nodeSass` option:

```js
var nodeSass = require('node-sass'); // loads the version in your package.json

var app = new EmberApp({
sassOptions: {
nodeSass: nodeSass
}
});
```

### Source Maps

Source maps work for reading with no configuration, but to edit the SASS in the Dev Tools
Expand Down Expand Up @@ -128,8 +122,8 @@ To compile SASS within an ember-cli addon, there are a few additional steps:

1. Include your styles in `addon/styles/addon.scss`.

2. Ensure you've installed `ember-cli-sass` under `dependencies` in your
`package.json`.
2. Ensure you've installed `ember-cli-sass` and either `sass` or `node-sass`
under `dependencies` in your `package.json`.

3. Define an `included` function in your app:
```js
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems like we should be encouraging users to pass in a Sass implementation here, but I'm not sure how.

Expand Down
2 changes: 2 additions & 0 deletions ember-cli-build.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
'use strict';

const EmberAddon = require('ember-cli/lib/broccoli/ember-addon');
const sass = require('sass');

module.exports = function(defaults) {
let app = new EmberAddon(defaults, {
Expand All @@ -14,6 +15,7 @@ module.exports = function(defaults) {
}
},
sassOptions: {
implementation: sass,
includePaths: [
'bower_components/SpinKit/scss',
]
Expand Down
11 changes: 10 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict';
/* eslint-env node */
var SassCompiler = require('broccoli-sass-source-maps');
var SassCompilerFactory = require('broccoli-sass-source-maps');
var path = require('path');
var VersionChecker = require('ember-cli-version-checker');
var Funnel = require('broccoli-funnel');
Expand Down Expand Up @@ -30,6 +30,15 @@ SASSPlugin.prototype.toTree = function(tree, inputPath, outputPath, inputOptions
inputTrees = inputTrees.concat(options.includePaths);
}

if (!options.implementation) {
var error = new Error(
'sassOptions must have an implementation option. For example:\n' +
' sassOptions: {implementation: require("sass")}');
error.type = 'Sass Plugin Error';
throw error;
}

var SassCompiler = SassCompilerFactory(options.implementation);
var ext = options.extension || 'scss';
var paths = options.outputPaths;
var trees = Object.keys(paths).map(function(file) {
Expand Down
Loading