Search code examples
javascriptnode.jssassgulpejs

Gulp creates dist folder but doesn't generate files (without errors)


I'm new to working with node, ejs and gulp, I have my project structured like this:

public
   css
     style.scss
   js
   images
views
   components
     navbar.ejs
   index.ejs
gulpfile.js
server.js

I tried this gulpfile.js:

const { src, dest, watch, series, parallel } = require('gulp');

const sourcemaps = require('gulp-sourcemaps');
var ejs = require("gulp-ejs");
const sass = require('gulp-sass');
const concat = require('gulp-concat');
const uglify = require('gulp-uglify');
const postcss = require('gulp-postcss');
const autoprefixer = require('autoprefixer');
const cssnano = require('cssnano');
var replace = require('gulp-replace');


// File paths
const files = {
    scssPath: 'puclic/css/**/*.scss',
    jsPath: 'public/js/**/*.js',
    ejsPath: 'views/*.ejs'
}


function scssTask() {
    return src(files.scssPath)
        .pipe(sourcemaps.init()) // initialize sourcemaps first
        .pipe(sass()) // compile SCSS to CSS
        .pipe(postcss([autoprefixer(), cssnano()])) // PostCSS plugins
        .pipe(sourcemaps.write('.')) // write sourcemaps file in current directory
        .pipe(dest('dist')); // put final CSS in dist folder
}


function jsTask() {
    return src([
            files.jsPath
            //,'!' + 'includes/js/jquery.min.js', // to exclude any specific files
        ])
        .pipe(concat('all.js'))
        .pipe(uglify())
        .pipe(dest('dist'));
}

function ejsTask() {
    return src(files.ejsPath)
    .pipe(ejs({}, {ext: '.html'}))
    .pipe(dest('dist'))
}


function watchTask() {
    watch([files.scssPath, files.jsPath, files.ejsPath],
        parallel(scssTask, jsTask, ejsTask));
}


exports.default = series(
    parallel(scssTask, jsTask, ejsTask),
    watchTask
);

I want to generate the dist folder with 'index.html' instead of 'index.ejs', 'style.css' instead of 'style.scss' and a minified js.

When I run gulp, it starts and finished the tasks and starts watching. A dist folder is created, but inside it I only get an index.ejs file and nothing else.

Gulp doesn't show any errors.

node v10.14; npm v6.7; gulp cli v2.2; gulp local v4.0


Solution

  • As I commented on your post, the reason this wasn't working was because you had a typo in your scssPath.

    Changing scssPath: 'puclic/css/**/*.scss', to scssPath: 'public/css/**/*.scss',

    will resolve the error.