如何使用Gulp向文件添加一行文本

时间:2022-06-20 06:07:58

I've been trying to figure out how to add a line of text to any type of file using Gulp.

我一直试图弄清楚如何使用Gulp为任何类型的文件添加一行文本。

For instance add:

比如添加:

@import 'plugins'

to my main.sass file.

到我的main.sass文件。

Or add a CDN to an index.html file.

或者将CDN添加到index.html文件中。

I did try:

我试过了:

gulp.task('inject-plugins', function(){   gulp.src('src/css/main.sass')    .pipe(inject.after('// Add Imports', '\n@import \'plugins\'\n'));});

with no joy. Any idea how I could achieve this please?

没有快乐。知道我怎么能做到这一点好吗?

1 个解决方案

#1


9  

Depends on what you want to do.

取决于你想做什么。

If you just want to add text to the beginning or end of a file gulp-header and gulp-footer are your friends:

如果你只是想在文件的开头或结尾添加文字gulp-header和gulp-footer是你的朋友:

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'));});

If you have some kind of "anchor" text in your file you can use gulp-replace:

如果你的文件中有某种“锚”文本,你可以使用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 + '\n@import \'plugins\'\n'))    .pipe(gulp.dest('dist'));});

Finally there's the swiss army knife of vinyl file manipulation: map-stream. This gives you direct access to file contents and allows you to do any kind of string manipulation that you can think of in JavaScript:

最后是瑞士军刀的乙烯基文件操作:map-stream。这使您可以直接访问文件内容,并允许您在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'));});

#1


9  

Depends on what you want to do.

取决于你想做什么。

If you just want to add text to the beginning or end of a file gulp-header and gulp-footer are your friends:

如果你只是想在文件的开头或结尾添加文字gulp-header和gulp-footer是你的朋友:

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'));});

If you have some kind of "anchor" text in your file you can use gulp-replace:

如果你的文件中有某种“锚”文本,你可以使用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 + '\n@import \'plugins\'\n'))    .pipe(gulp.dest('dist'));});

Finally there's the swiss army knife of vinyl file manipulation: map-stream. This gives you direct access to file contents and allows you to do any kind of string manipulation that you can think of in JavaScript:

最后是瑞士军刀的乙烯基文件操作:map-stream。这使您可以直接访问文件内容,并允许您在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'));});