如何使用gulp显示多个文件中单个文件的文件大小?

How to display file size of a single file among multiple files using gulp?

我正在使用以下 gulp 任务将所有 scss 处理为 CSS,将它们组合成一个缩小的文件,并显示文件大小。但是,我想分别查看缩小的 CSS 文件和地图文件的文件大小。以下不做作业。

gulp.task('styles', function () {
  return gulp.src(config.css.src)
    .pipe(glob())
    .pipe(plumber({
      errorHandler: function (error) {
        notify.onError({
          title: 'Processing all custom SCSS files to css',
          subtitle: 'Failed!',
          message: 'Error: <%= error.message %>',
          sound: 'Frog '
        })(error);
        this.emit('end');
      }}))
    .pipe(sourcemaps.init())
    .pipe(sass(sassOptions).on('error', sass.logError))
    .pipe(autoprefix(autoprefixerOptions))
    .pipe(sourcemaps.write('./css'))
    .pipe(gulp.dest(config.css.dest))
    .pipe(size({
      title: 'Total file size of custom css file and the map file associated with the css file: ',
      showFiles: 'true',
      showTotal: 'true',
      prettySize: 'true'
    }));
});

我会采用不同的方法,而不是在管道中再添加两个插件(gulp-filter 和 gulp-if)。

首先,我会为 gulp-filesize 更改 gulp-size 插件,并创建两个任务,一个用于样式编译、linting 和 sourcemaps。还有一个,只是为了获取你需要的那两个文件的文件大小。

const gulp = require('gulp');

// The rest of the plugins you're using here

const runSequence = require('run-sequence'); // Run sequentially tasks
const size = require('gulp-filesize'); // Change gulp-size to gulp-filesize

// Create one task that will handle both styles:compile and styles:size tasks
gulp.tasks('styles', function () {
   // You will run compilation first, then check file sizes
   runSequence('styles:compile', 'styles:size');
});

gulp.task('styles:compile', function () {
  return gulp.src(config.css.src)
    .pipe(glob())
    .pipe(plumber({
      errorHandler: function (error) {
        notify.onError({
          title: 'Processing all custom SCSS files to css',
          subtitle: 'Failed!',
          message: 'Error: <%= error.message %>',
          sound: 'Frog '
        })(error);
        this.emit('end');
      }}))
    .pipe(sourcemaps.init())
    .pipe(sass(sassOptions).on('error', sass.logError))
    .pipe(autoprefix(autoprefixerOptions))
    .pipe(sourcemaps.write('./css'))
    .pipe(gulp.dest(config.css.dest));
});

gulp.task('styles:size', function () {
  // This will output the size of both files
  return gulp
    .src(['path/css/yourcssfile.css', 'path/css/yourmapfile.css.map'])
    .pipe(size());
});

运行 gulp styles 你应该得到两个文件的大小,如下所示:

[14:12:36] Size main.css : 1234 B
[14:12:36] Size main.css.map : 1234 B

希望对您有所帮助:)