Gulp-带有precss插件的postcss在导入scss文件时抛出错误

Gulp-postcss with precss plugin throws an error when importing scss files

我试图在我的 app.scss 文件中导入多个 scss 文件,但 gulp 抛出错误:

//app.scss
@import "base";

body {
   background: #fff;
}

gulpfile.js

gulp.task('build-css', function () {
    return gulp.src(assetsDev + 'scss/*.scss')
        .pipe(sourcemaps.init())
        .pipe(postcss([precss, autoprefixer, cssnano]))
        .pipe(sourcemaps.write())
        .pipe(ext_replace('.css'))
        .pipe(gulp.dest(assetsProd + 'css/'));
});

目录结构:

├── assets
│   └── scss
│       ├── _base.scss
│       └── app.scss
├── src
│   └── css
│       └── style.css
└── gulpfile.js

并且 gulp 抛出此错误:

events.js:141
  throw er; // Unhandled 'error' event
  ^
Error: ENOENT: no such file or directory, open '...\assets\scss\_base.css'
at Error (native)

precss 使用 postcss-partial-import plugin for @import statements. By default 它假定导入的分音符的扩展名是 css.

您需要在所有 @import 语句中提供完整的文件名(带扩展名):

@import "_base.scss";

body {
  background: #fff;
}

或者您需要将 extension 选项传递给 precss():

gulp.task('build-css', function () {
  return gulp.src(assetsDev + 'scss/*.scss')
    .pipe(sourcemaps.init())
    .pipe(postcss([precss({extension:'scss'}), autoprefixer, cssnano]))
    .pipe(sourcemaps.write())
    .pipe(ext_replace('.css'))
    .pipe(gulp.dest(assetsProd + 'css/'));
});