Reputation: 3
I'm having issues setting a Gulp 4 task. When I run the default task which watch my files and run the functions everything works fine. The problem is when I try to run gulp prod or any other non-default task it executes, shows no errors but no folders or files are created.
This is a WordPress theme and I'm trying to setup development and production tasks. I'm running node 10.15.3, npm 6.4.1, gulp-cli 2.2.0 and gulp 4.0.0. I tried simplifying the functions and/or tasks, I read all the questions with problems like this but nothing worked.
My gulpfile as is right now:
const themename = "tree"
const { src, dest, watch, series, parallel } = require("gulp"),
autoprefixer = require("gulp-autoprefixer"),
browserSync = require("browser-sync").create(),
sass = require("gulp-sass"),
cleanCSS = require("gulp-clean-css"),
sourcemaps = require("gulp-sourcemaps"),
concat = require("gulp-concat"),
imagemin = require("gulp-imagemin"),
changed = require("gulp-changed"),
babel = require("gulp-babel"),
uglify = require("gulp-uglify"),
lineec = require("gulp-line-ending-corrector"),
rename = require("gulp-rename")
const root = `../${themename}`,
srcs = {
css: `${root}/src/css/**/*.css`,
sass: `${root}/sass`,
js: `${root}/src/js/app/**/*.js`,
img: `${root}/src/images/**/*.{jpe?g|gif|png|svg}`
},
dist = {
css: `${root}/dist/css/`,
js: `${root}/dist/js/`,
img: `${root}/dist/images/`
},
devPaths = {
css: [`${root}/dev/css/`],
js: `${root}/dev/js/`
},
watchFiles = {
php: `${root}/**/*.php`,
sass: `${srcs.sass}/**/*.scss`
}
function devSass() {
return src(`${srcs.sass}/style.scss`)
.pipe(sourcemaps.init({ loadMaps: true }))
.pipe(sass({ outputStyle: "expanded" }).on("error", sass.logError))
.pipe(autoprefixer("last 2 versions"))
.pipe(sourcemaps.write())
.pipe(lineec())
.pipe(dest(devPaths.css))
}
function devCss() {
return src([`${srcs.css}`, `${devPaths.css}/style.css`])
.pipe(sourcemaps.init({ loadMaps: true, largeFile: true }))
.pipe(concat("all.css"))
.pipe(sourcemaps.write(`${devPaths.css}/maps/`))
.pipe(lineec())
.pipe(dest(devPaths.css))
}
function devJs() {
return src(srcs.js)
.pipe(babel({ presets: ["@babel/env"] }))
.pipe(lineec())
.pipe(dest(devPaths.js))
}
function prodSass() {
return src(`${srcs.sass}/style.scss`)
.pipe(sass({ outputStyle: "compressed" }).on("error", sass.logError))
.pipe(autoprefixer("last 2 versions"))
.pipe(lineec())
.pipe(dest(dist.css))()
}
function prodCss() {
return src([srcs.css, devPaths.css])
.pipe(concat("all.min.css"))
.pipe(cleanCSS())
.pipe(lineec())
.pipe(dest(dist.css))
}
function prodJs() {
return src(srcs.js)
.pipe(rename({ suffix: ".min" }))
.pipe(babel({ presets: ["@babel/env"] }))
.pipe(uglify())
.pipe(dest(dist.js))
}
function imgOpt() {
return src(srcs.img)
.pipe(changed(dist.img))
.pipe(
imagemin([
imagemin.gifsicle({ interlaced: true }),
imagemin.jpegtran({ progressive: true }),
imagemin.optipng({ optimizationLevel: 5 })
])
)
.pipe(gulp.dest(dist.img))
}
function devWatchers() {
browserSync.init({
proxy: "localhost/tree/",
open: true,
notify: false
})
watch(watchFiles.sass, series([devSass, devCss]))
watch(srcs.js, devJs)
watch([srcs.img, dist.img]).on("change", browserSync.reload)
watch(watchFiles.php).on("change", browserSync.reload)
}
function prod() {
return parallel(prodJs, imgOpt, series(prodSass, prodCss))
}
exports.prod = prod
exports.devWatchers = devWatchers
exports.default = devWatchers
I expect that after running the prod task, the dist folder will be populated, but actually nothing happens. Once more, the default task runs everything.
Upvotes: 0
Views: 195
Reputation: 3
So I revised all my folder structure and all the strings inside the gulpfile. It was some problem with paths. I apologize if this should not be an answer. Everything works fine now.
Upvotes: 0