javascript - 运行 gulp.watch 或 gulp

标签 javascript gulp

我的项目 = 编码网站,我正在使用 gulp。 现在,当运行 gulp v4 时,我使用 cmd gulp watch 创建我的网站。 但我想知道 cmd gulp 的用途是什么,什么时候应该使用 gulp,什么时候应该使用 gulp watch? 我尝试使用 cmd gulp 但它给了我错误:任务从未定义:默认。 我知道我应该运行exports.default,但我想我还需要创建一个函数? 希望你能帮我弄清楚我是否需​​要使用 gulp 或 gulp watch 以及为导出.default 编写什么。 这是我的代码:)

const gulp = require('gulp');
const sass = require('gulp-sass');
const browserSync = require('browser-sync').create();
const autoprefixer = require('gulp-autoprefixer');



// complie scss to css

function style() {
    //1. ehere is the scss file
    return gulp.src('./scss/**/*.scss')
    //2. pass that file through sass compiler
        .pipe(sass().on('error', saas.logError))
    //3. where do I save the compled css?
        .pipe(gulp.dest('./css'))
    //4. stream changes to all browsers 
        .pipe(browserSync.stream())
    //5. fixes scaling
        .pipe(autoprefixer('last 3 versions'));
}

function watch() {
    browserSync.init({
        server: {
            baseDir: './'
        }
    });
    gulp.watch('./scss/**/*.scss', style);
    gulp.watch('./*index.html').on('change', browserSync.reload);
    gulp.watch('./js/**/*.js').on('change', browserSync.reload);
}

exports.style = style;
exports.watch = watch;

最佳答案

要运行 gulp cmd,您需要定义默认导出。

exports.default = watch

这可以方便地定义您经常使用的任务。

关于javascript - 运行 gulp.watch 或 gulp,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59445510/

相关文章:

javascript - ThreeJS 分子 - 双键

javascript - 匹配任何不以 .json 结尾的正则表达式

javascript - 通过 Node 脚本或命令行浏览器丑化?

node.js - 如何使用不同的 NODE_PATH 运行 NodeJS 单元测试

gulp - 为什么我的任务运行程序资源管理器中未创建子任务?

javascript - 当源文件夹中有多个jade文件时,gulp-jade仅构建一个HTML文件

javascript - 使用 $scope.$destroy 解决内存泄漏但破坏指令

javascript - 如何使用渲染 Prop 通过 Jest 正确模拟 React 组件

javascript - Javascript 中不区分大小写的匹配

javascript - 为 gulp HTML linter 设置选项/规则