我的项目 = 编码网站,我正在使用 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/