Phụ thuộc vào những gì bạn muốn làm.
Nếu bạn chỉ muốn thêm văn bản vào đầu hoặc cuối của một tập tin gulp-header
và gulp-footer
là bạn bè:
var header = require('gulp-header');
var footer = require('gulp-footer');
gulp.task('add-text-to-beginning', function() {
return gulp.src('src/css/main.sass')
.pipe(header('@import \'plugins\'\n'))
.pipe(gulp.dest('dist'));
});
gulp.task('add-text-to-end', function() {
return gulp.src('src/css/main.sass')
.pipe(footer('@import \'plugins\''))
.pipe(gulp.dest('dist'));
});
Nếu bạn có một số loại "neo" văn bản trong tập tin của bạn, bạn có thể sử dụng gulp-replace
:
var replace = require('gulp-replace');
gulp.task('replace-text', function() {
var anchor = '// Add Imports';
return gulp.src('src/css/main.sass')
.pipe(replace(anchor, anchor + '\[email protected] \'plugins\'\n'))
.pipe(gulp.dest('dist'));
});
Cuối cùng, con dao quân đội Thụy Sĩ xử lý tệp vinyl: map-stream
. Điều này cho phép bạn truy cập trực tiếp vào nội dung tệp và cho phép bạn thực hiện bất kỳ loại thao tác chuỗi nào mà bạn có thể nghĩ đến trong JavaScript:
var map = require('map-stream');
gulp.task('change-text', function() {
return gulp.src('src/css/main.sass')
.pipe(map(function(file, cb) {
var fileContents = file.contents.toString();
// --- do any string manipulation here ---
fileContents = fileContents.replace(/foo/, 'bar');
fileContents = 'First line\n' + fileContents;
// ---------------------------------------
file.contents = new Buffer(fileContents);
cb(null, file);
}))
.pipe(gulp.dest('dist'));
});
Nguồn
2016-07-14 14:13:00
Đừng quên thêm các thư viện đó! Nếu bạn đang sử dụng npm: thêm chúng vào "devDependencies": {..} trong tệp package.json và chạy "npm install" –