【问题标题】:Is it possible in Gulp to use a string to start with instead of a source file?在 Gulp 中是否可以使用字符串而不是源文件开头?
【发布时间】:2015-08-15 04:28:42
【问题描述】:

通常当我使用 Gulp 时,我会从:

gulp.src('some/file.txt).pipe( ... ) // etc

我也可以使用字符串代替文件吗?例如:

gulp.str('some text').pipe( .... ) // etc

我正在处理的用例是我正在创建一个 SCSS 字符串 ad-hoc 并希望将它通过管道传输到 libsass。但我不想创建一个临时文件,而是使用原始字符串。示例:

var myString = 'html { color: red; }';
gulp.str(myString).pipe(sass()) // etc

这可能吗?

【问题讨论】:

  • 你试过了吗?
  • 我不确定“你试过了吗?”是什么意思。我尝试了各种东西。 gulp.str() 纯粹是支持我的问题的假设方法。到目前为止,我想出的是尝试使用 gulp-sass / node-sass 的 dataoutFile 选项,但到目前为止没有运气:return gulp.src('./foo.txt', {read: false}) .pipe(sass({data: 'html { color: red; }', outFile: 'test.css'})) .pipe(gulp.dest('.'));
  • 啊,从示例代码中我不确定这是否是真正的方法。感谢您的澄清。

标签: string sass gulp


【解决方案1】:

你可以试试这样的:

var file = require('gulp-file');

gulp.task('build', function () {
  var str = 'Some string value that you want to pipe';

  return file('tmp.js', str, { src: true }) // The file name can be anything
    ... // <- Piping contents of str here
    .pipe(gulp.dest('xxx'));
});

【讨论】:

  • 这对我来说非常棒——可以在here找到正在使用的 gulp-file 插件。
猜你喜欢
  • 2017-03-01
  • 2014-01-15
  • 1970-01-01
  • 2016-09-16
  • 2011-11-24
  • 1970-01-01
  • 2012-04-01
  • 1970-01-01
  • 2014-03-20
相关资源
最近更新 更多