【问题标题】:How to add .watch to gulpfile.js如何将 .watch 添加到 gulpfile.js
【发布时间】:2021-05-31 12:59:59
【问题描述】:

我正在使用 gulp 来缩小 JS 和 CSS。 一切正常,但我需要添加 .watch 以自动执行此过程。

有人帮帮我吗?

这是我的代码:

// Require the npm modules we need
var gulp = require("gulp"),
    rename = require("gulp-rename"),
    cleanCSS = require("gulp-clean-css"),
    terser = require("gulp-terser");

// Looks for a file called styles.css inside the css directory
// Copies and renames the file to styles.min.css
// Minifies the CSS
// Saves the new file inside the css directory
function minifyCSS() {
  return gulp.src("./_statika/css/style.css")
    .pipe(rename("style.min.css"))
    .pipe(cleanCSS())
    .pipe(gulp.dest("./css"));
}

// Looks for a file called app.js inside the js directory
// Copies and renames the file to app.min.js
// Minifies the JS
// Saves the new file inside the js directory
function minifyJS() {
  return gulp.src("./_statika/js/scripts.js")
    .pipe(rename("scripts.min.js"))
    .pipe(terser())
    .pipe(gulp.dest("./js"));
}

// Makes both functions available as a single default task
// The two functions will execute asynchronously (in parallel)
// The task will run when you use the gulp command in the terminal
exports.default = gulp.parallel(minifyCSS, minifyJS);

非常感谢

【问题讨论】:

    标签: node.js gulp


    【解决方案1】:

    试试这个:

    gulp.watch("./_statika/css/style.css", minifyCSS);
    gulp.watch("./_statika/js/scripts.js", minifyJS);
    

    【讨论】:

    • 您应该包括如何将此代码添加到默认任务。
    • DiPierro 非常感谢,你能添加完整的代码吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-31
    • 2010-12-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-08
    相关资源
    最近更新 更多