Gathering detailed insights and metrics for node-sass-magic-importer
Gathering detailed insights and metrics for node-sass-magic-importer
Gathering detailed insights and metrics for node-sass-magic-importer
Gathering detailed insights and metrics for node-sass-magic-importer
nwb-node-sass-magic-importer
A node-sass-magic-importer plugin for nwb
sprite-magic-importer
Custom node-sass importer for create CSS Sprites like Magic Imports of the Compass.
sass-magic-importer
sass-magic-importer uses the node-sass-magic-importer to be used with native dart/sass implementation
Custom node-sass importer for selector specific imports, module importing, globbing support and importing files only once.
npm install node-sass-magic-importer
Typescript
Module System
Min. Node Version
Node Version
NPM Version
TypeScript (92.56%)
SCSS (5.81%)
JavaScript (0.85%)
Shell (0.42%)
CSS (0.36%)
Total Downloads
21,275,258
Last Day
17,080
Last Week
72,043
Last Month
320,349
Last Year
4,755,225
293 Stars
915 Commits
28 Forks
5 Watching
10 Branches
7 Contributors
Minified
Minified + Gzipped
Latest Version
5.3.3
Package Id
node-sass-magic-importer@5.3.3
Unpacked Size
97.02 kB
Size
23.20 kB
File Count
160
NPM Version
9.6.0
Node Version
18.14.2
Publised On
02 Jul 2023
Cumulative downloads
Total Downloads
Last day
9.7%
17,080
Compared to previous day
Last week
-12.7%
72,043
Compared to previous week
Last month
1.9%
320,349
Compared to previous month
Last year
-1.6%
4,755,225
Compared to previous year
Custom node-sass importer for selector specific imports, node importing, module importing, globbing support and importing files only once.
1npm install node-sass-magic-importer --save-dev
This importer enables several comfort functions for importing SASS files more easily.
node_modules
without specifying the full path.@import: 'scss/**/*.scss'
) to import multiple files at once.By default every file is only imported once even if you @import
the same file multiple times in your code (except if you are using filters).
With selector filtering, it is possible to import only certain CSS selectors form a file. This is especially useful if you want to import only a few CSS classes from a huge library or framework.
1// Example: 2@import '{ .btn, .btn-alert } from style.scss';
1// Result: 2.btn { } 3.btn-alert { }
1// Example: 2@import '{ .btn as .button, .btn-alert as .button--alert } from style.scss';
1// Result: 2.button { } 3.button--alert { } // Transformed to match BEM syntax.
1// Example: 2@import '{ /^\..+-alert/ } from style.scss';
1// Result: 2.box-alert { } 3.btn-alert { }
1// Example: 2@import '{ /^\.btn(.*)/ as .button$1 } from style.scss';
1// Result: 2.button { } 3.button-alert { }
Bootstrap is a mighty and robust framework but most of the time you use only certain parts of it. There is the possibility to customize Bootstrap to your needs but this can be annoying and you still end up with more code than you need. Also you might want to use just some specific parts of Bootstrap but your project uses the BEM syntax for writing class names.
1// This example uses the v4 dev version of the Bootstrap `alert` component: 2// https://github.com/twbs/bootstrap/blob/v4-dev/scss/_alert.scss 3@import 'bootstrap/scss/variables'; 4@import 'bootstrap/scss/mixins/border-radius'; 5@import 'bootstrap/scss/mixins/alert'; 6@import '{ 7 .alert, 8 .alert-dismissible as .alert--dismissible, 9 .close as .alert__close 10} from bootstrap/scss/alert';
1// Result: 2.alert { 3 padding: 15px; 4 margin-bottom: 1rem; 5 border: 1px solid transparent; 6 border-radius: 0.25rem; 7} 8 9.alert--dismissible { 10 padding-right: 35px; 11} 12 13.alert--dismissible .alert__close { 14 position: relative; 15 top: -2px; 16 right: -21px; 17 color: inherit; 18}
Filter certain elements from SCSS code.
1// Example: 2@import '[variables, mixins] from style.scss';
1// style.scss: 2$variable1: 'value'; 3$variable2: 'value'; 4.selector { } 5@mixin mixin() { } 6 7// Result: 8$variable1: 'value'; 9$variable2: 'value'; 10@mixin mixin() { }
@media
, @supports
, @mixin
,...@function
@mixin
.class-selector
, #id-selector
,...$variable
In modern day web development, modules and packages are everywhere. There is no way around npm if you are a JavaScript developer. More and more CSS and SASS projects move to npm but it can be annoying to find a convenient way of including them into your project. Module importing makes this a little easier.
1// Import the file that is specified in the `package.json` file of the module. 2// In the case of bootstrap, the following file is loaded: 3// https://github.com/twbs/bootstrap/blob/v4-dev/scss/bootstrap.scss 4@import '~bootstrap';
1// Import only specific files: 2@import '~bootstrap/scss/variables'; 3@import '~bootstrap/scss/mixins/border-radius'; 4@import '~bootstrap/scss/mixins/alert'; 5@import '~bootstrap/scss/alert';
The "~" is mandatory and marks the import path as module.
If only the module name is given (e.g. @import '~bootstrap'
) the importer looks in the package.json
file of the module for the following keys: "sass", "scss", "style", "css", "main.sass", "main.scss", "main.style", "main.css" and "main". The first key that is found is used for resolving the path and importing the file into your sass code.
To load only a certain file from a module you can specify the file in the import url (e.g. @import '~bootstrap/scss/_alert.scss'
). The node-sass-magic-importer
also supports partial file name resolving so you can import files by only specifying their base name without prefix and extension (e.g. @import '~bootstrap/scss/alert'
). Sadly bootstrap and most other frameworks do not load their dependencies directly in the concerned files. So you have to load all dependencies of a file manually like in the example above. I recommend you to do better and to import dependencies directly in the files that are using them.
Globbing allows pattern matching operators to be used to match multiple files at once.
1// Import all files inside the `scss` directory and subdirectories. 2@import: 'scss/**/*.scss';
1var sass = require('node-sass'); 2var magicImporter = require('node-sass-magic-importer'); 3 4sass.render({ 5 ... 6 importer: magicImporter() 7 ... 8});
1const sass = require('node-sass'); 2const magicImporter = require('node-sass-magic-importer'); 3 4const options = { 5 // Defines the path in which your node_modules directory is found. 6 cwd: process.cwd(), 7 // Define the package.json keys and in which order to search for them. 8 packageKeys: [ 9 'sass', 10 'scss', 11 'style', 12 'css', 13 'main.sass', 14 'main.scss', 15 'main.style', 16 'main.css', 17 'main' 18 ], 19 // You can set the special character for indicating a module resolution. 20 packagePrefix: '~', 21 // Disable console warnings. 22 disableWarnings: false, 23 // Disable importing files only once. 24 disableImportOnce: false, 25 // Add custom node filters. 26 customFilters: undefined 27}; 28 29sass.render({ 30 ... 31 importer: magicImporter(options) 32 ... 33});
1const sass = require('node-sass'); 2const magicImporter = require('node-sass-magic-importer'); 3 4const options = { 5 customFilters: { 6 // Add a node filter for a specific min-width media query. 7 customMediaWidth: [ 8 [ 9 { property: 'type', value: 'atrule' }, 10 { property: 'name', value: 'media' }, 11 { property: 'params', value:'(min-width: 42em)' } 12 ] 13 ], 14 // Add a node filter for print media queries. 15 customMediaPrint: [ 16 [ 17 { property: 'type', value: 'atrule' }, 18 { property: 'name', value: 'media' }, 19 { property: 'params', value: 'print' } 20 ] 21 ] 22 } 23}; 24 25sass.render({ 26 ... 27 importer: magicImporter(options) 28 ... 29});
1// Sass file which implements filter importing. 2@import '[custom-media-width, custom-media-print] from file/with/at/rules';
1// file/with/at/_rules.scss 2@media (min-width: 42em) { 3 .custom-1-mq { 4 content: 'Custom 1 mq'; 5 } 6} 7 8@media (min-width: 43em) { 9 .custom-2-mq { 10 content: 'Custom 1 mq'; 11 } 12} 13 14@media print { 15 .custom-print-mq { 16 content: 'Custom print mq'; 17 } 18}
1// CSS output – the `min-width: 43em` media query gets not imported. 2@media (min-width: 42em) { 3 .custom-1-mq { 4 content: 'Custom 1 mq'; 5 } 6} 7 8@media print { 9 .custom-print-mq { 10 content: 'Custom print mq'; 11 } 12}
1// webpack.config.js 2const magicImporter = require('node-sass-magic-importer'); 3const MiniCssExtractPlugin = require('mini-css-extract-plugin'); 4 5module.exports = { 6 module: { 7 rules: [ 8 { 9 test: /\.scss$/, 10 use: [ 11 { 12 loader: MiniCssExtractPlugin.loader, 13 }, 14 { 15 loader: 'css-loader' 16 }, 17 { 18 loader: 'sass-loader', 19 options: { 20 sassOptions: { 21 importer: magicImporter() 22 } 23 } 24 } 25 ] 26 } 27 ] 28 }, 29 plugins: [ 30 new MiniCssExtractPlugin({ 31 filename: 'style.css' 32 }) 33 ] 34}
1const gulp = require('gulp'); 2const sass = require('gulp-sass'); 3const magicImporter = require('node-sass-magic-importer'); 4 5gulp.task('sass', function () { 6 return gulp.src('./**/*.scss') 7 .pipe(sass({ importer: magicImporter() }).on('error', sass.logError)) 8 .pipe(gulp.dest('./css')); 9});
1node-sass --importer node_modules/node-sass-magic-importer/dist/cli.js -o dist src/index.scss
includePaths
option when initializing the importer. Use the node-sass includePaths option instead.prefix
option was renamed to packagePrefix
.Node filtering and selector filtering goes only one level deep. This means, if you're importing a file with selector or node 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 selectors or 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.
Markus Oberlehner
Website: https://markus.oberlehner.net
Twitter: https://twitter.com/MaOberlehner
PayPal.me: https://paypal.me/maoberlehner
Patreon: https://www.patreon.com/maoberlehner
MIT
No vulnerabilities found.
Reason
no binaries found in the repo
Reason
license file detected
Details
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
Found 0/1 approved changesets -- score normalized to 0
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
security policy file not detected
Details
Reason
dependency not pinned by hash detected -- score normalized to 0
Details
Reason
project is not fuzzed
Details
Reason
branch protection not enabled on development/release branches
Details
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
Reason
45 existing vulnerabilities detected
Details
Score
Last Scanned on 2025-01-27
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