Importing Sass through npm

后端 未结 5 1366
闹比i
闹比i 2020-12-13 17:18

Currently in our Sass files we have something like the following:

@import \"../../node_modules/some-module/sass/app\";

This is bad, because

5条回答
  •  旧巷少年郎
    2020-12-13 18:17

    I made the sass-npm module specifically for this.

    npm install sass-npm
    

    In your SASS:

    // Since node_modules/npm-module-name/style.scss exists, this will be imported.
    @import "npm-module-name";
    
    // Since just-a-sass-file isn't an installed npm module, it will be imported as a regular SCSS file.
    @import "just-a-sass-file";
    

    I normally use gulp-sass (which has the same 'importer' option as regular SASS)

    var gulp = require('gulp'),
        sass = require('gulp-sass'),
        sassNpm = require('sass-npm')();
    

    Then, in your .pipe(sass()), add the importer as an option:

    .pipe(sass({
        paths: ['public/scss'],
        importer: sassNpm.importer,
    }))
    

提交回复
热议问题