gpt4 book ai didi

javascript - 使用 gulp 检测文件更改

转载 作者:行者123 更新时间:2023-11-30 16:17:21 25 4
gpt4 key购买 nike

我有这个 gulpfile:

  var gulp = require('gulp'),
concat = require('gulp-concat'),
uglify = require('gulp-uglify');

gulp.task('minifyJS', function() {
return gulp.src(['src/*.js'])
.pipe(uglify())
.pipe(gulp.dest('min'));
});

gulp.task('watch', function() {
gulp.watch(['src/*.js'], ['minifyJS']);
});

我想知道什么文件触发观察者及其绝对路径。

例如:如果我的项目放在/myCode,我更改文件src/main.js,我想看到/myCode/src/main.jsminifyJS 任务中。有办法吗?

感谢您的宝贵时间。

最佳答案

您可以使用 gulp-ng-annotategulp-changed 来完成:

var gulp = require('gulp');
var changed = require('gulp-changed');
var rename = require('gulp-rename');
var ngAnnotate = require('gulp-ng-annotate'); // just as an example
var SRC = 'src/*.js';
var DEST = 'src/';

//Function to get the path from the file name
function createPath(file) {
var stringArray = file.split('/');
var path = '';
var name = stringArray[1].split('.');
stringArray = name[0].split(/(?=[A-Z])/);
if (stringArray.length>1) {stringArray.pop()};
return {folder: stringArray[0], name: name[0]}
}


gulp.task('default', function () {
return gulp.src(SRC)
.pipe(changed(DEST))
// ngAnnotate will only get the files that
// changed since the last time it was run
.pipe(ngAnnotate())
.pipe(rename(function (path) {
var createdPath = createPath(path);
path.dirname = createdPath.folder;
path.basename: createdPath.name,
path.prefix: "",
path.suffix: "",
path.extname: ".min.js"
}))
.pipe(gulp.dest(DEST));
});

结果:

enter image description here

关于javascript - 使用 gulp 检测文件更改,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35294738/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com