| [9af201e] | 1 | <div align="center">
|
|---|
| 2 | <img height="170"
|
|---|
| 3 | src="https://worldvectorlogo.com/logos/sass-1.svg">
|
|---|
| 4 | <a href="https://github.com/webpack/webpack">
|
|---|
| 5 | <img width="200" height="200"
|
|---|
| 6 | src="https://webpack.js.org/assets/icon-square-big.svg">
|
|---|
| 7 | </a>
|
|---|
| 8 | </div>
|
|---|
| 9 |
|
|---|
| 10 | [![npm][npm]][npm-url]
|
|---|
| 11 | [![node][node]][node-url]
|
|---|
| 12 | [![deps][deps]][deps-url]
|
|---|
| 13 | [![tests][tests]][tests-url]
|
|---|
| 14 | [![coverage][cover]][cover-url]
|
|---|
| 15 | [![chat][chat]][chat-url]
|
|---|
| 16 | [![size][size]][size-url]
|
|---|
| 17 |
|
|---|
| 18 | # sass-loader
|
|---|
| 19 |
|
|---|
| 20 | Loads a Sass/SCSS file and compiles it to CSS.
|
|---|
| 21 |
|
|---|
| 22 | ## Getting Started
|
|---|
| 23 |
|
|---|
| 24 | To begin, you'll need to install `sass-loader`:
|
|---|
| 25 |
|
|---|
| 26 | ```console
|
|---|
| 27 | npm install sass-loader sass webpack --save-dev
|
|---|
| 28 | ```
|
|---|
| 29 |
|
|---|
| 30 | or
|
|---|
| 31 |
|
|---|
| 32 | ```console
|
|---|
| 33 | yarn add -D sass-loader sass webpack
|
|---|
| 34 | ```
|
|---|
| 35 |
|
|---|
| 36 | or
|
|---|
| 37 |
|
|---|
| 38 | ```console
|
|---|
| 39 | pnpm add -D sass-loader sass webpack
|
|---|
| 40 | ```
|
|---|
| 41 |
|
|---|
| 42 | `sass-loader` requires you to install either [Dart Sass](https://github.com/sass/dart-sass), [Node Sass](https://github.com/sass/node-sass) on your own (more documentation can be found below) or [Sass Embedded](https://github.com/sass/embedded-host-node).
|
|---|
| 43 |
|
|---|
| 44 | This allows you to control the versions of all your dependencies, and to choose which Sass implementation to use.
|
|---|
| 45 |
|
|---|
| 46 | > ℹ️ We highly recommend using [Dart Sass](https://github.com/sass/dart-sass).
|
|---|
| 47 |
|
|---|
| 48 | > ⚠ [Node Sass](https://github.com/sass/node-sass) does not work with [Yarn PnP](https://classic.yarnpkg.com/en/docs/pnp/) feature and doesn't support [@use rule](https://sass-lang.com/documentation/at-rules/use).
|
|---|
| 49 |
|
|---|
| 50 | > ⚠ [Sass Embedded](https://github.com/sass/embedded-host-node) is experimental and in `beta`, therefore some features may not work
|
|---|
| 51 |
|
|---|
| 52 | 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 or the [mini-css-extract-plugin](https://github.com/webpack-contrib/mini-css-extract-plugin) to extract it into a separate file.
|
|---|
| 53 |
|
|---|
| 54 | Then add the loader to your Webpack configuration. For example:
|
|---|
| 55 |
|
|---|
| 56 | **app.js**
|
|---|
| 57 |
|
|---|
| 58 | ```js
|
|---|
| 59 | import "./style.scss";
|
|---|
| 60 | ```
|
|---|
| 61 |
|
|---|
| 62 | **style.scss**
|
|---|
| 63 |
|
|---|
| 64 | ```scss
|
|---|
| 65 | $body-color: red;
|
|---|
| 66 |
|
|---|
| 67 | body {
|
|---|
| 68 | color: $body-color;
|
|---|
| 69 | }
|
|---|
| 70 | ```
|
|---|
| 71 |
|
|---|
| 72 | **webpack.config.js**
|
|---|
| 73 |
|
|---|
| 74 | ```js
|
|---|
| 75 | module.exports = {
|
|---|
| 76 | module: {
|
|---|
| 77 | rules: [
|
|---|
| 78 | {
|
|---|
| 79 | test: /\.s[ac]ss$/i,
|
|---|
| 80 | use: [
|
|---|
| 81 | // Creates `style` nodes from JS strings
|
|---|
| 82 | "style-loader",
|
|---|
| 83 | // Translates CSS into CommonJS
|
|---|
| 84 | "css-loader",
|
|---|
| 85 | // Compiles Sass to CSS
|
|---|
| 86 | "sass-loader",
|
|---|
| 87 | ],
|
|---|
| 88 | },
|
|---|
| 89 | ],
|
|---|
| 90 | },
|
|---|
| 91 | };
|
|---|
| 92 | ```
|
|---|
| 93 |
|
|---|
| 94 | Finally run `webpack` via your preferred method.
|
|---|
| 95 |
|
|---|
| 96 | ### Resolving `import` at-rules
|
|---|
| 97 |
|
|---|
| 98 | Webpack provides an [advanced mechanism to resolve files](https://webpack.js.org/concepts/module-resolution/).
|
|---|
| 99 |
|
|---|
| 100 | The `sass-loader` uses Sass's custom importer feature to pass all queries to the Webpack resolving engine.
|
|---|
| 101 | Thus you can import your Sass modules from `node_modules`.
|
|---|
| 102 |
|
|---|
| 103 | ```scss
|
|---|
| 104 | @import "bootstrap";
|
|---|
| 105 | ```
|
|---|
| 106 |
|
|---|
| 107 | Using `~` is deprecated and can be removed from your code (**we recommend it**), but we still support it for historical reasons.
|
|---|
| 108 | Why can you remove it? The loader will first try to resolve `@import` as a relative path. If it cannot be resolved, then the loader will try to resolve `@import` inside [`node_modules`](https://webpack.js.org/configuration/resolve/#resolvemodules).
|
|---|
| 109 |
|
|---|
| 110 | Prepending module paths with a `~` tells webpack to search through [`node_modules`](https://webpack.js.org/configuration/resolve/#resolvemodules).
|
|---|
| 111 |
|
|---|
| 112 | ```scss
|
|---|
| 113 | @import "~bootstrap";
|
|---|
| 114 | ```
|
|---|
| 115 |
|
|---|
| 116 | It's important to prepend it with only `~`, because `~/` resolves to the home directory.
|
|---|
| 117 | Webpack needs to distinguish between `bootstrap` and `~bootstrap` because CSS and Sass files have no special syntax for importing relative files.
|
|---|
| 118 | Writing `@import "style.scss"` is the same as `@import "./style.scss";`
|
|---|
| 119 |
|
|---|
| 120 | ### Problems with `url(...)`
|
|---|
| 121 |
|
|---|
| 122 | Since Sass implementations don't provide [url rewriting](https://github.com/sass/libsass/issues/532), all linked assets must be relative to the output.
|
|---|
| 123 |
|
|---|
| 124 | - If you pass the generated CSS on to the `css-loader`, all urls must be relative to the entry-file (e.g. `main.scss`).
|
|---|
| 125 | - If you're just generating CSS without passing it to the `css-loader`, it must be relative to your web root.
|
|---|
| 126 |
|
|---|
| 127 | You will be disrupted by this first issue. It is natural to expect relative references to be resolved against the `.sass`/`.scss` file in which they are specified (like in regular `.css` files).
|
|---|
| 128 |
|
|---|
| 129 | Thankfully there are a two solutions to this problem:
|
|---|
| 130 |
|
|---|
| 131 | - Add the missing url rewriting using the [resolve-url-loader](https://github.com/bholloway/resolve-url-loader). Place it before `sass-loader` in the loader chain.
|
|---|
| 132 | - 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`.
|
|---|
| 133 |
|
|---|
| 134 | ## Options
|
|---|
| 135 |
|
|---|
| 136 | - **[`implementation`](#implementation)**
|
|---|
| 137 | - **[`sassOptions`](#sassoptions)**
|
|---|
| 138 | - **[`sourceMap`](#sourcemap)**
|
|---|
| 139 | - **[`additionalData`](#additionaldata)**
|
|---|
| 140 | - **[`webpackImporter`](#webpackimporter)**
|
|---|
| 141 | - **[`warnRuleAsWarning`](#warnruleaswarning)**
|
|---|
| 142 |
|
|---|
| 143 | ### `implementation`
|
|---|
| 144 |
|
|---|
| 145 | Type:
|
|---|
| 146 |
|
|---|
| 147 | ```ts
|
|---|
| 148 | type implementation = object | string;
|
|---|
| 149 | ```
|
|---|
| 150 |
|
|---|
| 151 | Default: `sass`
|
|---|
| 152 |
|
|---|
| 153 | The special `implementation` option determines which implementation of Sass to use.
|
|---|
| 154 |
|
|---|
| 155 | By default the loader resolve the implementation based on your dependencies.
|
|---|
| 156 | Just add required implementation to `package.json` (`sass` or `node-sass` package) and install dependencies.
|
|---|
| 157 |
|
|---|
| 158 | Example where the `sass-loader` loader uses the `sass` (`dart-sass`) implementation:
|
|---|
| 159 |
|
|---|
| 160 | **package.json**
|
|---|
| 161 |
|
|---|
| 162 | ```json
|
|---|
| 163 | {
|
|---|
| 164 | "devDependencies": {
|
|---|
| 165 | "sass-loader": "^7.2.0",
|
|---|
| 166 | "sass": "^1.22.10"
|
|---|
| 167 | }
|
|---|
| 168 | }
|
|---|
| 169 | ```
|
|---|
| 170 |
|
|---|
| 171 | Example where the `sass-loader` loader uses the `node-sass` implementation:
|
|---|
| 172 |
|
|---|
| 173 | **package.json**
|
|---|
| 174 |
|
|---|
| 175 | ```json
|
|---|
| 176 | {
|
|---|
| 177 | "devDependencies": {
|
|---|
| 178 | "sass-loader": "^7.2.0",
|
|---|
| 179 | "node-sass": "^5.0.0"
|
|---|
| 180 | }
|
|---|
| 181 | }
|
|---|
| 182 | ```
|
|---|
| 183 |
|
|---|
| 184 | Beware the situation when `node-sass` and `sass` were installed! By default the `sass-loader` prefers `sass`.
|
|---|
| 185 | In order to avoid this situation you can use the `implementation` option.
|
|---|
| 186 |
|
|---|
| 187 | The `implementation` options either accepts `sass` (`Dart Sass`) or `node-sass` as a module.
|
|---|
| 188 |
|
|---|
| 189 | #### `object`
|
|---|
| 190 |
|
|---|
| 191 | For example, to use Dart Sass, you'd pass:
|
|---|
| 192 |
|
|---|
| 193 | ```js
|
|---|
| 194 | module.exports = {
|
|---|
| 195 | module: {
|
|---|
| 196 | rules: [
|
|---|
| 197 | {
|
|---|
| 198 | test: /\.s[ac]ss$/i,
|
|---|
| 199 | use: [
|
|---|
| 200 | "style-loader",
|
|---|
| 201 | "css-loader",
|
|---|
| 202 | {
|
|---|
| 203 | loader: "sass-loader",
|
|---|
| 204 | options: {
|
|---|
| 205 | // Prefer `dart-sass`
|
|---|
| 206 | implementation: require("sass"),
|
|---|
| 207 | },
|
|---|
| 208 | },
|
|---|
| 209 | ],
|
|---|
| 210 | },
|
|---|
| 211 | ],
|
|---|
| 212 | },
|
|---|
| 213 | };
|
|---|
| 214 | ```
|
|---|
| 215 |
|
|---|
| 216 | #### `string`
|
|---|
| 217 |
|
|---|
| 218 | For example, to use Dart Sass, you'd pass:
|
|---|
| 219 |
|
|---|
| 220 | ```js
|
|---|
| 221 | module.exports = {
|
|---|
| 222 | module: {
|
|---|
| 223 | rules: [
|
|---|
| 224 | {
|
|---|
| 225 | test: /\.s[ac]ss$/i,
|
|---|
| 226 | use: [
|
|---|
| 227 | "style-loader",
|
|---|
| 228 | "css-loader",
|
|---|
| 229 | {
|
|---|
| 230 | loader: "sass-loader",
|
|---|
| 231 | options: {
|
|---|
| 232 | // Prefer `dart-sass`
|
|---|
| 233 | implementation: require.resolve("sass"),
|
|---|
| 234 | },
|
|---|
| 235 | },
|
|---|
| 236 | ],
|
|---|
| 237 | },
|
|---|
| 238 | ],
|
|---|
| 239 | },
|
|---|
| 240 | };
|
|---|
| 241 | ```
|
|---|
| 242 |
|
|---|
| 243 | Note that when using `sass` (`Dart Sass`), **synchronous compilation is twice as fast as asynchronous compilation** by default, due to the overhead of asynchronous callbacks.
|
|---|
| 244 | To avoid this overhead, you can use the [fibers](https://www.npmjs.com/package/fibers) package to call asynchronous importers from the synchronous code path.
|
|---|
| 245 |
|
|---|
| 246 | We automatically inject the [`fibers`](https://github.com/laverdet/node-fibers) package (setup `sassOptions.fiber`) for `Node.js` less v16.0.0 if is possible (i.e. you need install the [`fibers`](https://github.com/laverdet/node-fibers) package).
|
|---|
| 247 |
|
|---|
| 248 | > Fibers is not compatible with `Node.js` v16.0.0 or later ([see introduction to readme](https://github.com/laverdet/node-fibers)).
|
|---|
| 249 |
|
|---|
| 250 | **package.json**
|
|---|
| 251 |
|
|---|
| 252 | ```json
|
|---|
| 253 | {
|
|---|
| 254 | "devDependencies": {
|
|---|
| 255 | "sass-loader": "^7.2.0",
|
|---|
| 256 | "sass": "^1.22.10",
|
|---|
| 257 | "fibers": "^4.0.1"
|
|---|
| 258 | }
|
|---|
| 259 | }
|
|---|
| 260 | ```
|
|---|
| 261 |
|
|---|
| 262 | You can disable automatically injecting the [`fibers`](https://github.com/laverdet/node-fibers) package by passing a `false` value for the `sassOptions.fiber` option.
|
|---|
| 263 |
|
|---|
| 264 | **webpack.config.js**
|
|---|
| 265 |
|
|---|
| 266 | ```js
|
|---|
| 267 | module.exports = {
|
|---|
| 268 | module: {
|
|---|
| 269 | rules: [
|
|---|
| 270 | {
|
|---|
| 271 | test: /\.s[ac]ss$/i,
|
|---|
| 272 | use: [
|
|---|
| 273 | "style-loader",
|
|---|
| 274 | "css-loader",
|
|---|
| 275 | {
|
|---|
| 276 | loader: "sass-loader",
|
|---|
| 277 | options: {
|
|---|
| 278 | implementation: require("sass"),
|
|---|
| 279 | sassOptions: {
|
|---|
| 280 | fiber: false,
|
|---|
| 281 | },
|
|---|
| 282 | },
|
|---|
| 283 | },
|
|---|
| 284 | ],
|
|---|
| 285 | },
|
|---|
| 286 | ],
|
|---|
| 287 | },
|
|---|
| 288 | };
|
|---|
| 289 | ```
|
|---|
| 290 |
|
|---|
| 291 | You can also pass the `fiber` value using this code:
|
|---|
| 292 |
|
|---|
| 293 | **webpack.config.js**
|
|---|
| 294 |
|
|---|
| 295 | ```js
|
|---|
| 296 | module.exports = {
|
|---|
| 297 | module: {
|
|---|
| 298 | rules: [
|
|---|
| 299 | {
|
|---|
| 300 | test: /\.s[ac]ss$/i,
|
|---|
| 301 | use: [
|
|---|
| 302 | "style-loader",
|
|---|
| 303 | "css-loader",
|
|---|
| 304 | {
|
|---|
| 305 | loader: "sass-loader",
|
|---|
| 306 | options: {
|
|---|
| 307 | implementation: require("sass"),
|
|---|
| 308 | sassOptions: {
|
|---|
| 309 | fiber: require("fibers"),
|
|---|
| 310 | },
|
|---|
| 311 | },
|
|---|
| 312 | },
|
|---|
| 313 | ],
|
|---|
| 314 | },
|
|---|
| 315 | ],
|
|---|
| 316 | },
|
|---|
| 317 | };
|
|---|
| 318 | ```
|
|---|
| 319 |
|
|---|
| 320 | ### `sassOptions`
|
|---|
| 321 |
|
|---|
| 322 | Type:
|
|---|
| 323 |
|
|---|
| 324 | ```ts
|
|---|
| 325 | type sassOptions =
|
|---|
| 326 | | import("sass").LegacyOptions<"async">
|
|---|
| 327 | | ((
|
|---|
| 328 | content: string | Buffer,
|
|---|
| 329 | loaderContext: LoaderContext,
|
|---|
| 330 | meta: any
|
|---|
| 331 | ) => import("sass").LegacyOptions<"async">);
|
|---|
| 332 | ```
|
|---|
| 333 |
|
|---|
| 334 | Default: defaults values for Sass implementation
|
|---|
| 335 |
|
|---|
| 336 | Options for [Dart Sass](http://sass-lang.com/dart-sass) or [Node Sass](https://github.com/sass/node-sass) implementation.
|
|---|
| 337 |
|
|---|
| 338 | > ℹ️ The `charset` option has `true` value by default for `dart-sass`, we strongly discourage change value to `false`, because webpack doesn't support files other than `utf-8`.
|
|---|
| 339 |
|
|---|
| 340 | > ℹ️ The `indentedSyntax` option has `true` value for the `sass` extension.
|
|---|
| 341 |
|
|---|
| 342 | > ℹ️ Options such as `data` and `file` are unavailable and will be ignored.
|
|---|
| 343 |
|
|---|
| 344 | > ℹ We strongly discourage change `outFile`, `sourceMapContents`, `sourceMapEmbed`, `sourceMapRoot` options because `sass-loader` automatically sets these options when the `sourceMap` option is `true`.
|
|---|
| 345 |
|
|---|
| 346 | > ℹ️ Access to the [loader context](https://webpack.js.org/api/loaders/#the-loader-context) inside the custom importer can be done using the `this.webpackLoaderContext` property.
|
|---|
| 347 |
|
|---|
| 348 | There is a slight difference between the `sass` (`dart-sass`) and `node-sass` options.
|
|---|
| 349 |
|
|---|
| 350 | Please consult documentation before using them:
|
|---|
| 351 |
|
|---|
| 352 | - [Dart Sass documentation](https://github.com/sass/dart-sass#javascript-api) for all available `sass` options.
|
|---|
| 353 | - [Node Sass documentation](https://github.com/sass/node-sass/#options) for all available `node-sass` options.
|
|---|
| 354 |
|
|---|
| 355 | #### `object`
|
|---|
| 356 |
|
|---|
| 357 | Use an object for the Sass implementation setup.
|
|---|
| 358 |
|
|---|
| 359 | **webpack.config.js**
|
|---|
| 360 |
|
|---|
| 361 | ```js
|
|---|
| 362 | module.exports = {
|
|---|
| 363 | module: {
|
|---|
| 364 | rules: [
|
|---|
| 365 | {
|
|---|
| 366 | test: /\.s[ac]ss$/i,
|
|---|
| 367 | use: [
|
|---|
| 368 | "style-loader",
|
|---|
| 369 | "css-loader",
|
|---|
| 370 | {
|
|---|
| 371 | loader: "sass-loader",
|
|---|
| 372 | options: {
|
|---|
| 373 | sassOptions: {
|
|---|
| 374 | indentWidth: 4,
|
|---|
| 375 | includePaths: ["absolute/path/a", "absolute/path/b"],
|
|---|
| 376 | },
|
|---|
| 377 | },
|
|---|
| 378 | },
|
|---|
| 379 | ],
|
|---|
| 380 | },
|
|---|
| 381 | ],
|
|---|
| 382 | },
|
|---|
| 383 | };
|
|---|
| 384 | ```
|
|---|
| 385 |
|
|---|
| 386 | #### `function`
|
|---|
| 387 |
|
|---|
| 388 | Allows to setup the Sass implementation by setting different options based on the loader context.
|
|---|
| 389 |
|
|---|
| 390 | ```js
|
|---|
| 391 | module.exports = {
|
|---|
| 392 | module: {
|
|---|
| 393 | rules: [
|
|---|
| 394 | {
|
|---|
| 395 | test: /\.s[ac]ss$/i,
|
|---|
| 396 | use: [
|
|---|
| 397 | "style-loader",
|
|---|
| 398 | "css-loader",
|
|---|
| 399 | {
|
|---|
| 400 | loader: "sass-loader",
|
|---|
| 401 | options: {
|
|---|
| 402 | sassOptions: (loaderContext) => {
|
|---|
| 403 | // More information about available properties https://webpack.js.org/api/loaders/
|
|---|
| 404 | const { resourcePath, rootContext } = loaderContext;
|
|---|
| 405 | const relativePath = path.relative(rootContext, resourcePath);
|
|---|
| 406 |
|
|---|
| 407 | if (relativePath === "styles/foo.scss") {
|
|---|
| 408 | return {
|
|---|
| 409 | includePaths: ["absolute/path/c", "absolute/path/d"],
|
|---|
| 410 | };
|
|---|
| 411 | }
|
|---|
| 412 |
|
|---|
| 413 | return {
|
|---|
| 414 | includePaths: ["absolute/path/a", "absolute/path/b"],
|
|---|
| 415 | };
|
|---|
| 416 | },
|
|---|
| 417 | },
|
|---|
| 418 | },
|
|---|
| 419 | ],
|
|---|
| 420 | },
|
|---|
| 421 | ],
|
|---|
| 422 | },
|
|---|
| 423 | };
|
|---|
| 424 | ```
|
|---|
| 425 |
|
|---|
| 426 | ### `sourceMap`
|
|---|
| 427 |
|
|---|
| 428 | Type:
|
|---|
| 429 |
|
|---|
| 430 | ```ts
|
|---|
| 431 | type sourceMap = boolean;
|
|---|
| 432 | ```
|
|---|
| 433 |
|
|---|
| 434 | Default: depends on the `compiler.devtool` value
|
|---|
| 435 |
|
|---|
| 436 | Enables/Disables generation of source maps.
|
|---|
| 437 |
|
|---|
| 438 | By default generation of source maps depends on the [`devtool`](https://webpack.js.org/configuration/devtool/) option.
|
|---|
| 439 | All values enable source map generation except `eval` and `false` value.
|
|---|
| 440 |
|
|---|
| 441 | > ℹ If a `true` the `sourceMap`, `sourceMapRoot`, `sourceMapEmbed`, `sourceMapContents` and `omitSourceMapUrl` from `sassOptions` will be ignored.
|
|---|
| 442 |
|
|---|
| 443 | **webpack.config.js**
|
|---|
| 444 |
|
|---|
| 445 | ```js
|
|---|
| 446 | module.exports = {
|
|---|
| 447 | module: {
|
|---|
| 448 | rules: [
|
|---|
| 449 | {
|
|---|
| 450 | test: /\.s[ac]ss$/i,
|
|---|
| 451 | use: [
|
|---|
| 452 | "style-loader",
|
|---|
| 453 | {
|
|---|
| 454 | loader: "css-loader",
|
|---|
| 455 | options: {
|
|---|
| 456 | sourceMap: true,
|
|---|
| 457 | },
|
|---|
| 458 | },
|
|---|
| 459 | {
|
|---|
| 460 | loader: "sass-loader",
|
|---|
| 461 | options: {
|
|---|
| 462 | sourceMap: true,
|
|---|
| 463 | },
|
|---|
| 464 | },
|
|---|
| 465 | ],
|
|---|
| 466 | },
|
|---|
| 467 | ],
|
|---|
| 468 | },
|
|---|
| 469 | };
|
|---|
| 470 | ```
|
|---|
| 471 |
|
|---|
| 472 | > ℹ In some rare cases `node-sass` can output invalid source maps (it is a `node-sass` bug).
|
|---|
| 473 |
|
|---|
| 474 | > > In order to avoid this, you can try to update `node-sass` to latest version or you can try to set within `sassOptions` the `outputStyle` option to `compressed`.
|
|---|
| 475 |
|
|---|
| 476 | **webpack.config.js**
|
|---|
| 477 |
|
|---|
| 478 | ```js
|
|---|
| 479 | module.exports = {
|
|---|
| 480 | module: {
|
|---|
| 481 | rules: [
|
|---|
| 482 | {
|
|---|
| 483 | test: /\.s[ac]ss$/i,
|
|---|
| 484 | use: [
|
|---|
| 485 | "style-loader",
|
|---|
| 486 | "css-loader",
|
|---|
| 487 | {
|
|---|
| 488 | loader: "sass-loader",
|
|---|
| 489 | options: {
|
|---|
| 490 | sourceMap: true,
|
|---|
| 491 | sassOptions: {
|
|---|
| 492 | outputStyle: "compressed",
|
|---|
| 493 | },
|
|---|
| 494 | },
|
|---|
| 495 | },
|
|---|
| 496 | ],
|
|---|
| 497 | },
|
|---|
| 498 | ],
|
|---|
| 499 | },
|
|---|
| 500 | };
|
|---|
| 501 | ```
|
|---|
| 502 |
|
|---|
| 503 | ### `additionalData`
|
|---|
| 504 |
|
|---|
| 505 | Type:
|
|---|
| 506 |
|
|---|
| 507 | ```ts
|
|---|
| 508 | type additionalData =
|
|---|
| 509 | | string
|
|---|
| 510 | | ((content: string | Buffer, loaderContext: LoaderContext) => string);
|
|---|
| 511 | ```
|
|---|
| 512 |
|
|---|
| 513 | Default: `undefined`
|
|---|
| 514 |
|
|---|
| 515 | Prepends `Sass`/`SCSS` code before the actual entry file.
|
|---|
| 516 | In this case, the `sass-loader` will not override the `data` option but just **prepend** the entry's content.
|
|---|
| 517 |
|
|---|
| 518 | This is especially useful when some of your Sass variables depend on the environment:
|
|---|
| 519 |
|
|---|
| 520 | #### `string`
|
|---|
| 521 |
|
|---|
| 522 | ```js
|
|---|
| 523 | module.exports = {
|
|---|
| 524 | module: {
|
|---|
| 525 | rules: [
|
|---|
| 526 | {
|
|---|
| 527 | test: /\.s[ac]ss$/i,
|
|---|
| 528 | use: [
|
|---|
| 529 | "style-loader",
|
|---|
| 530 | "css-loader",
|
|---|
| 531 | {
|
|---|
| 532 | loader: "sass-loader",
|
|---|
| 533 | options: {
|
|---|
| 534 | additionalData: "$env: " + process.env.NODE_ENV + ";",
|
|---|
| 535 | },
|
|---|
| 536 | },
|
|---|
| 537 | ],
|
|---|
| 538 | },
|
|---|
| 539 | ],
|
|---|
| 540 | },
|
|---|
| 541 | };
|
|---|
| 542 | ```
|
|---|
| 543 |
|
|---|
| 544 | #### `function`
|
|---|
| 545 |
|
|---|
| 546 | ##### Sync
|
|---|
| 547 |
|
|---|
| 548 | ```js
|
|---|
| 549 | module.exports = {
|
|---|
| 550 | module: {
|
|---|
| 551 | rules: [
|
|---|
| 552 | {
|
|---|
| 553 | test: /\.s[ac]ss$/i,
|
|---|
| 554 | use: [
|
|---|
| 555 | "style-loader",
|
|---|
| 556 | "css-loader",
|
|---|
| 557 | {
|
|---|
| 558 | loader: "sass-loader",
|
|---|
| 559 | options: {
|
|---|
| 560 | additionalData: (content, loaderContext) => {
|
|---|
| 561 | // More information about available properties https://webpack.js.org/api/loaders/
|
|---|
| 562 | const { resourcePath, rootContext } = loaderContext;
|
|---|
| 563 | const relativePath = path.relative(rootContext, resourcePath);
|
|---|
| 564 |
|
|---|
| 565 | if (relativePath === "styles/foo.scss") {
|
|---|
| 566 | return "$value: 100px;" + content;
|
|---|
| 567 | }
|
|---|
| 568 |
|
|---|
| 569 | return "$value: 200px;" + content;
|
|---|
| 570 | },
|
|---|
| 571 | },
|
|---|
| 572 | },
|
|---|
| 573 | ],
|
|---|
| 574 | },
|
|---|
| 575 | ],
|
|---|
| 576 | },
|
|---|
| 577 | };
|
|---|
| 578 | ```
|
|---|
| 579 |
|
|---|
| 580 | ##### Async
|
|---|
| 581 |
|
|---|
| 582 | ```js
|
|---|
| 583 | module.exports = {
|
|---|
| 584 | module: {
|
|---|
| 585 | rules: [
|
|---|
| 586 | {
|
|---|
| 587 | test: /\.s[ac]ss$/i,
|
|---|
| 588 | use: [
|
|---|
| 589 | "style-loader",
|
|---|
| 590 | "css-loader",
|
|---|
| 591 | {
|
|---|
| 592 | loader: "sass-loader",
|
|---|
| 593 | options: {
|
|---|
| 594 | additionalData: async (content, loaderContext) => {
|
|---|
| 595 | // More information about available properties https://webpack.js.org/api/loaders/
|
|---|
| 596 | const { resourcePath, rootContext } = loaderContext;
|
|---|
| 597 | const relativePath = path.relative(rootContext, resourcePath);
|
|---|
| 598 |
|
|---|
| 599 | if (relativePath === "styles/foo.scss") {
|
|---|
| 600 | return "$value: 100px;" + content;
|
|---|
| 601 | }
|
|---|
| 602 |
|
|---|
| 603 | return "$value: 200px;" + content;
|
|---|
| 604 | },
|
|---|
| 605 | },
|
|---|
| 606 | },
|
|---|
| 607 | ],
|
|---|
| 608 | },
|
|---|
| 609 | ],
|
|---|
| 610 | },
|
|---|
| 611 | };
|
|---|
| 612 | ```
|
|---|
| 613 |
|
|---|
| 614 | ### `webpackImporter`
|
|---|
| 615 |
|
|---|
| 616 | Type:
|
|---|
| 617 |
|
|---|
| 618 | ```ts
|
|---|
| 619 | type webpackImporter = boolean;
|
|---|
| 620 | ```
|
|---|
| 621 |
|
|---|
| 622 | Default: `true`
|
|---|
| 623 |
|
|---|
| 624 | Enables/Disables the default Webpack importer.
|
|---|
| 625 |
|
|---|
| 626 | This can improve performance in some cases. Use it with caution because aliases and `@import` at-rules starting with `~` will not work.
|
|---|
| 627 | You can pass own `importer` to solve this (see [`importer docs`](https://github.com/sass/node-sass#importer--v200---experimental)).
|
|---|
| 628 |
|
|---|
| 629 | **webpack.config.js**
|
|---|
| 630 |
|
|---|
| 631 | ```js
|
|---|
| 632 | module.exports = {
|
|---|
| 633 | module: {
|
|---|
| 634 | rules: [
|
|---|
| 635 | {
|
|---|
| 636 | test: /\.s[ac]ss$/i,
|
|---|
| 637 | use: [
|
|---|
| 638 | "style-loader",
|
|---|
| 639 | "css-loader",
|
|---|
| 640 | {
|
|---|
| 641 | loader: "sass-loader",
|
|---|
| 642 | options: {
|
|---|
| 643 | webpackImporter: false,
|
|---|
| 644 | },
|
|---|
| 645 | },
|
|---|
| 646 | ],
|
|---|
| 647 | },
|
|---|
| 648 | ],
|
|---|
| 649 | },
|
|---|
| 650 | };
|
|---|
| 651 | ```
|
|---|
| 652 |
|
|---|
| 653 | ### `warnRuleAsWarning`
|
|---|
| 654 |
|
|---|
| 655 | Type:
|
|---|
| 656 |
|
|---|
| 657 | ```ts
|
|---|
| 658 | type warnRuleAsWarning = boolean;
|
|---|
| 659 | ```
|
|---|
| 660 |
|
|---|
| 661 | Default: `false`
|
|---|
| 662 |
|
|---|
| 663 | Treats the `@warn` rule as a webpack warning.
|
|---|
| 664 |
|
|---|
| 665 | > ℹ️ It will be `true` by default in the next major release.
|
|---|
| 666 |
|
|---|
| 667 | **style.scss**
|
|---|
| 668 |
|
|---|
| 669 | ```scss
|
|---|
| 670 | $known-prefixes: webkit, moz, ms, o;
|
|---|
| 671 |
|
|---|
| 672 | @mixin prefix($property, $value, $prefixes) {
|
|---|
| 673 | @each $prefix in $prefixes {
|
|---|
| 674 | @if not index($known-prefixes, $prefix) {
|
|---|
| 675 | @warn "Unknown prefix #{$prefix}.";
|
|---|
| 676 | }
|
|---|
| 677 |
|
|---|
| 678 | -#{$prefix}-#{$property}: $value;
|
|---|
| 679 | }
|
|---|
| 680 | #{$property}: $value;
|
|---|
| 681 | }
|
|---|
| 682 |
|
|---|
| 683 | .tilt {
|
|---|
| 684 | // Oops, we typo'd "webkit" as "wekbit"!
|
|---|
| 685 | @include prefix(transform, rotate(15deg), wekbit ms);
|
|---|
| 686 | }
|
|---|
| 687 | ```
|
|---|
| 688 |
|
|---|
| 689 | The presented code will throw webpack warning instead logging.
|
|---|
| 690 |
|
|---|
| 691 | To ignore unnecessary warnings you can use the [ignoreWarnings](https://webpack.js.org/configuration/other-options/#ignorewarnings) option.
|
|---|
| 692 |
|
|---|
| 693 | **webpack.config.js**
|
|---|
| 694 |
|
|---|
| 695 | ```js
|
|---|
| 696 | module.exports = {
|
|---|
| 697 | module: {
|
|---|
| 698 | rules: [
|
|---|
| 699 | {
|
|---|
| 700 | test: /\.s[ac]ss$/i,
|
|---|
| 701 | use: [
|
|---|
| 702 | "style-loader",
|
|---|
| 703 | "css-loader",
|
|---|
| 704 | {
|
|---|
| 705 | loader: "sass-loader",
|
|---|
| 706 | options: {
|
|---|
| 707 | warnRuleAsWarning: true,
|
|---|
| 708 | },
|
|---|
| 709 | },
|
|---|
| 710 | ],
|
|---|
| 711 | },
|
|---|
| 712 | ],
|
|---|
| 713 | },
|
|---|
| 714 | };
|
|---|
| 715 | ```
|
|---|
| 716 |
|
|---|
| 717 | ### `api`
|
|---|
| 718 |
|
|---|
| 719 | Type:
|
|---|
| 720 |
|
|---|
| 721 | ```ts
|
|---|
| 722 | type api = "legacy" | "modern";
|
|---|
| 723 | ```
|
|---|
| 724 |
|
|---|
| 725 | Default: `"legacy"`
|
|---|
| 726 |
|
|---|
| 727 | Allows you to switch between `legacy` and `modern` API. You can find more information [here](https://sass-lang.com/documentation/js-api).
|
|---|
| 728 |
|
|---|
| 729 | > ⚠ "modern" API is experimental, so some features may not work (known: built-in `importer` is not working and files with errors is not watching on initial run), you can follow this [here](https://github.com/webpack-contrib/sass-loader/issues/774).
|
|---|
| 730 |
|
|---|
| 731 | > ⚠ The sass options are different for `modern` and `old` APIs. Please look at [docs](https://sass-lang.com/documentation/js-api) how to migrate on new options.
|
|---|
| 732 |
|
|---|
| 733 | **webpack.config.js**
|
|---|
| 734 |
|
|---|
| 735 | ```js
|
|---|
| 736 | module.exports = {
|
|---|
| 737 | module: {
|
|---|
| 738 | rules: [
|
|---|
| 739 | {
|
|---|
| 740 | test: /\.s[ac]ss$/i,
|
|---|
| 741 | use: [
|
|---|
| 742 | "style-loader",
|
|---|
| 743 | "css-loader",
|
|---|
| 744 | {
|
|---|
| 745 | loader: "sass-loader",
|
|---|
| 746 | options: {
|
|---|
| 747 | api: "modern",
|
|---|
| 748 | sassOptions: {
|
|---|
| 749 | // Your sass options
|
|---|
| 750 | },
|
|---|
| 751 | },
|
|---|
| 752 | },
|
|---|
| 753 | ],
|
|---|
| 754 | },
|
|---|
| 755 | ],
|
|---|
| 756 | },
|
|---|
| 757 | };
|
|---|
| 758 | ```
|
|---|
| 759 |
|
|---|
| 760 | ## Examples
|
|---|
| 761 |
|
|---|
| 762 | ### Extracts CSS into separate files
|
|---|
| 763 |
|
|---|
| 764 | For production builds it's recommended to extract the CSS from your bundle being able to use parallel loading of CSS/JS resources later on.
|
|---|
| 765 |
|
|---|
| 766 | There are two possibilities to extract a style sheet from the bundle:
|
|---|
| 767 |
|
|---|
| 768 | - [mini-css-extract-plugin](https://github.com/webpack-contrib/mini-css-extract-plugin)
|
|---|
| 769 | - [extract-loader](https://github.com/peerigon/extract-loader) (simpler, but specialized on the css-loader's output)
|
|---|
| 770 |
|
|---|
| 771 | **webpack.config.js**
|
|---|
| 772 |
|
|---|
| 773 | ```js
|
|---|
| 774 | const MiniCssExtractPlugin = require("mini-css-extract-plugin");
|
|---|
| 775 |
|
|---|
| 776 | module.exports = {
|
|---|
| 777 | module: {
|
|---|
| 778 | rules: [
|
|---|
| 779 | {
|
|---|
| 780 | test: /\.s[ac]ss$/i,
|
|---|
| 781 | use: [
|
|---|
| 782 | // fallback to style-loader in development
|
|---|
| 783 | process.env.NODE_ENV !== "production"
|
|---|
| 784 | ? "style-loader"
|
|---|
| 785 | : MiniCssExtractPlugin.loader,
|
|---|
| 786 | "css-loader",
|
|---|
| 787 | "sass-loader",
|
|---|
| 788 | ],
|
|---|
| 789 | },
|
|---|
| 790 | ],
|
|---|
| 791 | },
|
|---|
| 792 | plugins: [
|
|---|
| 793 | new MiniCssExtractPlugin({
|
|---|
| 794 | // Options similar to the same options in webpackOptions.output
|
|---|
| 795 | // both options are optional
|
|---|
| 796 | filename: "[name].css",
|
|---|
| 797 | chunkFilename: "[id].css",
|
|---|
| 798 | }),
|
|---|
| 799 | ],
|
|---|
| 800 | };
|
|---|
| 801 | ```
|
|---|
| 802 |
|
|---|
| 803 | ### Source maps
|
|---|
| 804 |
|
|---|
| 805 | Enables/Disables generation of source maps.
|
|---|
| 806 |
|
|---|
| 807 | To enable CSS source maps, you'll need to pass the `sourceMap` option to the `sass-loader` _and_ the css-loader.
|
|---|
| 808 |
|
|---|
| 809 | **webpack.config.js**
|
|---|
| 810 |
|
|---|
| 811 | ```javascript
|
|---|
| 812 | module.exports = {
|
|---|
| 813 | devtool: "source-map", // any "source-map"-like devtool is possible
|
|---|
| 814 | module: {
|
|---|
| 815 | rules: [
|
|---|
| 816 | {
|
|---|
| 817 | test: /\.s[ac]ss$/i,
|
|---|
| 818 | use: [
|
|---|
| 819 | "style-loader",
|
|---|
| 820 | {
|
|---|
| 821 | loader: "css-loader",
|
|---|
| 822 | options: {
|
|---|
| 823 | sourceMap: true,
|
|---|
| 824 | },
|
|---|
| 825 | },
|
|---|
| 826 | {
|
|---|
| 827 | loader: "sass-loader",
|
|---|
| 828 | options: {
|
|---|
| 829 | sourceMap: true,
|
|---|
| 830 | },
|
|---|
| 831 | },
|
|---|
| 832 | ],
|
|---|
| 833 | },
|
|---|
| 834 | ],
|
|---|
| 835 | },
|
|---|
| 836 | };
|
|---|
| 837 | ```
|
|---|
| 838 |
|
|---|
| 839 | 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.
|
|---|
| 840 |
|
|---|
| 841 | ## Contributing
|
|---|
| 842 |
|
|---|
| 843 | Please take a moment to read our contributing guidelines if you haven't yet done so.
|
|---|
| 844 |
|
|---|
| 845 | [CONTRIBUTING](./.github/CONTRIBUTING.md)
|
|---|
| 846 |
|
|---|
| 847 | ## License
|
|---|
| 848 |
|
|---|
| 849 | [MIT](./LICENSE)
|
|---|
| 850 |
|
|---|
| 851 | [npm]: https://img.shields.io/npm/v/sass-loader.svg
|
|---|
| 852 | [npm-url]: https://npmjs.com/package/sass-loader
|
|---|
| 853 | [node]: https://img.shields.io/node/v/sass-loader.svg
|
|---|
| 854 | [node-url]: https://nodejs.org
|
|---|
| 855 | [deps]: https://david-dm.org/webpack-contrib/sass-loader.svg
|
|---|
| 856 | [deps-url]: https://david-dm.org/webpack-contrib/sass-loader
|
|---|
| 857 | [tests]: https://github.com/webpack-contrib/sass-loader/workflows/sass-loader/badge.svg
|
|---|
| 858 | [tests-url]: https://github.com/webpack-contrib/sass-loader/actions
|
|---|
| 859 | [cover]: https://codecov.io/gh/webpack-contrib/sass-loader/branch/master/graph/badge.svg
|
|---|
| 860 | [cover-url]: https://codecov.io/gh/webpack-contrib/sass-loader
|
|---|
| 861 | [chat]: https://badges.gitter.im/webpack/webpack.svg
|
|---|
| 862 | [chat-url]: https://gitter.im/webpack/webpack
|
|---|
| 863 | [size]: https://packagephobia.now.sh/badge?p=sass-loader
|
|---|
| 864 | [size-url]: https://packagephobia.now.sh/result?p=sass-loader
|
|---|