node-sass-filter-importer
Custom importer for node-sass to filter certain elements like variables, mixins or at-rules from Sass code
Last updated a year ago by maoberlehner .
MIT · Repository · Bugs · Original npm · Tarball · package.json
$ npm install node-sass-filter-importer 
SYNC missed versions from official npm registry.

node-sass-filter-importer

Patreon Donate Build Status Coverage Status GitHub stars

Filter certain elements from SCSS code.

Usage

const sass = require('node-sass');
const filterImporter = require('node-sass-filter-importer');

sass.render({
  ...
  importer: filterImporter()
  ...
});
// Example:
@import '[variables, mixins] from style.scss';
// style.scss:
$variable1: 'value';
$variable2: 'value';
.selector { }
@mixin mixin() { }

// Result:
$variable1: 'value';
$variable2: 'value';
@mixin mixin() { }

Filters

  • at-rules: @media, @supports, @mixin,...
  • functions: @function
  • mixins: @mixin
  • rules: .class-selector, #id-selector,...
  • silent: Extract only nodes that do not compile to CSS code (mixins, placeholder selectors, variables,...)
  • variables: $variable
  • make-your-own: Define custom filters

Options

In the example below you can see the default configuration options.

  • customFilters: Define custom node filters.
const sass = require('node-sass');
const filterImporter = require('node-sass-filter-importer');

const options = {
  customFilters: {
    // Add a node filter for a specific min-width media query.
    customMediaWidth: [
      [
        { property: 'type', value: 'atrule' },
        { property: 'name', value: 'media' },
        { property: 'params', value:'(min-width: 42em)' }
      ]
    ],
    // Add a node filter for print media queries.
    customMediaPrint: [
      [
        { property: 'type', value: 'atrule' },
        { property: 'name', value: 'media' },
        { property: 'params', value: 'print' }
      ]
    ]
  }
};

sass.render({
  ...
  importer: filterImporter(options)
  ...
});
// Sass file which implements filter importing.
@import '[custom-media-width, custom-media-print] from file/with/at/rules';
// file/with/at/_rules.scss
@media (min-width: 42em) {
  .custom-1-mq {
    content: 'Custom 1 mq';
  }
}

@media (min-width: 43em) {
  .custom-2-mq {
    content: 'Custom 1 mq';
  }
}

@media print {
  .custom-print-mq {
    content: 'Custom print mq';
  }
}
// CSS output – the `min-width: 43em` media query gets not imported.
@media (min-width: 42em) {
  .custom-1-mq {
    content: 'Custom 1 mq';
  }
}

@media print {
  .custom-print-mq {
    content: 'Custom print mq';
  }
}

webpack

// webpack.config.js
const filterImporter = require('node-sass-filter-importer');
const MiniCssExtractPlugin = require('mini-css-extract-plugin');

module.exports = {
  module: {
    rules: [
      {
        test: /\.scss$/,
        use: [
          {
            loader: MiniCssExtractPlugin.loader,
          },
          {
            loader: 'css-loader'
          },
          {
            loader: 'sass-loader',
            options: {
              sassOptions: {
                importer: filterImporter()
              }
            }
          }
        ]
      }
    ]
  },
  plugins: [
    new MiniCssExtractPlugin({
      filename: 'style.css'
    })
  ]
}

CLI

node-sass --importer node_modules/node-sass-filter-importer/dist/cli.js -o dist src/index.scss

Upgrade from 1.x.x to 5.x.x

It is not possible anymore to set the includePaths option when initializing the importer. Use the node-sass includePaths option instead.

Why is there no 2.x, 3.x or 4.x version?

This module is maintained in one repository together with multiple other node-sass custom importers. The node-sass-magic-importer repository is using a monorepo approach with fixed versions for all packages. The projects maintained in the node-sass-magic-importer monorepo started out as separate repositories with separate versioning, so when they were integrated into the monorepo, the versions of all projects were raised to 5.0.0 and are in sync since then.

node-sass-magic-importer

This module is powered by node-sass-magic-importer.

Known issues

Multi level filtering

Filtering goes only one level deep. This means, if you're importing a file with filtering which is importing other files, those files are not filtered but imported as is. On a technical level, there is no good solution for this problem. One possibility would be to just pass the filters to all imports in the line but this carries the risk of filtering nodes on which one of the imported files might depend and therefore break the import. I might add this as an optional feature (which can be activated on demand) in the future – let me know if you're interested in multi level filter imports.

About

Author

Markus Oberlehner
Website: https://markus.oberlehner.net
Twitter: https://twitter.com/MaOberlehner
PayPal.me: https://paypal.me/maoberlehner
Patreon: https://www.patreon.com/maoberlehner

License

MIT

Current Tags

  • 5.3.3                                ...           latest (a year ago)

33 Versions

  • 5.3.3                                ...           a year ago
  • 5.3.2                                ...           6 years ago
  • 5.3.1                                ...           6 years ago
  • 5.3.0                                ...           6 years ago
  • 5.2.0                                ...           6 years ago
  • 5.1.3                                ...           7 years ago
  • 5.1.2                                ...           7 years ago
  • 5.1.1                                ...           7 years ago
  • 5.1.0                                ...           7 years ago
  • 5.0.3                                ...           7 years ago
  • 5.0.2                                ...           7 years ago
  • 5.0.1                                ...           7 years ago
  • 5.0.0                                ...           7 years ago
  • 5.0.0-alpha.17                                ...           7 years ago
  • 5.0.0-alpha.16                                ...           7 years ago
  • 5.0.0-alpha.15                                ...           7 years ago
  • 5.0.0-alpha.14                                ...           7 years ago
  • 5.0.0-alpha.13                                ...           7 years ago
  • 5.0.0-alpha.12                                ...           7 years ago
  • 5.0.0-alpha.11                                ...           7 years ago
  • 5.0.0-alpha.10                                ...           7 years ago
  • 5.0.0-alpha.9                                ...           7 years ago
  • 5.0.0-alpha.8                                ...           7 years ago
  • 5.0.0-alpha.7                                ...           7 years ago
  • 5.0.0-alpha.6                                ...           7 years ago
  • 5.0.0-alpha.5                                ...           7 years ago
  • 5.0.0-alpha.4                                ...           7 years ago
  • 5.0.0-alpha.3                                ...           7 years ago
  • 5.0.0-alpha.2                                ...           7 years ago
  • 5.0.0-alpha.1                                ...           7 years ago
  • 1.0.1                                ...           8 years ago
  • 1.0.0                                ...           8 years ago
  • 0.1.0                                ...           8 years ago
Maintainers (1)
Downloads
Total 1
Today 0
This Week 0
This Month 0
Last Day 0
Last Week 0
Last Month 0
Dependencies (3)
Dev Dependencies (3)
Dependents (1)

© 2010 - cnpmjs.org x YWFE | Home | YWFE