Target only the contents of a folder in gulp

2019-07-01 23:40发布

问题:

I'm using Gulp to compile a project. I'm also using gulp-zip to zip a bunch of files.

I want to zip up all the files in the "dist" folder, so I'm using this:

var thesrc: ['dist/**/*'];
gulp.task('createMainZip', ['createPluginZip'], function () {
    return gulp.src(thesrc)
    .pipe(zip('main_files.zip'))
    .pipe(gulp.dest('compiled'));
});

This compiles the files to a zip in the following way:

  • dist
    • file.css
    • another.js
    • folder
      • file.js

However, I want it like this:

  • file.css
  • another.js
  • folder
    • file.js

Without the dist folder. Is there a way to do this using a different src path?

Thanks for your help.

回答1:

gulp-zip doesn't honor base. See for some background:

  • https://github.com/sindresorhus/gulp-zip/issues/10
  • https://github.com/sindresorhus/gulp-zip/pull/11
  • https://github.com/sindresorhus/gulp-zip/blob/master/index.js#L30

Now, you can do something like that (admittedly ugly):

var gulp = require('gulp');
var zip = require('gulp-zip');
var thesrc = ['**/*'];
gulp.task('createMainZip', function () {
  return gulp.src(thesrc, {cwd: __dirname + "/dist"})
  .pipe(zip('main_files.zip'))
  .pipe(gulp.dest('compiled'));
});


回答2:

Ok, try:

return gulp.src(thesrc, {base: 'dist'})


回答3:

For me, following did work:

// Taking everything from src and doc dirs
return gulp.src(['src/**/*', 'doc/**/*'], { base: __dirname })
  .pipe(zip('dist.zip'))
  .pipe(gulp.dest('dist'));