You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

пре 3 година
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. [![npm][npm]][npm-url]
  2. [![node][node]][node-url]
  3. [![npm-stats][npm-stats]][npm-url]
  4. [![deps][deps]][deps-url]
  5. [![travis][travis]][travis-url]
  6. [![appveyor][appveyor]][appveyor-url]
  7. [![coverage][cover]][cover-url]
  8. [![chat][chat]][chat-url]
  9. <div align="center">
  10. <img height="100"
  11. src="https://worldvectorlogo.com/logos/sass-1.svg">
  12. <a href="https://github.com/webpack/webpack">
  13. <img width="200" height="200"
  14. src="https://webpack.js.org/assets/icon-square-big.svg">
  15. </a>
  16. <h1>Sass Loader</h1>
  17. <p>Loads a Sass/SCSS file and compiles it to CSS.</p>
  18. </div>
  19. Use the [css-loader](https://github.com/webpack-contrib/css-loader) or the [raw-loader](https://github.com/webpack-contrib/raw-loader) to turn it into a JS module and the [MiniCssExtractPlugin](https://github.com/webpack-contrib/mini-css-extract-plugin) to extract it into a separate file.
  20. Looking for the webpack 1 loader? Check out the [archive/webpack-1 branch](https://github.com/webpack-contrib/sass-loader/tree/archive/webpack-1).
  21. <h2 align="center">Install</h2>
  22. ```bash
  23. npm install sass-loader node-sass webpack --save-dev
  24. ```
  25. The sass-loader requires [webpack](https://github.com/webpack) as a
  26. [`peerDependency`](https://docs.npmjs.com/files/package.json#peerdependencies)
  27. and it requires you to install either [Node Sass][] or [Dart Sass][] on your
  28. own. This allows you to control the versions of all your dependencies, and to
  29. choose which Sass implementation to use.
  30. [Node Sass]: https://github.com/sass/node-sass
  31. [Dart Sass]: http://sass-lang.com/dart-sass
  32. <h2 align="center">Examples</h2>
  33. Chain the sass-loader with the [css-loader](https://github.com/webpack-contrib/css-loader) and the [style-loader](https://github.com/webpack-contrib/style-loader) to immediately apply all styles to the DOM.
  34. ```bash
  35. npm install style-loader css-loader --save-dev
  36. ```
  37. ```js
  38. // webpack.config.js
  39. module.exports = {
  40. ...
  41. module: {
  42. rules: [{
  43. test: /\.scss$/,
  44. use: [
  45. "style-loader", // creates style nodes from JS strings
  46. "css-loader", // translates CSS into CommonJS
  47. "sass-loader" // compiles Sass to CSS, using Node Sass by default
  48. ]
  49. }]
  50. }
  51. };
  52. ```
  53. You can also pass options directly to [Node Sass][] or [Dart Sass][]:
  54. ```js
  55. // webpack.config.js
  56. module.exports = {
  57. ...
  58. module: {
  59. rules: [{
  60. test: /\.scss$/,
  61. use: [{
  62. loader: "style-loader"
  63. }, {
  64. loader: "css-loader"
  65. }, {
  66. loader: "sass-loader",
  67. options: {
  68. includePaths: ["absolute/path/a", "absolute/path/b"]
  69. }
  70. }]
  71. }]
  72. }
  73. };
  74. ```
  75. See [the Node Sass documentation](https://github.com/sass/node-sass/blob/master/README.md#options) for all available Sass options.
  76. The special `implementation` option determines which implementation of Sass to
  77. use. It takes either a [Node Sass][] or a [Dart Sass][] module. For example, to
  78. use Dart Sass, you'd pass:
  79. ```js
  80. // ...
  81. {
  82. loader: "sass-loader",
  83. options: {
  84. implementation: require("sass")
  85. }
  86. }
  87. // ...
  88. ```
  89. Note that when using Dart Sass, **synchronous compilation is twice as fast as
  90. asynchronous compilation** by default, due to the overhead of asynchronous
  91. callbacks. To avoid this overhead, you can use the
  92. [`fibers`](https://www.npmjs.com/package/fibers) package to call asynchronous
  93. importers from the synchronous code path. To enable this, pass the `Fiber` class
  94. to the `fiber` option:
  95. ```js
  96. // webpack.config.js
  97. const Fiber = require('fibers');
  98. module.exports = {
  99. ...
  100. module: {
  101. rules: [{
  102. test: /\.scss$/,
  103. use: [{
  104. loader: "style-loader"
  105. }, {
  106. loader: "css-loader"
  107. }, {
  108. loader: "sass-loader",
  109. options: {
  110. implementation: require("sass"),
  111. fiber: Fiber
  112. }
  113. }]
  114. }]
  115. }
  116. };
  117. ```
  118. ### In production
  119. Usually, it's recommended to extract the style sheets into a dedicated file in production using the [MiniCssExtractPlugin](https://github.com/webpack-contrib/mini-css-extract-plugin). This way your styles are not dependent on JavaScript:
  120. ```js
  121. const MiniCssExtractPlugin = require("mini-css-extract-plugin");
  122. module.exports = {
  123. ...
  124. module: {
  125. rules: [{
  126. test: /\.scss$/,
  127. use: [
  128. // fallback to style-loader in development
  129. process.env.NODE_ENV !== 'production' ? 'style-loader' : MiniCssExtractPlugin.loader,
  130. "css-loader",
  131. "sass-loader"
  132. ]
  133. }]
  134. },
  135. plugins: [
  136. new MiniCssExtractPlugin({
  137. // Options similar to the same options in webpackOptions.output
  138. // both options are optional
  139. filename: "[name].css",
  140. chunkFilename: "[id].css"
  141. })
  142. ]
  143. };
  144. ```
  145. <h2 align="center">Usage</h2>
  146. ### Imports
  147. webpack provides an [advanced mechanism to resolve files](https://webpack.js.org/concepts/module-resolution/). The sass-loader uses Sass's 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:
  148. ```css
  149. @import "~bootstrap/dist/css/bootstrap";
  150. ```
  151. 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";`
  152. ### Problems with `url(...)`
  153. Since Sass/[libsass](https://github.com/sass/libsass) does not provide [url rewriting](https://github.com/sass/libsass/issues/532), all linked assets must be relative to the output.
  154. - If you're just generating CSS without passing it to the css-loader, it must be relative to your web root.
  155. - If you pass the generated CSS on to the css-loader, all urls must be relative to the entry-file (e.g. `main.scss`).
  156. More likely you will be disrupted by this second issue. It is natural to expect relative references to be resolved against the `.scss` file in which they are specified (like in regular `.css` files). Thankfully there are a two solutions to this problem:
  157. - Add the missing url rewriting using the [resolve-url-loader](https://github.com/bholloway/resolve-url-loader). Place it before the sass-loader in the loader chain.
  158. - 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/webpack-contrib/sass-loader/tree/master/test/bootstrapSass).
  159. ### Extracting style sheets
  160. Bundling CSS with webpack has some nice advantages like referencing images and fonts with hashed urls or [hot module replacement](https://webpack.js.org/concepts/hot-module-replacement/) in development. In production, on the other hand, it's not a good idea to apply your style sheets 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.
  161. There are two possibilities to extract a style sheet from the bundle:
  162. - [extract-loader](https://github.com/peerigon/extract-loader) (simpler, but specialized on the css-loader's output)
  163. - [extract-text-webpack-plugin](https://github.com/webpack-contrib/extract-text-webpack-plugin) (more complex, but works in all use-cases)
  164. ### Source maps
  165. To enable CSS source maps, you'll need to pass the `sourceMap` option to the sass-loader *and* the css-loader. Your `webpack.config.js` should look like this:
  166. ```javascript
  167. module.exports = {
  168. ...
  169. devtool: "source-map", // any "source-map"-like devtool is possible
  170. module: {
  171. rules: [{
  172. test: /\.scss$/,
  173. use: [{
  174. loader: "style-loader"
  175. }, {
  176. loader: "css-loader", options: {
  177. sourceMap: true
  178. }
  179. }, {
  180. loader: "sass-loader", options: {
  181. sourceMap: true
  182. }
  183. }]
  184. }]
  185. }
  186. };
  187. ```
  188. If you want to edit the original Sass files inside Chrome, [there's a good blog post](https://medium.com/@toolmantim/getting-started-with-css-sourcemaps-and-in-browser-sass-editing-b4daab987fb0). Checkout [test/sourceMap](https://github.com/webpack-contrib/sass-loader/tree/master/test) for a running example.
  189. ### Environment variables
  190. If you want to prepend Sass code before the actual entry file, you can 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:
  191. ```javascript
  192. {
  193. loader: "sass-loader",
  194. options: {
  195. data: "$env: " + process.env.NODE_ENV + ";"
  196. }
  197. }
  198. ```
  199. **Please note:** Since you're injecting code, this will break the source mappings in your entry file. Often there's a simpler solution than this, like multiple Sass entry files.
  200. <h2 align="center">Maintainers</h2>
  201. <table>
  202. <tr>
  203. <td align="center">
  204. <a href="https://github.com/jhnns"><img width="150" height="150" src="https://avatars0.githubusercontent.com/u/781746?v=3"></a><br>
  205. <a href="https://github.com/jhnns">Johannes Ewald</a>
  206. </td>
  207. <td align="center">
  208. <a href="https://github.com/webpack-contrib"><img width="150" height="150" src="https://avatars1.githubusercontent.com/u/1243901?v=3&s=460"></a><br>
  209. <a href="https://github.com/webpack-contrib">Jorik Tangelder</a>
  210. </td>
  211. <td align="center">
  212. <a href="https://github.com/akiran"><img width="150" height="150" src="https://avatars1.githubusercontent.com/u/3403295?v=3"></a><br>
  213. <a href="https://github.com/akiran">Kiran</a>
  214. </td>
  215. <tr>
  216. </table>
  217. <h2 align="center">License</h2>
  218. [MIT](http://www.opensource.org/licenses/mit-license.php)
  219. [npm]: https://img.shields.io/npm/v/sass-loader.svg
  220. [npm-stats]: https://img.shields.io/npm/dm/sass-loader.svg
  221. [npm-url]: https://npmjs.com/package/sass-loader
  222. [node]: https://img.shields.io/node/v/sass-loader.svg
  223. [node-url]: https://nodejs.org
  224. [deps]: https://david-dm.org/webpack-contrib/sass-loader.svg
  225. [deps-url]: https://david-dm.org/webpack-contrib/sass-loader
  226. [travis]: http://img.shields.io/travis/webpack-contrib/sass-loader.svg
  227. [travis-url]: https://travis-ci.org/webpack-contrib/sass-loader
  228. [appveyor-url]: https://ci.appveyor.com/project/webpack-contrib/sass-loader/branch/master
  229. [appveyor]: https://ci.appveyor.com/api/projects/status/rqpy1vaovh20ttxs/branch/master?svg=true
  230. [cover]: https://codecov.io/gh/webpack-contrib/sass-loader/branch/master/graph/badge.svg
  231. [cover-url]: https://codecov.io/gh/webpack-contrib/sass-loader
  232. [chat]: https://badges.gitter.im/webpack/webpack.svg
  233. [chat-url]: https://gitter.im/webpack/webpack