slushjs / slush

The streaming scaffolding system - Gulp as a replacement for Yeoman
http://slushjs.github.io/generators
MIT License
1.24k stars 58 forks source link

Slush copy corrupt my image file #46

Closed zhouhao27 closed 8 years ago

zhouhao27 commented 8 years ago

I created a slush project. It works fine except it corrupt my image file. For example, there is a file called logo.png in my assets folder. After I use slush to generate my project. The image files in my assets folder are corrupted. I'm sure it's not because of the error of my gulp file. Because after the project is generated, the images are file if I copy new images to my assets folder. I'm thinking it could be caused by the slush-template copy.

Here is my slush file:

inquirer.prompt(prompts,
    function (answers) {
        if (!answers.moveon) {
            return done();
        }
        answers.appNameSlug = _.slugify(answers.appName);
        gulp.src(__dirname + '/templates/**')
            .pipe(template(answers))
            .pipe(rename(function (file) {
                if (file.basename[0] === '_') {
                    file.basename = '.' + file.basename.slice(1);
                }
            }))
            .pipe(conflict('./'))
            .pipe(gulp.dest('./'))
            .pipe(install())
            .on('end', function () {
                done();
            });
    });

My copy task in my gulp file inside template folder:

gulp.task('assets', function() {    
var imgDest = './build/assets/';

return gulp.src(imgSrc)
    .pipe(changed(imgDest))
    .pipe(gulp.dest(imgDest))
    .pipe(browserSync.reload({stream : true}));
   });

What could be the cause? Can anybody help? Thanks.

zhouhao27 commented 8 years ago

This is a really weird issue. The other files are no problem except the image files.

RyanMetin commented 8 years ago

I just noticed this problem with my generator last night. Emits a broken ico and png.

peterjuras commented 8 years ago

This is due to the gulp-template plugin. Don't pass images through or it will break them.

You can use the gulp-filter plugin to filter out files for a certain stream, e.g.:

var gulpFilter = require('gulp-filter');

// ...

// Create a filter that excludes certain files 
// (Alternatively, only pass templated files to the template plugin)
var filterWithoutCertainImages = gulpFilter(['**/**', '!yourimage.png'], { restore: true );

// ...

gulp.src(relevantFiles)
  // Remove the image files from the stream
  .pipe(filterWithoutCertainImages)
  // Images will now no longer be run through gulp-template
  .pipe(template(answers))
  // Restore the image files into the stream
  .pipe(filterWithoutCertainImages.restore)
  // continue with other plugins
joakimbeng commented 8 years ago

Thanks @peterjuras! Closing...