Import CSS from node_modules using Gulp-SASS

感情迁移 提交于 2021-02-06 10:42:36

问题


I want to import a CSS-file from my node_modules using SASS.

@import 'normalize.css/normalize';

This is how my gulpfile.js handles my SASS:

const
  gulp = require('gulp'),
  sass = require('gulp-sass');

gulp.task('sass', function () {
  return gulp.src(['source/assets/css/**.scss', '!source/assets/css/**/_*.[scss|sass'])
    .pipe(sass())
    .pipe(gulp.dest('output/assets/css'));
});

SASS compiler will not import the css from node_modules. Instead, this will throw an error.

Error: File to import not found or unreadable: normalize.css/normalize.

回答1:


SASS compiler doesn't know where to look for the files. The location needs to be specified.

gulp.task('sass', function () {
  return gulp.src(['source/assets/css/**.scss', '!source/assets/css/**/_*.[scss|sass'])
    .pipe(sass({
      includePaths: ['node_modules']
    }))
    .pipe(gulp.dest('output/assets/css'));
});



回答2:


What works for me, in 2020, is this:

function styles() {
    return (
      gulp.src(paths.styles.src)
        .pipe(sourcemaps.init())
        .pipe(sass({
                    includePaths: ['./node_modules/purecss-sass/vendor/assets/stylesheets/',
                                  './node_modules/modularscale-sass/stylesheets/',
                                  './node_modules/typi/scss/'
                                  ]
                  }))
        .on("error", sass.logError)
        .pipe(postcss([autoprefixer(), cssnano()]))
        .pipe(sourcemaps.write())
        .pipe(gulp.dest(paths.styles.dest))
        .pipe(browserSync.stream())
    );
}

Now in the scss files, I can

@import 'modularscale';
@import 'typi';
@import 'purecss';

The other options seem to be:

  1. put the full paths to the main _somelibrary.scss file directly in the scss files (minus the extension), so something like:
@import '../../node_modules/purecss-sass/vendor/assets/stylesheets/_purecss';
  1. Put includePaths: ['./node_modules'] and add the relative paths in the scss files:
@import 'purecss-sass/vendor/assets/stylesheets/_purecss';


来源:https://stackoverflow.com/questions/44551822/import-css-from-node-modules-using-gulp-sass

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!