Skip to content

Commit 735808f

Browse files
committed
Update README
- Add section about Extracting stylesheets - Add section about Environment variables - Small adjustments
1 parent a114cca commit 735808f

File tree

1 file changed

+29
-6
lines changed

1 file changed

+29
-6
lines changed

README.md

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,17 @@ as [`peerDependency`](https://docs.npmjs.com/files/package.json#peerdependencies
1515

1616
``` javascript
1717
var css = require("!raw!sass!./file.scss");
18-
// => returns compiled css code from file.scss, resolves Sass imports
18+
// returns compiled css code from file.scss, resolves Sass imports
1919
var css = require("!css!sass!./file.scss");
20-
// => returns compiled css code from file.scss, resolves Sass and CSS imports and url(...)s
20+
// returns compiled css code from file.scss, resolves Sass and CSS imports and url(...)s
2121
```
2222

2323
Use in tandem with the [`style-loader`](https://github.com/webpack/style-loader) and [`css-loader`](https://github.com/webpack/css-loader) to add the css rules to your document:
2424

2525
``` javascript
2626
require("!style!css!sass!./file.scss");
2727
```
28-
*NOTE: If you encounter module errors complaining about a missing `style` or `css` module, make sure you have installed all required loaders via npm.*
28+
*Please note: If you encounter module errors complaining about a missing `style` or `css` module, make sure you have installed all required loaders via npm.*
2929

3030
### Apply via webpack config
3131

@@ -49,7 +49,7 @@ Then you only need to write: `require("./file.scss")`.
4949

5050
### Sass options
5151

52-
You can pass options to node-sass by defining a `sassLoader`-property on your `webpack.config.js`. See [node-sass](https://github.com/andrew/node-sass) for all available options.
52+
You can pass options to node-sass by defining a `sassLoader`-property on your `webpack.config.js`. See [node-sass](https://github.com/andrew/node-sass) for all available Sass-options.
5353

5454
```javascript
5555
module.exports = {
@@ -91,13 +91,27 @@ module.exports = {
9191

9292
### Imports
9393

94-
webpack provides an [advanced mechanism to resolve files](http://webpack.github.io/docs/resolving.html). The sass-loader uses node-sass' custom importer feature to pass all queries to the webpack resolving engine. Thus you can import your sass-modules from `node_modules`. Just prepend them with a `~` which tells webpack to look-up the [`modulesDirectories`](http://webpack.github.io/docs/configuration.html#resolve-modulesdirectories).
94+
webpack provides an [advanced mechanism to resolve files](http://webpack.github.io/docs/resolving.html). The sass-loader uses node-sass' custom importer feature to pass all queries to the webpack resolving engine. Thus you can import your Sass modules from `node_modules`. Just prepend them with a `~` to tell webpack that this is not a relative import:
9595

9696
```css
9797
@import "~bootstrap/less/bootstrap";
9898
```
9999

100-
It's important to only prepend it with `~`, because `~/` resolves to the home-directory. webpack needs to distinguish between `bootstrap` and `~bootstrap` because CSS- and Sass-files have no special syntax for importing relative files. Writing `@import "file"` is the same as `@import "./file";`
100+
It's important to only prepend it with `~`, because `~/` resolves to the home directory. webpack needs to distinguish between `bootstrap` and `~bootstrap` because CSS- and Sass-files have no special syntax for importing relative files. Writing `@import "file"` is the same as `@import "./file";`
101+
102+
### Environment variables
103+
104+
If you want to prepend Sass code before the actual entry file, you can simply set the `data`-option. In this case, the sass-loader will not override the `data`-option but just append the entry's content. This is especially useful when some of your Sass variables depend on the environment:
105+
106+
```javascript
107+
module.exports = {
108+
...
109+
sassLoader: {
110+
data: "$env: " + process.env.ENV + ";"
111+
}
112+
};
113+
```
114+
101115

102116
### Problems with `url(...)`
103117

@@ -111,6 +125,15 @@ More likely you will be disrupted by this second issue. It is natural to expect
111125
- Add the missing url rewriting using the [resolve-url-loader](https://github.com/bholloway/resolve-url-loader). Place it directly after the sass-loader in the loader chain.
112126
- Library authors usually provide a variable to modify the asset path. [bootstrap-sass](https://github.com/twbs/bootstrap-sass) for example has an `$icon-font-path`. Check out [this working bootstrap example](https://github.com/jtangelder/sass-loader/tree/master/test/bootstrapSass).
113127

128+
### Extracting stylesheets
129+
130+
Bundling CSS with webpack has some nice advantages like referencing images and fonts with hashed urls or [hot module replacement](http://webpack.github.io/docs/hot-module-replacement-with-webpack.html) in development. In production, on the other hand, it's not a good idea to apply your stylesheets depending on JS execution. Rendering may be delayed or even a [FOUC](https://en.wikipedia.org/wiki/Flash_of_unstyled_content) might be visible. Thus it's often still better to have them as separate files in your final production build.
131+
132+
There are two possibilties to extract a stylesheet from the bundle:
133+
134+
- [extract-loader](https://github.com/peerigon/extract-loader) (simpler, but specialized on the css-loader's output)
135+
- [extract-text-webpack-plugin](https://github.com/webpack/extract-text-webpack-plugin) (more complex, but works in all use-cases)
136+
114137
### Source maps
115138

116139
To enable CSS Source maps, you'll need to pass the `sourceMap`-option to the sass- *and* the css-loader. Your `webpack.config.js` should look like this:

0 commit comments

Comments
 (0)