Search code examples
javascriptgulpgulp-sassgulp-watch

How to setup gulp for this type of scaffolding?


Folder Structure

Components ------component1 -partials - js - html - scss - component1.css - component1.js ------component2 -partials - js - html - scss - component2.css - component2.js

Functionality is all my js, html and scss file convert into one css and js fine but inside into the component folder. If I create a new component I don't want every time to add them separately into gulp It will automatically add through gulp. How can I write my gulp to achieve this type of functionality ?

Help will be really appreaticed...!!!


Solution

  • You can use this code that I created for your case:

    Project structure: folder structure tested

    gulpfile.js

    /**
     * Created by Anderson on 22/04/2016.
     */
    
    'use strict';
    
    var gulp        = require('gulp');
    var data        = require('gulp-data');
    var concat      = require('gulp-concat');
    var sass        = require('gulp-sass');
    var gcallback   = require('gulp-callback');
    var rename      = require('gulp-rename');
    var del         = require('del');
    var path        = require('path');
    var fs          = require('fs');
    
    
    gulp.task('main',function(){
        //scan componens folder all files and folders
        gulp.src("./components/**")
            .pipe(data(function(file,cb){
                var filepath = file.path;
                var stats = fs.lstatSync(filepath);
    
                var base = file.base;
                var fpath = filepath.replace(file.base,"");
                //console.log(fpath);
    
                var array = fpath.split("\\");
                var basename = path.basename(fpath);
                var componentName = array[0];
                //destiny
                var dest = base + fpath.replace(basename,"");
                    dest = dest.replace("partials\\","");
    
                //process the scss
                if(stats.isDirectory() && basename == "scss"){
                    //console.log(array,componentName,basename);
                    //console.log(file.path);
                    var scssScan = base + fpath+"\\*.scss";
                    console.log("scan",scssScan );
                    console.log("dest",dest);
                    //scan dir for scss
                    gulp.src(scssScan)
                        //concat all scss in a temporary file
                        .pipe(concat(componentName+".scss"))
                        .pipe(gulp.dest(dest))
                        .pipe(gcallback(function(){
                            //scan to process the scss
                            var componentPath = base + componentName;
                            var componentScssScan = componentPath + "\\*.scss";
                            //console.log(componentPath,componentScssScan);
                            //console.log(componentPath + "\\" + componentName+".scss");
                            //scan the component dir for scss
                            gulp.src(componentScssScan)
                                //process the sass
                                .pipe(sass())
                                .pipe(gulp.dest(dest));
                                //delete the temporary scss
                                //.pipe(del(componentPath + "\\" + componentName+".scss"));
                        }));
    
                }
    
                //process the js
                if(stats.isDirectory() && basename == "js"){
                    //console.log(array,componentName,basename);
                    //console.log(file.path);
                    var scssScan = base + fpath+"\\*.js";
                    console.log("scan",scssScan );
                    console.log("dest",dest);
                    //scan dir for js
                    gulp.src(scssScan)
                        //concat all js in a temporary file
                        .pipe(concat(componentName+".js"))
                        .pipe(gulp.dest(dest));
    
                }
    
                cb(undefined,undefined);
            }));
    });