Gathering detailed insights and metrics for sass-loader
Gathering detailed insights and metrics for sass-loader
Gathering detailed insights and metrics for sass-loader
Gathering detailed insights and metrics for sass-loader
npm install sass-loader
Typescript
Module System
Min. Node Version
Node Version
NPM Version
55.4
Supply Chain
64.7
Quality
85.8
Maintenance
100
Vulnerability
89.3
License
Updated on 04 Dec 2024
Minified
Minified + Gzipped
JavaScript (79.99%)
SCSS (8.95%)
Sass (8.91%)
HTML (1.84%)
CSS (0.31%)
Cumulative downloads
Total Downloads
Last day
19.8%
Compared to previous day
Last week
0.4%
Compared to previous week
Last month
1.9%
Compared to previous month
Last year
1.3%
Compared to previous year
1
5
39
Loads a Sass/SCSS file and compiles it to CSS.
To begin, you'll need to install sass-loader
:
1npm install sass-loader sass webpack --save-dev
or
1yarn add -D sass-loader sass webpack
or
1pnpm add -D sass-loader sass webpack
[!NOTE]
To enable CSS processing in your project, you need to install style-loader and css-loader via
npm i style-loader css-loader
.
sass-loader
requires you to install either Dart Sass, Node Sass on your own (more documentation can be found below) or Sass Embedded.
This allows you to control the versions of all your dependencies, and to choose which Sass implementation to use.
[!NOTE]
We highly recommend using Sass Embedded or Dart Sass.
[!WARNING]
Node Sass does not work with Yarn PnP and doesn't support @use rule.
Chain the sass-loader
with the css-loader and the style-loader to immediately apply all styles to the DOM or the mini-css-extract-plugin to extract it into a separate file.
Then add the loader to your webpack configuration. For example:
app.js
1import "./style.scss";
style.scss
1$body-color: red; 2 3body { 4 color: $body-color; 5}
webpack.config.js
1module.exports = { 2 module: { 3 rules: [ 4 { 5 test: /\.s[ac]ss$/i, 6 use: [ 7 // Creates `style` nodes from JS strings 8 "style-loader", 9 // Translates CSS into CommonJS 10 "css-loader", 11 // Compiles Sass to CSS 12 "sass-loader", 13 ], 14 }, 15 ], 16 }, 17};
Finally run webpack
via your preferred method.
style
(new API, by default since 16 version) and outputStyle
(old API) options in production
modeFor production
mode, the style
(new API, by default since 16 version) and outputStyle
(old API) options default to compressed
unless otherwise specified in sassOptions
.
import
and use
at-rulesWebpack provides an advanced mechanism to resolve files.
The sass-loader
uses Sass's custom importer feature to pass all queries to the webpack resolving engine enabling you to import your Sass modules from node_modules
.
1@import "bootstrap";
Using ~
is deprecated and should be removed from your code, but we still support it for historical reasons.
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
.
Prepending module paths with a ~
tells webpack to search through node_modules
.
1@import "~bootstrap";
It's important to prepend the path with only ~
, 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 "style.scss"
is the same as @import "./style.scss";
url(...)
Since Sass implementations don't provide url rewriting, all linked assets must be relative to the output.
css-loader
, all urls must be relative to the entry-file (e.g. main.scss
).css-loader
, it must be relative to your web root.You might be surprised by this first issue, as 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).
Thankfully there are two solutions to this problem:
sass-loader
in the loader chain.$icon-font-path
.implementation
Type:
1type implementation = object | string;
Default: sass
The special implementation
option determines which implementation of Sass to use.
By default, the loader resolves the implementation based on your dependencies.
Just add the desired implementation to your package.json
(sass
, sass-embedded
, or node-sass
package) and install dependencies.
Example where the sass-loader
loader uses the sass
(dart-sass
) implementation:
package.json
1{ 2 "devDependencies": { 3 "sass-loader": "^7.2.0", 4 "sass": "^1.22.10" 5 } 6}
Example where the sass-loader
loader uses the node-sass
implementation:
package.json
1{ 2 "devDependencies": { 3 "sass-loader": "^7.2.0", 4 "node-sass": "^5.0.0" 5 } 6}
Example where the sass-loader
loader uses the sass-embedded
implementation:
package.json
1{ 2 "devDependencies": { 3 "sass-loader": "^7.2.0", 4 "sass": "^1.22.10" 5 }, 6 "optionalDependencies": { 7 "sass-embedded": "^1.70.0" 8 } 9}
[!NOTE]
Using
optionalDependencies
means thatsass-loader
can fallback tosass
when running on an operating system not supported bysass-embedded
Be aware of the order that sass-loader
will resolve the implementation:
sass-embedded
sass
node-sass
You can specify a specific implementation by using the implementation
option, which accepts one of the above values.
object
For example, to always use Dart Sass, you'd pass:
1module.exports = { 2 module: { 3 rules: [ 4 { 5 test: /\.s[ac]ss$/i, 6 use: [ 7 "style-loader", 8 "css-loader", 9 { 10 loader: "sass-loader", 11 options: { 12 // Prefer `dart-sass`, even if `sass-embedded` is available 13 implementation: require("sass"), 14 }, 15 }, 16 ], 17 }, 18 ], 19 }, 20};
string
For example, to use Dart Sass, you'd pass:
1module.exports = { 2 module: { 3 rules: [ 4 { 5 test: /\.s[ac]ss$/i, 6 use: [ 7 "style-loader", 8 "css-loader", 9 { 10 loader: "sass-loader", 11 options: { 12 // Prefer `dart-sass`, even if `sass-embedded` is available 13 implementation: require.resolve("sass"), 14 }, 15 }, 16 ], 17 }, 18 ], 19 }, 20};
sassOptions
Type:
1type sassOptions = 2 | import("sass").LegacyOptions<"async"> 3 | (( 4 content: string | Buffer, 5 loaderContext: LoaderContext, 6 meta: any, 7 ) => import("sass").LegacyOptions<"async">);
Default: defaults values for Sass implementation
Options for Dart Sass or Node Sass implementation.
[!NOTE]
The
charset
option istrue
by default fordart-sass
, we strongly discourage setting this tofalse
, because webpack doesn't support files other thanutf-8
.
[!NOTE]
The
syntax
(new API, by default since 16 version)and
indentedSyntax(old API) option is
scssfor the
scssextension,
indentedfor the
sassextension and
cssfor the
css` extension.
[!NOTE]
Options such as
data
andfile
are unavailable and will be ignored.
ℹ We strongly discourage changing the
sourceMap
(new API, by default since 16 version),outFile
(old API),sourceMapContents
(old API),sourceMapEmbed
(old API), andsourceMapRoot
(old API) options becausesass-loader
sets these automatically when thesourceMap
option istrue
.
[!NOTE]
Access to the loader context inside the custom importer can be done using the
this.webpackLoaderContext
property.
There is a slight difference between the options for sass
(dart-sass
) and node-sass
.
Please consult their respective documentation before using them:
sass
options.sass
options.node-sass
options.object
Use an object for the Sass implementation setup.
webpack.config.js
1module.exports = { 2 module: { 3 rules: [ 4 { 5 test: /\.s[ac]ss$/i, 6 use: [ 7 "style-loader", 8 "css-loader", 9 { 10 loader: "sass-loader", 11 options: { 12 sassOptions: { 13 style: `compressed`, 14 loadPaths: ["absolute/path/a", "absolute/path/b"], 15 }, 16 }, 17 }, 18 ], 19 }, 20 ], 21 }, 22};
function
Allows configuring the Sass implementation with different options based on the loader context.
1module.exports = { 2 module: { 3 rules: [ 4 { 5 test: /\.s[ac]ss$/i, 6 use: [ 7 "style-loader", 8 "css-loader", 9 { 10 loader: "sass-loader", 11 options: { 12 sassOptions: (loaderContext) => { 13 // More information about available properties https://webpack.js.org/api/loaders/ 14 const { resourcePath, rootContext } = loaderContext; 15 const relativePath = path.relative(rootContext, resourcePath); 16 17 if (relativePath === "styles/foo.scss") { 18 return { 19 loadPaths: ["absolute/path/c", "absolute/path/d"], 20 }; 21 } 22 23 return { 24 loadPaths: ["absolute/path/a", "absolute/path/b"], 25 }; 26 }, 27 }, 28 }, 29 ], 30 }, 31 ], 32 }, 33};
sourceMap
Type:
1type sourceMap = boolean;
Default: depends on the compiler.devtool
value
Enables/Disables generation of source maps.
By default generation of source maps depends on the devtool
option.
All values enable source map generation except eval
and false
.
ℹ If
true
, thesourceMap
(new API, by default since 16 version),outFile
(old API),sourceMapContents
(old API),sourceMapEmbed
(old API), andsourceMapRoot
(old API) fromsassOptions
will be ignored.
webpack.config.js
1module.exports = { 2 module: { 3 rules: [ 4 { 5 test: /\.s[ac]ss$/i, 6 use: [ 7 "style-loader", 8 { 9 loader: "css-loader", 10 options: { 11 sourceMap: true, 12 }, 13 }, 14 { 15 loader: "sass-loader", 16 options: { 17 sourceMap: true, 18 }, 19 }, 20 ], 21 }, 22 ], 23 }, 24};
ℹ In some rare cases
node-sass
can output invalid source maps (it is anode-sass
bug).In order to avoid this, you can try to update
node-sass
to latest version, or you can try to set withinsassOptions
theoutputStyle
option tocompressed
.
webpack.config.js
1module.exports = { 2 module: { 3 rules: [ 4 { 5 test: /\.s[ac]ss$/i, 6 use: [ 7 "style-loader", 8 "css-loader", 9 { 10 loader: "sass-loader", 11 options: { 12 sourceMap: true, 13 sassOptions: { 14 outputStyle: "compressed", 15 }, 16 }, 17 }, 18 ], 19 }, 20 ], 21 }, 22};
additionalData
Type:
1type additionalData = 2 | string 3 | ((content: string | Buffer, loaderContext: LoaderContext) => string);
Default: undefined
Prepends Sass
/SCSS
code before the actual entry file.
In this case, the sass-loader
will not override the data
option but just prepend the entry's content.
This is especially useful when some of your Sass variables depend on the environment:
string
1module.exports = { 2 module: { 3 rules: [ 4 { 5 test: /\.s[ac]ss$/i, 6 use: [ 7 "style-loader", 8 "css-loader", 9 { 10 loader: "sass-loader", 11 options: { 12 additionalData: "$env: " + process.env.NODE_ENV + ";", 13 }, 14 }, 15 ], 16 }, 17 ], 18 }, 19};
function
1module.exports = { 2 module: { 3 rules: [ 4 { 5 test: /\.s[ac]ss$/i, 6 use: [ 7 "style-loader", 8 "css-loader", 9 { 10 loader: "sass-loader", 11 options: { 12 additionalData: (content, loaderContext) => { 13 // More information about available properties https://webpack.js.org/api/loaders/ 14 const { resourcePath, rootContext } = loaderContext; 15 const relativePath = path.relative(rootContext, resourcePath); 16 17 if (relativePath === "styles/foo.scss") { 18 return "$value: 100px;" + content; 19 } 20 21 return "$value: 200px;" + content; 22 }, 23 }, 24 }, 25 ], 26 }, 27 ], 28 }, 29};
1module.exports = { 2 module: { 3 rules: [ 4 { 5 test: /\.s[ac]ss$/i, 6 use: [ 7 "style-loader", 8 "css-loader", 9 { 10 loader: "sass-loader", 11 options: { 12 additionalData: async (content, loaderContext) => { 13 // More information about available properties https://webpack.js.org/api/loaders/ 14 const { resourcePath, rootContext } = loaderContext; 15 const relativePath = path.relative(rootContext, resourcePath); 16 17 if (relativePath === "styles/foo.scss") { 18 return "$value: 100px;" + content; 19 } 20 21 return "$value: 200px;" + content; 22 }, 23 }, 24 }, 25 ], 26 }, 27 ], 28 }, 29};
webpackImporter
Type:
1type webpackImporter = boolean;
Default: true
Enables/Disables the default webpack importer.
This can improve performance in some cases, though use it with caution because aliases and @import
at-rules starting with ~
will not work.
You can pass your own importer
to solve this (see importer docs
).
webpack.config.js
1module.exports = { 2 module: { 3 rules: [ 4 { 5 test: /\.s[ac]ss$/i, 6 use: [ 7 "style-loader", 8 "css-loader", 9 { 10 loader: "sass-loader", 11 options: { 12 webpackImporter: false, 13 }, 14 }, 15 ], 16 }, 17 ], 18 }, 19};
warnRuleAsWarning
Type:
1type warnRuleAsWarning = boolean;
Default: true
Treats the @warn
rule as a webpack warning.
style.scss
1$known-prefixes: webkit, moz, ms, o; 2 3@mixin prefix($property, $value, $prefixes) { 4 @each $prefix in $prefixes { 5 @if not index($known-prefixes, $prefix) { 6 @warn "Unknown prefix #{$prefix}."; 7 } 8 9 -#{$prefix}-#{$property}: $value; 10 } 11 #{$property}: $value; 12} 13 14.tilt { 15 // Oops, we typo'd "webkit" as "wekbit"! 16 @include prefix(transform, rotate(15deg), wekbit ms); 17}
The presented code will throw a webpack warning instead logging.
To ignore unnecessary warnings you can use the ignoreWarnings option.
webpack.config.js
1module.exports = { 2 module: { 3 rules: [ 4 { 5 test: /\.s[ac]ss$/i, 6 use: [ 7 "style-loader", 8 "css-loader", 9 { 10 loader: "sass-loader", 11 options: { 12 warnRuleAsWarning: true, 13 }, 14 }, 15 ], 16 }, 17 ], 18 }, 19};
api
Type:
1type api = "legacy" | "modern" | "modern-compiler";
Default: "modern"
for sass
(dart-sass
) and sass-embedded
, or "legacy"
for node-sass
Allows you to switch between the legacy
and modern
APIs. You can find more information here. The modern-compiler
option enables the modern API with support for Shared Resources.
[!NOTE]
Using
modern-compiler
andsass-embedded
together significantly improve performance and decrease built time. We strongly recommend their use. We will enable them by default in a future major release.
[!WARNING]
The sass options are different for the
legacy
andmodern
APIs. Please look at docs how to migrate to the modern options.
webpack.config.js
1module.exports = { 2 module: { 3 rules: [ 4 { 5 test: /\.s[ac]ss$/i, 6 use: [ 7 "style-loader", 8 "css-loader", 9 { 10 loader: "sass-loader", 11 options: { 12 api: "modern-compiler", 13 sassOptions: { 14 // Your sass options 15 }, 16 }, 17 }, 18 ], 19 }, 20 ], 21 }, 22};
@debug
outputBy default, the output of @debug
messages are disabled.
Add the following to webpack.config.js to enable them:
1module.exports = { 2 stats: { 3 loggingDebug: ["sass-loader"], 4 }, 5 // ... 6};
For production builds it's recommended to extract the CSS from your bundle to be able to use parallel loading of CSS/JS resources later on.
There are four recommended ways to extract a stylesheet from a bundle:
webpack.config.js
1const MiniCssExtractPlugin = require("mini-css-extract-plugin"); 2 3module.exports = { 4 module: { 5 rules: [ 6 { 7 test: /\.s[ac]ss$/i, 8 use: [ 9 // fallback to style-loader in development 10 process.env.NODE_ENV !== "production" 11 ? "style-loader" 12 : MiniCssExtractPlugin.loader, 13 "css-loader", 14 "sass-loader", 15 ], 16 }, 17 ], 18 }, 19 plugins: [ 20 new MiniCssExtractPlugin({ 21 // Options similar to the same options in webpackOptions.output 22 // both options are optional 23 filename: "[name].css", 24 chunkFilename: "[id].css", 25 }), 26 ], 27};
webpack.config.js
1module.exports = { 2 entry: [__dirname + "/src/scss/app.scss"], 3 module: { 4 rules: [ 5 { 6 test: /\.js$/, 7 exclude: /node_modules/, 8 use: [], 9 }, 10 { 11 test: /\.scss$/, 12 exclude: /node_modules/, 13 type: "asset/resource", 14 generator: { 15 filename: "bundle.css", 16 }, 17 use: ["sass-loader"], 18 }, 19 ], 20 }, 21};
webpack.config.js
1module.exports = { 2 entry: [__dirname + "/src/scss/app.scss"], 3 module: { 4 rules: [ 5 { 6 test: /\.js$/, 7 exclude: /node_modules/, 8 use: [], 9 }, 10 { 11 test: /\.scss$/, 12 exclude: /node_modules/, 13 use: [ 14 { 15 loader: "file-loader", 16 options: { outputPath: "css/", name: "[name].min.css" }, 17 }, 18 "sass-loader", 19 ], 20 }, 21 ], 22 }, 23};
(source: https://stackoverflow.com/a/60029923/2969615)
Enables/Disables generation of source maps.
To enable CSS source maps, you'll need to pass the sourceMap
option to the sass-loader
and the css-loader
.
webpack.config.js
1module.exports = { 2 devtool: "source-map", // any "source-map"-like devtool is possible 3 module: { 4 rules: [ 5 { 6 test: /\.s[ac]ss$/i, 7 use: [ 8 "style-loader", 9 { 10 loader: "css-loader", 11 options: { 12 sourceMap: true, 13 }, 14 }, 15 { 16 loader: "sass-loader", 17 options: { 18 sourceMap: true, 19 }, 20 }, 21 ], 22 }, 23 ], 24 }, 25};
If you want to edit the original Sass files inside Chrome, there's a good blog post. Checkout test/sourceMap for a running example.
Please take a moment to read our contributing guidelines if you haven't yet done so.
No vulnerabilities found.
Reason
no dangerous workflow patterns detected
Reason
GitHub workflow tokens follow principle of least privilege
Details
Reason
10 commit(s) and 8 issue activity found in the last 90 days -- score normalized to 10
Reason
no binaries found in the repo
Reason
license file detected
Details
Reason
Found 12/27 approved changesets -- score normalized to 4
Reason
dependency not pinned by hash detected -- score normalized to 3
Details
Reason
7 existing vulnerabilities detected
Details
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
security policy file not detected
Details
Reason
project is not fuzzed
Details
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
Score
Last Scanned on 2024-11-25
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More