Gulp - 使用自定义文件夹构建文件

时间:2015-05-12 21:15:38

标签: javascript gulp

我正在创建一个用于构建自定义电子邮件模板的工作流程。在我的文件夹中,我有这样的结构:

folder/
    templates/
        template-001.html
        image-001.jpg
    images/
        image-default.jpg
    src/
        style.scss

我使用gulp-inline-css获取编译css文件,并将所有样式内联插入文件template-001.html中。然后,该任务获取所有这些文件并放入build文件夹。像这样:

folder/
    templates/
        template-001.html
        image-001.jpg
    images/
        image-default.jpg
    src/
        style.scss
    build/
        template-001.html
        image-default.jpg
        image-001.jpg

这对单个项目来说没问题。但我希望能够构建多个模板并以正确的方式组织文件。我想有一个像这样的文件夹结构:

folder/
    templates/
        template-001/
            template-001.html
            image-001.jpg
    images/
        image-default.jpg
    src/
        style.scss
    build/
        template-001/
            template-001.html
            image-default.jpg
            image-001.jpg

换句话说,我希望能够获取template-001.html的文件夹并在build文件夹中重现它。根据我目前的设置,我不知道如何使它成为可能。

这是我的gulpfile.js

var gulp = require( 'gulp' ),
        sass = require( 'gulp-sass' ),
        autoprefixer = require( 'gulp-autoprefixer' ),
        minifycss = require( 'gulp-minify-css' ),
        imagemin = require( 'gulp-imagemin' ),
        rename = require( 'gulp-rename' ),
        concat = require( 'gulp-concat' ),
        plumber = require( 'gulp-plumber' ),
        notify = require( 'gulp-notify' ),
        inlineCss = require('gulp-inline-css'),
        projectTitle = 'E-mail Templates';

// styles task
gulp.task( 'styles', function() {
    return gulp.src( 'src/*.scss' )
        .pipe( plumber() )
        .pipe( sass({ paths: ['src/'] }) )
        .pipe( notify( {
        title: projectTitle,
        message: 'File: <%= file.relative %> was compiled!'
        } ) )
        .pipe( autoprefixer( 'last 2 version', 'safari 5', 'ie 8', 'ie 9', 'opera 12.1', 'ios 6', 'android 4' ) )
        .pipe( gulp.dest( 'src/' ) )
        .pipe( notify( {
        title: projectTitle,
        message: 'Minified file: <%= file.relative %> was created / updated!'
        } ) )

        console.log( '<%= file %>' );
} );

// styles task
gulp.task( 'html-build', function() {
    return gulp.src( 'templates/**/*.html' )
        .pipe( inlineCss({
                applyStyleTags: false,
                applyLinkTags: true,
                removeStyleTags: false,
                removeLinkTags: true
        }) )
        .pipe( gulp.dest('build/') )
        .pipe( notify( {
        title: projectTitle,
        message: 'Template File: <%= file.relative %> was created / updated!'
        } ) )
} );

// images task
gulp.task( 'images', function() {
    return gulp.src( 'images/**/*' )
        .pipe( plumber() )
        .pipe( imagemin({ optimizationLevel: 7, progressive: true, interlaced: true }) )
        .pipe( gulp.dest( 'build/' ) )
        .pipe( notify( {
        title: projectTitle,
        message: 'Image: <%= file.relative %> was optimized!'
        } ) )
} );

// watch task
gulp.task( 'watch', [ 'styles' ], function() {

    // Watch .scss files
    gulp.watch( 'src/**/*.scss', [ 'styles', 'html-build' ] );

    // Watch .html files
    gulp.watch( 'templates/**/*.html', [ 'html-build' ] );

    // Watch image files
    gulp.watch('images/**/*', ['images'] );

});

有关热的任何提示或建议使其成为可能吗?谢谢!

1 个答案:

答案 0 :(得分:1)

那么,您想将模板文件夹中非HTML的所有文件复制到dest文件夹吗?

gulp.task('copy', function() {
    return gulp.src(['templates/**/*','!templates/**/*.html'])
        .pipe(gulp.dest('build'));
});

这将采取这个:

folder/
  templates/
    template-001/
      template-001.html
      image-001.jpg
    template-002/
      template-002.html
      image-002.jpg
    template-003/
      template-003.html
      image-003.jpg

并创建:

folder/
  build/
    template-001/
      image-001.jpg
    template-002/
      image-002.jpg
    template-003/
      image-003.jpg

这应该可以解决问题并与其他任务一起工作。当我正确解释您的Gulpfile时,HTML文件应该已经到位。您是否还需要帮助复制images中的文件?

更新

我重写了images任务。请阅读评论以了解发生了什么:

var fs = require('fs');
gulp.task('images', function(done) {
    // get all the directories inside templates. this will return
    // an array of folders
    var templates = fs.readdirSync('templates').filter(function(el) {
        return fs.statSync('templates/' + el).isDirectory()
    });

    // we start our stream with all the files from images we want to have
    var stream = gulp.src( 'images/**/*' )
        .pipe(plumber())
        .pipe(imagemin({ optimizationLevel: 7, progressive: true, interlaced: true }));

    // now we add a destination dir for each one of our
    // templates
    templates.forEach(function(el) {
        stream.pipe(gulp.dest('templates/' + el));
    });

    // we add the rest of our pipes
    stream.pipe(notify({
        title: projectTitle,
        message: 'Image: <%= file.relative %> was optimized!'
    }));

    // returning the stream kicks it off
    return stream;
});
相关问题