You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+29-6Lines changed: 29 additions & 6 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -15,17 +15,17 @@ as [`peerDependency`](https://docs.npmjs.com/files/package.json#peerdependencies
15
15
16
16
```javascript
17
17
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
19
19
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
21
21
```
22
22
23
23
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:
24
24
25
25
```javascript
26
26
require("!style!css!sass!./file.scss");
27
27
```
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.*
29
29
30
30
### Apply via webpack config
31
31
@@ -49,7 +49,7 @@ Then you only need to write: `require("./file.scss")`.
49
49
50
50
### Sass options
51
51
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.
53
53
54
54
```javascript
55
55
module.exports= {
@@ -91,13 +91,27 @@ module.exports = {
91
91
92
92
### Imports
93
93
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:
95
95
96
96
```css
97
97
@import"~bootstrap/less/bootstrap";
98
98
```
99
99
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
+
101
115
102
116
### Problems with `url(...)`
103
117
@@ -111,6 +125,15 @@ More likely you will be disrupted by this second issue. It is natural to expect
111
125
- 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.
112
126
- 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).
113
127
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
+
114
137
### Source maps
115
138
116
139
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