如何忽略 gulp-sass 中的源文件夹结构?

How to ignore source folder structure in gulp-sass?

我正在尝试将 "main.css" 文件直接放在 css 文件夹中。 但是,我得到这条路径“/css/Content/scss/main.css”并且我想要 "/css/main.css"

gulp.task('sass', () => {
    const sourceFolder = path.join('.', 'Content', 'scss', 'main.scss');
    const distFolder = path.join('.', 'wwwroot', 'css');
    return gulp.src(sourceFolder)
        .pipe(sass({
            sourceMap: true,
            style: 'compressed'
        }))
        .pipe(cleanCSS({
            compatibility: 'ie8'
        }))
        .pipe(sourcemaps.write('.'))
        .pipe(gulp.dest(distFolder))
        .on('end', () => console.log(`[${new Date().toLocaleTimeString()}] -> sass compilation complete...`));
});

假设您希望 main.csswwwroot/css 结束,请尝试更改为此行:

return gulp.src(sourceFolder, { base: path.join('Content', 'scss') })

抱歉,我无法解释为什么您的原始代码适用于 Linux。

来自glob base in gulpjs.docs

Vinyl instances generated by src() are constructed with the glob base set as their base property. When written to the file system with dest(), the base will be removed from the output path to preserve directory structures.

因此将 base 设置为 path.join('Content', 'scss') 文件路径的那部分将被删除,因此 main.css 将直接进入您的 distFolder 并删除父文件夹。