gulp-dynamic/task/smartcss.js

56 lines
1.4 KiB
JavaScript
Raw Normal View History

2021-02-17 12:23:08 +01:00
const gulp = require('gulp'),
HelperUnit = require('./../tools/helperUnit'),
sass = require('gulp-sass'),
clean = require('gulp-clean-css'),
path = require('path'),
workers = [];
class SCSSWorker {
constructor(config, helper) {
this.config = config;
this.helper = helper.clone();
this.input = this.helper.replaceVariables(this.config.input);
this.helper.addConfigItem("$name", this.config.name);
this.helper.addConfigItem("$", this.input);
this.out = this.helper.replaceVariables(this.config.output);
this.watch = this.helper.replaceVariables(this.config.watch || '$/**/*.{scss,sass}');
gulp.task(this.taskName, this.work.bind(this));
}
startWatch() {
gulp.watch(this.watch, gulp.series(this.taskName))
}
get taskName() {
return `scss-${this.config.name || 'unknown'}`;
}
work() {
return gulp.src(this.input)
.pipe(sass().on('error', sass.logError))
.pipe(clean())
.pipe(gulp.dest(this.out));
}
}
module.exports = {
build: function () {
let array = [];
for (let worker of workers) {
array.push(worker.taskName);
}
return array;
},
prepare: function (config) {
const scss = config.scss || [];
for (let scssItem of scss) {
workers.push(new SCSSWorker(scssItem, config.helper));
}
},
startWatch: function () {
for (let worker of workers) {
worker.startWatch();
}
}
}