@4c/fast-sass-loader 中文文档教程

发布于 4年前 浏览 29 项目主页 更新于 3年前

fast-sass-loader

构建状态Coverage Status

用于 webpack 的超快 sass 加载器。

提示:使用 fast-css-loader 你将获得超过 10 倍的 css 构建性能

特性:

  • 5~10 times faster than sass-loader in large sass project
  • support sass file dedupe, never worry about @import same file in different place
  • support url resolve, never worry about the problem with url(...) (see https://github.com/webpack-contrib/sass-loader#problems-with-url)

用于 webpack 的快速 sass 加载器。 比sass-loader快5~10倍,支持url解析。

vs sass-loader

Featuresfast-sass-loadersass-loader
PerformanceFast (5~10 times)Slow
Sass Dedupe×
Url Resolve× (need resolve-url-loader, it's buggy)
Loader Config×
Source Map×
Internal Cache×

Performance

性能基准(运行 npm run perf):

image

由于 sass-loader 不会对重复的 sass 文件进行重复数据删除,因此结果会非常非常大(6.95MB!!!),总编译时间需要 64.9 秒(将近比 fast-sass-loader 长 6 倍)。

Why fast-sass-loader is faster than sass-loader ?

  1. Support sass file dedupe, so node-sass won't compile same file repeatedly, the performance improvement is s ignificant when your sass files number grows very large.
  2. Before node-sass compile, fast-sass-loader will merge all sass files into a single file, so node-sass only need to compile one large file, it's faster than @importer of libsass.
  3. The internal cache will store all result for every entry, only compile sass when related file changed.

Install

通过 npm 安装:

npm install fast-sass-loader --save-dev

您需要安装 node-sasswebpack 作为对等依赖项。

Configration

webpack 2, 3 and 4:

{
  module: {
    rules: [
      {
        test: /\.(scss|sass)$/,
        use: [
          'css-loader',
          {
            loader: 'fast-sass-loader',
            options: {
              includePaths: [ ... ]
            }
          }
        ]
      },
      // other loaders ...
    ]
  }
}

webpack 1:

{
  module: {
    loaders: [
      {
        test: /\.(scss|sass)$/,
        loader: 'css!fast-sass'
      },
      // other loaders ...
    ]
  }
}

Options

includePaths:

node-sass 可以查看的一组路径,以尝试解析您的 @import 声明。 在使用数据的时候,推荐大家使用这个。

data:

如果你想在实际入口文件之前添加 Sass 代码,你可以设置数据选项。 在这种情况下,加载程序不会覆盖数据选项,而只是附加条目的内容。 当您的某些 Sass 变量依赖于环境时,这尤其有用:

{
    loader: "fast-sass-loader",
    options: {
        data: "$env: " + process.env.NODE_ENV + ";"
    }
}

请注意:由于您正在注入代码,这将破坏您的入口文件中的源映射。 通常有比这更简单的解决方案。

transformers:

如果你想导入不是基本 Sass 或 css 文件的文件,你可以使用 transformers 选项。 此选项采用一组转换器条目,每个条目都有一个文件扩展名列表和一个转换函数。 如果导入文件的扩展名与其中一个转换器的扩展名匹配,则文件内容将传递给相应的转换函数。 你的转换函数应该返回一个 sass 字符串,它将直接写入你编译的 Sass 文件中。 如果你使用 .json 文件来跨平台共享你的基本样式并且你想将你的 .json 文件直接导入到你的 Sass 中,这将特别有用。

{
    loader: "fast-sass-loader",
    options: {
        transformers: [
            {
                extensions: [".json"],
                transform: function(rawFile) {
                    return jsonToSass(rawFile);
                }
            }
        ]
    }
}

Warning

Mixing import .scss and.sass file is not allowed

由于 fast-sass-loader 将解析 @import 并将所有文件合并到单个 sass 文件中,因此您不能从 导入 .scss 文件.sass (或相反)。

例如:

// file: entry.scss
@import "path/to/file.sass";  // cannot import `path/to/file.sass` in a `.scss` file

body {
  background: #FFF;
}

Avoid same variable name in different sass files

由于 fast-sass-loader 会去重 sass 文件,之后导入的文件将被忽略。 在不同的 sass 填充中使用相同的变量名会产生意外的输出。

例如(用fast-sass-loader编译entry.scss):

// a.scss
$foobar: #000;
// b.scss
@import "a.scss";
$foobar: #AAA;

h1 { color: $foobar; }
// entry.scss
@import "b.scss";
@import "a.scss"; // this file will be ignore: $foobar === #AAA

h2 { color: $foobar; }

// will output:
// h1 { color: #AAA; }
// h2 { color: #AAA; }

可以使用变量前缀来绕过。

Avoid nested @import in sass rules

fast-sass-loader 不支持 sass 规则中的 @import 语句,例如:

.a {
  @import 'group'
}

.b {
  @import 'group'
}

您应该使用 mixin 包装要导入的规则,然后将它们包含在您的 .a { ....b { ... }

License

麻省理工学院

fast-sass-loader

Build StatusCoverage Status

Blazingly fast sass loader for webpack.

Tips: using with fast-css-loader you will get more than 10 times css build performance

Features:

  • 5~10 times faster than sass-loader in large sass project
  • support sass file dedupe, never worry about @import same file in different place
  • support url resolve, never worry about the problem with url(...) (see https://github.com/webpack-contrib/sass-loader#problems-with-url)

fast sass loader for webpack. 5~10 times faster than sass-loader, and support url resolve.

vs sass-loader

Featuresfast-sass-loadersass-loader
PerformanceFast (5~10 times)Slow
Sass Dedupe×
Url Resolve× (need resolve-url-loader, it's buggy)
Loader Config×
Source Map×
Internal Cache×

Performance

performance benchmark (run npm run perf):

image

Since the sass-loader doesn't dedupe repeated sass files, the result will be very very large (6.95MB!!!), and the total compile time takes 64.9 seconds (nearly 6 times longer than fast-sass-loader).

Why fast-sass-loader is faster than sass-loader ?

  1. Support sass file dedupe, so node-sass won't compile same file repeatedly, the performance improvement is s ignificant when your sass files number grows very large.
  2. Before node-sass compile, fast-sass-loader will merge all sass files into a single file, so node-sass only need to compile one large file, it's faster than @importer of libsass.
  3. The internal cache will store all result for every entry, only compile sass when related file changed.

Install

install by npm:

npm install fast-sass-loader --save-dev

and you need install node-sass and webpack as peer dependencies.

Configration

webpack 2, 3 and 4:

{
  module: {
    rules: [
      {
        test: /\.(scss|sass)$/,
        use: [
          'css-loader',
          {
            loader: 'fast-sass-loader',
            options: {
              includePaths: [ ... ]
            }
          }
        ]
      },
      // other loaders ...
    ]
  }
}

webpack 1:

{
  module: {
    loaders: [
      {
        test: /\.(scss|sass)$/,
        loader: 'css!fast-sass'
      },
      // other loaders ...
    ]
  }
}

Options

includePaths:

An array of paths that node-sass can look in to attempt to resolve your @import declarations. When using data, it is recommended that you use this.

data:

If you want to prepend Sass code before the actual entry file, you can set the data option. In this case, the 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:

{
    loader: "fast-sass-loader",
    options: {
        data: "$env: " + process.env.NODE_ENV + ";"
    }
}

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.

transformers:

If you want to import files that aren't basic Sass or css files, you can use the transformers option. This option takes an array of transformer entries, each with a list of file extensions and a tranform function. If an imported file's extension matches one of the transformers' extensions, the file contents will be passed to the corresponding transform function. Your transform function should return a sass string that will be directly written into your compiled Sass file. This is especially useful if you use .json files to share your basic styles across platforms and you'd like to import your .json files directly into your Sass.

{
    loader: "fast-sass-loader",
    options: {
        transformers: [
            {
                extensions: [".json"],
                transform: function(rawFile) {
                    return jsonToSass(rawFile);
                }
            }
        ]
    }
}

Warning

Mixing import .scss and.sass file is not allowed

Since fast-sass-loader will parse @import and merge all files into single sass file, you cannot import .scss file from .sass (or opposite).

For example:

// file: entry.scss
@import "path/to/file.sass";  // cannot import `path/to/file.sass` in a `.scss` file

body {
  background: #FFF;
}

Avoid same variable name in different sass files

Since fast-sass-loader will dedupe sass file, later imported file will be ignored. Using same variable name in different sass fill would produce unexpected output.

For example (compile entry.scss with fast-sass-loader):

// a.scss
$foobar: #000;
// b.scss
@import "a.scss";
$foobar: #AAA;

h1 { color: $foobar; }
// entry.scss
@import "b.scss";
@import "a.scss"; // this file will be ignore: $foobar === #AAA

h2 { color: $foobar; }

// will output:
// h1 { color: #AAA; }
// h2 { color: #AAA; }

You can use variable prefix to bypass.

Avoid nested @import in sass rules

fast-sass-loader doesn't support @import statement in sass rules, for example:

.a {
  @import 'group'
}

.b {
  @import 'group'
}

you should wrap the rules that you want to import with mixin, then include them in your .a { ... } or .b { ... }

License

MIT

    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文