Reputation: 271
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
Upvotes: 1
Views: 1287
Reputation: 182781
For your .ejs
problem, you are misusing its options (assuming you are using v4 of gulp-ejs). You have
.pipe(ejs({}, {ext: '.html'}))
But see renaming file extensions:
As of version 4, the third api parameter settings was removed. You can no longer provide an extension. This is because it falls out of the scope of gulp-ejs. So if you need to save the file with a different extension you can use gulp-rename.
Here's an example for template files with .ejs extension that are rendered into .html files:
const ejs = require('gulp-ejs')
const rename = require('gulp-rename')
gulp.src('./templates/*.ejs')
.pipe(ejs({ title: 'gulp-ejs' }))
.pipe(rename({ extname: '.html' }))
.pipe(gulp.dest('./dist'))
Upvotes: 1
Reputation: 552
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.
Upvotes: 1